设置哈希缓存
<?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" }