PHP Redis - Hash (哈希)

2023-03-07,,

Redis hash 是一个string类型的field和value的映射表,特别适合用于存储对象。

Redis 中每个 hash 可以存储  232-1(4294967295) 键值对

赋值(hset)

$redis->hSet('website_bd', '百度搜索', 'www.baidu.com');
$redis->hSet('website_bd', '百度地图', 'map.baidu.com');
$redis->hSet('website_bd', '百度新闻', 'news.baidu.com');

批量赋值(hMset)

$website_all = [
'bing' => 'https://cn.bing.com/',
'360' => 'https://www.so.com/',
'soso' => 'http://www.soso.com/',
    'page' => 340,
'sogou' => 'https://www.sogou.com/'
];
$redis->hMSet('website_all',$website_all);

为表中不存在的字段赋值(hSetNx)

$redis->hSetNx('website_bd', '百度贴吧', 'https://tieba.baidu.com/');

获取指定字段的值(hGet)

$redis->hGet('website_bd','百度搜索');

获取所有给定字段的值(hMget)

$redis->hMget('website_all', ['bing', 'age360]);

获取所有表中的字段(hKeys)

$redis->hKeys('website_all');

获取表中所有值(hVals)

$redis->hVals('website_all');

获取全部元素(hGetAll)

$redis->hGetAll('website_all');

删除某个元素(hDle)

$redis->hDel('website_hd', '百度新闻');

判断元素是否存在(hExists)

$redis->hExists('website_all', 'baidu');

获取长度(hLen)

$redis->hLen('website_all');

哈希表中的字段增加指定增量值(hIncrBy)

$redis->hIncrBy('website_all', page', 10);

PHP Redis - Hash (哈希)的相关教程结束。

《PHP Redis - Hash (哈希).doc》

下载本文的Word格式文档,以方便收藏与打印。