Redis hash 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象。Redis 中每个 hash 最多可以存储 2^32 - 1 键值对(40多亿)。
我们先了解下hash的内部编码实现,其分为两种:压缩列表ziplist和哈希表hashtable。数据量存储较小的情况下使用ziplist,其满足一下设置的两个条件,就会使用ziplist:
1)、hash-max-ziplist-entries,默认值为512,哈希元素少于该配置,
2)、hash-max-ziplist-value,默认值为64,哈希值小于该配置
我们打开安装目录下的文件redis.windows-service.conf或redis.windows.conf,可以看到其配置:
# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
看名字就知道ziplist使用了更加紧凑的结构实现多个元素的连续存储,所以ziplist比hashtable更节省内存;但是由于hashtable的读写时间复杂度为O(1),所以其读写效率要