redis hash类型

field 或者 value的大小超出一定限制后,Redis 会在内部自动将 zipmap 替换成正常的 hash 实现. 这个限制可以在配置文件中指定hash 对象时开始是用 zipmap(又称为 small hash)来存储的。这个 zipmap 其实并不是 hash table,但是 zipmap 相比正常的 hash 实现可以节省不少 hash 本身需要的一些元数据存储开销。

hash-max-zipmap-entries 64 #配置字段最多 64 个

hash-max-zipmap-value 512 #配置 value 最大为 512 字节

1 hset

设置 hash field 为指定值,如果 key 不存在,则先创建。

127.0.0.1:6379> hset hash name wxc

(integer) 1

2 hsetnx

置 hash field 为指定值,如果 key 不存在,则先创建。如果 field 已经存在,返回 0。

127.0.0.1:6379> hset hash name wxc

(integer) 1

127.0.0.1:6379> hsetnx hash eamil wxc@126.com

(integer) 1

127.0.0.1:6379> hsetnx hash name wxcc

(integer) 0

3 hmset

存在即覆盖

127.0.0.1:6379> hmset hash name wxc email wxc@126.com

OK

4 hget

获取指定的 hash field。

5 hmget

获取全部指定的 hash filed

127.0.0.1:6379> hmget hash name email

1) "wxc"

2) "wxc@126.com"

6 hincrby

指定的 hash filed 加上给定值。类似incrby。默认为0。

127.0.0.1:6379> hincrby hash age 4

(integer) 4

7 hexists

测试指定 field 是否存在。

8 hlen

返回指定 hash 的 field 数量。

127.0.0.1:6379> hmset hash name wxc email wxc@126.com age 13

OK

127.0.0.1:6379> hmget hash name email age

1) "wxc"

2) "wxc@126.com"

3) "13"

127.0.0.1:6379> hlen hash

(integer) 3

9 hdel

删除指定 hash 的 field 。

127.0.0.1:6379> hlen hash

(integer) 3

127.0.0.1:6379> hdel hash name

(integer) 1

127.0.0.1:6379> hlen hash

(integer) 2

10 hkeys

返回 hash 的所有 field。

127.0.0.1:6379> hkeys hash

1) "email"

2) "age"

11 hvals

返回 hash 的所有 value。

127.0.0.1:6379> hkeys hash

1) "email"

2) "age"

127.0.0.1:6379> hvals hash

1) "wxc@126.com"

2) "13"

12 hgetall

获取某个 hash 中全部的 filed 及 value。

127.0.0.1:6379> hgetall hash

1) "email"

2) "wxc@126.com"

3) "age"

4) "13"

13 hscan

14 hincrbyfloat

为哈希表key中的域field加上浮点数增量increment。

如果哈希表中没有域field,那么hincrbyfloat会先将域field的值设为0,然后再执行加法操作。

如果键key不存在,那么hincrbyfloat会先创建一个哈希表,再创建域field,最后再执行加法操作。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容