Redis Hash存储对象

Hash

哈希表是一种数据结构,而Redis Hash也是采用哈希表来实现的存储。

Hash命令

1、赋值(hset key filed value)
127.0.0.1:6379> hset uid name zhonglimo
(integer) 1
127.0.0.1:6379> hsetnx uid name zhonglimo
(integer) 0

hset不存在则创建,存在则修改。
hsetnx不存在则创建,存在则返回0。

2、批量赋值
127.0.0.1:6379> hmset uid name zhonglimo age 24
OK
3、获取值
127.0.0.1:6379> hget uid name
"zhonglimo"
127.0.0.1:6379> hmget uid name age
1) "zhonglimo"
2) "24"
127.0.0.1:6379> hgetall uid
1) "name"
2) "zhonglimo"
3) "age"
4) "24"

hiredis中Redis执行命令的返回值结构体为redisReply ,如果成功的是一个数组,比如上述hgetgall返回了4个值,则返回的redisReply中elements为4代表长度为4,element则为这4个返回值地址数组首地址。
typedef struct redisReply {
int type; /* REDIS_REPLY_* /
long long integer; /
The integer when type is REDIS_REPLY_INTEGER /
int len; /
Length of string */
char str; / Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING /
size_t elements; /
number of elements, for REDIS_REPLY_ARRAY */
struct redisReply *element; / elements vector for REDIS_REPLY_ARRAY */
} redisReply;

4、获取key对应的域值对个数
127.0.0.1:6379> hlen uid
(integer) 2
5、获取key对应的所有的域
127.0.0.1:6379> hkeys uid
1) "name"
2) "age"
6、获取key对应的所有的值
1) "name"
2) "age"
127.0.0.1:6379> hvals uid
1) "zhonglimo"
2) "24"
7、判断key中是否存在某域
127.0.0.1:6379> hexists uid name
(integer) 1
8、删除key中某域
127.0.0.1:6379> hdel uid age
(integer) 1
127.0.0.1:6379> hgetall uid
1) "name"
2) "zhonglimo"

Hash不支持直接设置过期时间,可以在设置好值域之后,再使用EXPIRE key seconds或者PEXPIRE key milliseconds来设置过期时间,使用PERSIST key移除过期时间。

使用Hash存储对象结构

127.0.0.1:6379> hmset 20201010 name zhonglimo age 24 sex man
OK
127.0.0.1:6379> hgetall 20201010
1) "name"
2) "zhonglimo"
3) "age"
4) "24"
5) "sex"
6) "man"
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值