redis zse如何取值_你真的懂redis的數據結構了嗎?redis內部數據結構和外部數據結構揭秘...
Redis有哪些數(shù)據(jù)結(jié)構(gòu)?
字符串String、字典Hash、列表List、集合Set、有序集合SortedSet。
很多人面試時(shí)都遇到過(guò)這種場(chǎng)景吧?
其實(shí)除了上面的幾種常見數(shù)據(jù)結(jié)構(gòu),還需要加上數(shù)據(jù)結(jié)構(gòu)HyperLogLog、Geo。
可是很多人不知道redis 不僅有上面的幾種數(shù)據(jù)結(jié)構(gòu),還內(nèi)藏了內(nèi)部的數(shù)據(jù)結(jié)構(gòu)。即redis可以分為外部數(shù)據(jù)結(jié)構(gòu)和內(nèi)部數(shù)據(jù)結(jié)構(gòu)。
1. 如何查看redis的數(shù)據(jù)結(jié)構(gòu)?
1.1 如何查看redis的外部數(shù)據(jù)結(jié)構(gòu)?
可以使用type命令,返回key的類型,如string,?list,?set,?zset,?hash?和stream,實(shí)例如下:
redis> SET key1 "value"
"OK"redis> LPUSH key2 "value"(integer)1redis> SADD key3 "value"(integer)1redis>TYPE key1"string"redis>TYPE key2"list"redis>TYPE key3"set"redis>
1.2 如何查看redis的內(nèi)部數(shù)據(jù)結(jié)構(gòu)
可以通過(guò)Object命令來(lái)查看。object命令允許從內(nèi)部察看給定?key?的 Redis 對(duì)象。
它通常用在除錯(cuò)(debugging)或者了解為了節(jié)省空間而對(duì)
key?使用特殊編碼的情況。
當(dāng)將Redis用作緩存程序時(shí),你也可以通過(guò)它命令中的信息,決定
key?的驅(qū)逐策略(eviction policies)。
2.redis數(shù)據(jù)結(jié)構(gòu)的定義redisObject
內(nèi)部數(shù)據(jù)類型server.h
typedef structredisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS;/*LRU time (relative to global lru_clock) or
* LFU data (least significant 8 bits frequency
* and most significant 16 bits access time).*/
intrefcount;void *ptr;
} robj;
其中,type為redis的外部數(shù)據(jù)結(jié)構(gòu),encoding為redis的內(nèi)部數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)
type的值如下:
/*-----------------------------------------------------------------------------
* Data types
*----------------------------------------------------------------------------*/
/*A redis object, that is a type able to hold a string / list / set*/
/*The actual Redis Object*/
#define OBJ_STRING 0 /* String object. */
#define OBJ_LIST 1 /* List object. */
#define OBJ_SET 2 /* Set object. */
#define OBJ_ZSET 3 /* Sorted set object. */
#define OBJ_HASH 4 /* Hash object. */
/*The "module" object type is a special one that signals that the object
* is one directly managed by a Redis module. In this case the value points
* to a moduleValue struct, which contains the object value (which is only
* handled by the module itself) and the RedisModuleType struct which lists
* function pointers in order to serialize, deserialize, AOF-rewrite and
* free the object.
*
* Inside the RDB file, module types are encoded as OBJ_MODULE followed
* by a 64 bit module type ID, which has a 54 bits module-specific signature
* in order to dispatch the loading to the right module, plus a 10 bits
* encoding version.*/
#define OBJ_MODULE 5 /* Module object. */
#define OBJ_STREAM 6 /* Stream object. */
encoding的值如下:server.h
/*Objects encoding. Some kind of objects like Strings and Hashes can be
* internally represented in multiple ways. The 'encoding' field of the object
* is set to one of this fields for this object.*/
#define OBJ_ENCODING_RAW 0 /* Raw representation */
#define OBJ_ENCODING_INT 1 /* Encoded as integer */
#define OBJ_ENCODING_HT 2 /* Encoded as hash table */
#define OBJ_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
#define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
#define OBJ_ENCODING_INTSET 6 /* Encoded as intset */
#define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
#define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */
#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */
#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */
內(nèi)部類型總結(jié)
3.數(shù)據(jù)結(jié)構(gòu)的限制server.h
/*Zipped structures related defaults*/
#define OBJ_HASH_MAX_ZIPLIST_ENTRIES 512
#define OBJ_HASH_MAX_ZIPLIST_VALUE 64
#define OBJ_SET_MAX_INTSET_ENTRIES 512
#define OBJ_ZSET_MAX_ZIPLIST_ENTRIES 128
#define OBJ_ZSET_MAX_ZIPLIST_VALUE 64
#define OBJ_STREAM_NODE_MAX_BYTES 4096
#define OBJ_STREAM_NODE_MAX_ENTRIES 100
/*HyperLogLog defines*/
#define CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES 3000
4.實(shí)例
4.1 字符串String
int :8個(gè)字節(jié)的長(zhǎng)整型
embstr:小於44個(gè)字節(jié)的字符串(目前),3.0以前的版本為39
raw:大於39個(gè)字節(jié)小於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
驗(yàn)證一下:
Connected.
local:0>objectencoding test1"int"local:0>objectencoding test2"embstr"local:0>objectencoding 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
當(dāng)filed的個(gè)數(shù)少於512,且沒(méi)有value大於64字節(jié)時(shí),內(nèi)部編碼為ziplist
當(dāng)filed的個(gè)數(shù)大於512,或者value大於64字節(jié)時(shí),內(nèi)部編碼為hashtable
Connected.
local:0>hmset hashtest1 field1 value1 field2 value2 field3 value3"OK"local:0>objectencoding 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>objectencoding hashtest2"hashtable"local:0>
4.3? 列表list
redis 3.2 之前
當(dāng)列表list中的元素個(gè)數(shù)少於512,且沒(méi)有value大於64字節(jié)時(shí),內(nèi)部編碼為ziplist
當(dāng)列表list中的元素個(gè)數(shù)大於512,或者value大於64字節(jié)時(shí),內(nèi)部編碼為linkedlist
redis 3.2 之后
都使用quicklist
Connected.
local:0>rpush listtest1 value1 value2 value3 value4 value5"5"local:0>objectencoding 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>objectencoding listtest2"quicklist"local:0>
4.4 集合set
當(dāng)集合set中的元素都是整數(shù)且元素個(gè)數(shù)小於512(默認(rèn)時(shí))使用intset
其它條件使用hashtable
local:0>sadd settest1 1 2 3
"3"local:0>objectencoding settest1"intset"local:0>sadd settest2 "hello world!"
"1"local:0>objectencoding settest2"hashtable"local:0>
4.5 有序集合zset
當(dāng)有序集合zse中的元素個(gè)數(shù)少於128(默認(rèn)),且沒(méi)有value大於64字節(jié)時(shí),內(nèi)部編碼為ziplist
當(dāng)有序集合zse中的元素個(gè)數(shù)大於128(默認(rèn)),或者value大於64字節(jié)時(shí),內(nèi)部編碼為skiplist
Connected.
local:0>zadd zsettest1 10 value1 20 value2 30value3"3"local:0>objectencoding 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>objectencoding 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>objectencoding Sicily"ziplist"local:0
4.7?HyperLogLog
Connected.
local:0>PFADD hll a b c d e f g"1"local:0>objectencoding h11nulllocal:0>objectencoding hll"raw"local:0>
5 總結(jié)
1. 外部數(shù)據(jù)結(jié)構(gòu)類型可以通過(guò)type來(lái)查看
2.內(nèi)部數(shù)據(jù)結(jié)構(gòu)類型可以通過(guò)object來(lái)查看
3. 理解內(nèi)部數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)有助於我們深入理解redis
4. 可以復(fù)習(xí)一下數(shù)據(jù)結(jié)構(gòu)及其實(shí)現(xiàn)
總結(jié)
以上是生活随笔為你收集整理的redis zse如何取值_你真的懂redis的數據結構了嗎?redis內部數據結構和外部數據結構揭秘...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 格力变频空调遥控器怎么调灯光
- 下一篇: for循环java_java的这三种fo