redis3.0.7源码阅读(九)redis对象

矫情吗;* 2022-07-13 11:58 147阅读 0赞

版本:3.0.7

1.源文件

redis.h

object.c

t_hash.c

t_list.c

t_set.c

t_string.c

t_zset.c

2.数据结构

redis使用对象来表示数据库中的键和值(k/v),每创建一个键值对时,至少会创建两个对象,分别用于k/v,redis的键(k)总是字符串,值(v)则可以是字符串对象/列表对象/集合对象/有序集合对象/哈希对象,每种类型的对象又至少使用了两种不同的编码。举个例子,

127.0.0.1:6379> hset a b 1

(integer) 1

127.0.0.1:6379> type a

hash

127.0.0.1:6379> object encoding a

“ziplist”

  1. /*
  2. * 对象类型
  3. * 通过TYPE命令可查看值的类型
  4. */
  5. /* Object types */
  6. // 字符串对象
  7. #define REDIS_STRING 0
  8. // 列表对象
  9. #define REDIS_LIST 1
  10. // 集合对象
  11. #define REDIS_SET 2
  12. // 有序集合对象
  13. #define REDIS_ZSET 3
  14. // 哈希对象
  15. #define REDIS_HASH 4
  16. /*
  17. * 对象编码
  18. * 通过OBJECT ENCODING命令可查看值的编码类型
  19. */
  20. /* Objects encoding. Some kind of objects like Strings and Hashes can be
  21. * internally represented in multiple ways. The 'encoding' field of the object
  22. * is set to one of this fields for this object. */
  23. // 动态字符串
  24. #define REDIS_ENCODING_RAW 0 /* Raw representation */
  25. // 整数
  26. #define REDIS_ENCODING_INT 1 /* Encoded as integer */
  27. // 哈希表
  28. #define REDIS_ENCODING_HT 2 /* Encoded as hash table */
  29. // 压缩map
  30. #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
  31. // 双端链表
  32. #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */
  33. // 压缩表
  34. #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
  35. // 整数集合
  36. #define REDIS_ENCODING_INTSET 6 /* Encoded as intset */
  37. // 跳跃表
  38. #define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
  39. // embstr编码的动态字符串
  40. #define REDIS_ENCODING_EMBSTR 8 /* Embedded sds string encoding */
  41. /*
  42. * Redis 对象
  43. */
  44. typedef struct redisObject {
  45. // 对象类型
  46. unsigned type:4;
  47. // 对象编码
  48. unsigned encoding:4;
  49. // 对象最后一次被访问的时间
  50. unsigned lru:REDIS_LRU_BITS; /* lru time (relative to server.lruclock) */
  51. // 引用计数
  52. int refcount;
  53. // 指向实际值的指针
  54. void *ptr;
  55. } robj;

3.内存分布

4.对象编码

4.1 字符串对象的编码:

int/raw/embstr

4.2 列表对象的编码:

ziplist/linkedlist

4.3 哈希对象的编码

ziplist/hashtables

4.4 集合对象的编码

inset/hashtables

4.5 有序集合对象的编码

ziplist/skiplist

注1:redis在执行命令时,会先进行对象类型检查,对象类型检查通过后,再进行对象编码检查,根据编码类型调用处理函数。

注2:redis会对整数值的字符串对象进行共享,OBJECT ENCODING查看编码类型

注3:OBJECT IDLETIME查看键的空闲时间

原文出自: http://blog.csdn.net/daiyudong2020/article/details/54236690

End;

发表评论

表情:
评论列表 (有 0 条评论,147人围观)

还没有评论,来说两句吧...

相关阅读