hash对象的编码可以是ziplist或者字典。
ziplist编码类型
每个键值对使用两个紧挨在一起的压缩列表节点来保存,第一个节点保存key,第二个节点则保存value。数据采取尾部插入的方式进行存储。
字典编码类型
hash对象中的每个键值对都使用字典中的一个键值对进行保存,正正好。
对象中不同类型的适用条件:
只有同时满足以下两个条件才会使用ziplist进行编码:
- 对象保存的所有键值对的键和值的字符串长度都小于64字节;
- 键值对的数量小于512个;
对象类型的结构图
-
压缩列表类型编码
压缩列表对象.PNG
字典类型编码
对象类型的转换
当ziplist编码的对象有任意条件不符合时就会转换成字典编码。
127.0.0.1:6379> hset profile name "Tom"
(integer) 1
127.0.0.1:6379> object encoding profile
"ziplist"
127.0.0.1:6379> hset profile description "I just want to have a long sentence. This sentence should be more than 64 byte to make this hash set's encoding type change from ziplist to dic."
(integer) 1
127.0.0.1:6379> object encoding profile
"hashtable"
网友评论