你真的懂了redis的数据结构吗?( 二 )


/* HyperLogLog defines */
#define CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES 30004.实例
4.1 字符串String
int :8个字节的长整型
embstr:小于44个字节的字符串(目前),3.0以前的版本为39
raw:大于39个字节小于512MB的字符串
object.c/* Create a string object with EMBSTR encoding if it is smaller than
* OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is
* used.
*
* The current limit of 44 is chosen so that the biggest string object
* we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */
#define OBJ_ENCODING_EMBSTR_SIZE_LIMIT 44验证一下:
Connected.
local:0>object encoding test1
"int"
local:0>object encoding test2
"embstr"
local:0>object encoding test3
"raw"
local:0>get test1
"10000"
local:0>get test2
"hello world!"
local:0>get test3
"Redis is not a plain key-value store, it is actually a data structures server, supporting different kinds of values. What this means is that, while in traditional key-value stores you associated string keys to string values, in Redis the value is not limited to a simple string, but can also hold more complex data structures. The following is the list of all the data structures supported by Redis, which will be covered separately in this tutorial:"
local:0>4.2 哈希hash
当filed的个数少于512,且没有value大于64字节时,内部编码为ziplist
当filed的个数大于512,或者value大于64字节时,内部编码为hashtable
Connected.
local:0>hmset hashtest1 field1 value1 field2 value2 field3 value3
"OK"
local:0>object encoding hashtest1
"ziplist"
local:0>hset hashtest2 field1 "Redis modules can access Redis built-in data structures both at high level, by calling Redis commands, and at low level, by manipulating the data structures directly."
"1"
local:0>object encoding hashtest2
"hashtable"
local:0>4.3 列表list
redis 3.2 之前
当列表list中的元素个数少于512,且没有value大于64字节时,内部编码为ziplist
当列表list中的元素个数大于512,或者value大于64字节时,内部编码为linkedlist
redis 3.2 之后
都使用quicklist
Connected.
local:0>rpush listtest1 value1 value2 value3 value4 value5
"5"
local:0>object encoding listtest1
"quicklist"
local:0>rpush listtest2 "Redis modules can access Redis built-in data structures both at high level, by calling Redis commands, and at low level, by manipulating the data structures directly."
"1"
local:0>object encoding listtest2
"quicklist"
local:0>4.4 集合set
当集合set中的元素都是整数且元素个数小于512(默认时)使用intset
其它条件使用hashtable
local:0>sadd settest1 1 2 3
"3"
local:0>object encoding settest1
"intset"
local:0>sadd settest2 "hello world!"
"1"
local:0>object encoding settest2
"hashtable"
local:0>4.5 有序集合zset
当有序集合zse中的元素个数少于128(默认),且没有value大于64字节时,内部编码为ziplist
【你真的懂了redis的数据结构吗?】当有序集合zse中的元素个数大于128(默认),或者value大于64字节时,内部编码为skiplist
Connected.
local:0>zadd zsettest1 10 value1 20 value2 30 value3
"3"
local:0>object encoding zsettest1
"ziplist"
local:0>zadd zsettest2 60 "Redis modules can access Redis built-in data structures both at high level, by calling Redis commands, and at low level, by manipulating the data structures directly."
"1"
local:0>object encoding zsettest2
"skiplist"
local:0>4.6 Geo
Connected.
local:0>GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
"2"
local:0>object encoding Sicily
"ziplist"
local:04.7 HyperLogLog
Connected.
local:0>PFADD hll a b c d e f g
"1"
local:0>object encoding h11
null
local:0>object encoding hll
"raw"
local:0>5 总结

  1. 外部数据结构类型可以通过type来查看
  2. 内部数据结构类型可以通过object来查看
  3. 理解内部数据结构的实现有助于我们深入理解redis
  4. 可以复习一下数据结构及其实现




推荐阅读