redis有哪些数据结构?
字符串String、字典Hash、列表List、集合Set、有序集合SortedSet 。
很多人面试时都遇到过这种场景吧?
其实除了上面的几种常见数据结构,还需要加上 数据结构HyperLogLog、Geo 。
可是很多人不知道redis 不仅有上面的几种数据结构,还内藏了内部的数据结构 。即redis可以分为外部数据结构和内部数据结构 。
1. 如何查看redis的数据结构?
1.1 如何查看redis的外部数据结构?
可以使用type命令,返回key的类型,如 string , list , set , zset , hash 和 stream,实例如下:
redis> SET key1 "value"
"OK"
redis> LPUSH key2 "value"
(integer) 1
redis> SADD key3 "value"
(integer) 1
redis> TYPE key1
"string"
redis> TYPE key2
"list"
redis> TYPE key3
"set"
redis>1.2 如何查看redis的内部数据结构
可以通过Object命令来查看 。object命令允许从内部察看给定 <tt class="docutils literal" style="box-sizing: border-box;">key</tt> 的 Redis 对象 。
它通常用在除错(debugging)或者了解为了节省空间而对 <tt class="docutils literal" style="box-sizing: border-box;">key</tt> 使用特殊编码的情况 。
当将Redis用作缓存程序时,你也可以通过它命令中的信息,决定 <tt class="docutils literal" style="box-sizing: border-box;">key</tt> 的驱逐策略(eviction policies) 。
2.redis数据结构的定义redisObject
内部数据类型server.h
typedef struct redisObject {
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). */
int refcount;
void *ptr;
} robj;其中,type为redis的外部数据结构,encoding为redis的内部数据结构实现
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 */内部类型总结
文章插图
3.数据结构的限制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
推荐阅读
- Web测试之功能测试总结,你不知道的这都有
- 祝我生日快乐经典句子 祝你生日快乐的句子
- 喝红茶真的好吗 红茶的十三大功效作用总结
- 这样喝茶,别说你体验过,估计你见也没见过!
- @所有人!助力余杭大径山,就差你一票!
- 秋分时节贵在补血滋阴 两款养生粥让你满“血”归来
- 又是一年秋分到 养生指南伴你行
- 你好火焰蓝电视剧什么时候播出 你好火焰蓝电视剧开拍了吗
- 车险中的不计免赔险是什么?一定要弄清楚,不懂就要吃亏了
- 车借给朋友后出事故咋办?交警:借出去前做好这4点,没你事