php-使用哈希对象缓存

设置哈希缓存

<?php  
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);
    $data = $driver->hGetAll('people');
    var_dump($data);exit;

output

array(4) { ["name"]=> string(3) "hls" ["age"]=> string(2) "20" ["height"]=> string(3) "180" ["width"]=> string(2) "55" }

获取哈希缓存对象的所有key

<?php  
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);
    $data = $driver->hKeys('people');
    var_dump($data);exit;

output

array(4) { [0]=> string(4) "name" [1]=> string(3) "age" [2]=> string(6) "height" [3]=> string(5) "width" }

获取哈希缓存对象的长度(key的个数)

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);
    $data = $driver->hLen('people');
    var_dump($data);exit;

output

int(4)

获取哈希缓存对象某个属性进行递增

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);
    $driver->hIncrBy('people','age',5);
    $data = $driver->hGetAll('people');
    var_dump($data);exit;

使用hIncrBy如果递增不存在的key 会默认创建key

output

array(4) { ["name"]=> string(3) "hls" ["age"]=> string(2) "25" ["height"]=> string(3) "180" ["width"]=> string(2) "55" }

获取哈希缓存对象指定的key

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);

    $data = $driver->hGet('people','name');
    var_dump($data);exit;

output

string(3) "hls"

获取哈希缓存对象指定的keys(数组返回)

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);

    $data = $driver->hMGet('people',['name','age']);
    var_dump($data);exit;

output

array(2) { ["name"]=> string(3) "hls" ["age"]=> string(2) "20" }

不存在的key 返回的值为false

设置哈希缓存对象指定的key的value

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);
    $driver->hSet('people','age',100);
    $data = $driver->hGetAll('people');
    var_dump($data);exit;

output

array(6) { ["name"]=> string(3) "hls" ["age"]=> string(3) "100" ["height"]=> string(3) "180" ["width"]=> string(2) "55" ["age1"]=> string(2) "10" ["age2"]=> string(1) "5" }

判断哈希缓存对象指定的key是否存在

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);


    $result = $driver->hExists('people','qwe');
    var_dump($result);exit;

output

bool(false)

删除哈希对象指定的属性key

<?php
    error_reporting(E_ALL);
    ini_set('display_errors','On');
    $driver = new Redis();
    $driver->connect('127.0.0.1',6379);
    $driver->select(0);
    $driver->delete('people');
    $driver->hMset('people',[
        'name' => 'hls',
        'age' => 20,
        'height' => 180,
        'width' => 55,
    ]);


    $result = $driver->hDel('people','name');
    $data = $driver->hGetAll('people');
    var_dump($data);exit;

output

array(3) { ["age"]=> string(2) "20" ["height"]=> string(3) "180" ["width"]=> string(2) "55" }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值