Hash

82 阅读1分钟
简介

Redis hash是一个键值对集合。

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

类似java中的Map

用户ID为查找的key,存储的value用户对象包含姓名,年龄,生日等信息,如果用普通的key/value结构来存储

image.png

实操 将对象存入 redis

常用命令

hset 给集合中的field键赋值

image.png

hget 从集合中

image.png

hmset

hexists(是否存在)

image.png

hkeys(所有field)

image.png

hvals(所有属性值)

image.png

hincrby(给属性的值添加值)

image.png

hsetnx(如果字段不存在,添加字段)

image.png

数据结构

Hash类型对应的数据结构是两种: ziplist(压缩列表), hashtable(哈希表)。当field-value长度较短且个数较少时,使用ziplist,否则使用hashtable。