Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1852697
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: NOSQL

2019-03-19 22:12:52

之前写了redis当中的底部实现以及几种类型的简单命令,今天来聊一聊redis当中的对象模型~~
redis为每种外部可以访问到的数据结构提供了一个叫做对象类型的抽象,其底层实现是基于之前咱们讲过的adlist,ziplist等等那一堆,其对外隐藏了这些细节转而实现大家都知道的在文档当中介绍的命令。其实,在我们在redis当中创建某个数据类型的时候,redis为我们至少创建了两个对象,一个对象是存储该数据类型的名称的字符串对象,另一个就是实际存储这个数据类型的对象了。
在redis当中,其每一个对象都用一个redisObject结构体来表示,其展示如下:

点击(此处)折叠或打开

  1. typedef struct redisObject {
  2.     unsigned type:4;
  3.     unsigned encoding:4;
  4.     unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
  5.                             * LFU data (least significant 8 bits frequency
  6.                             * and most significant 16 bits access time). */
  7.     int refcount;
  8.     void *ptr;
  9. } robj;
其中type和encoding分别表示当前对象的种类以及编码类型,具体取值见下面的代码:

点击(此处)折叠或打开

  1. /* A redis object, that is a type able to hold a string / list / set */
  2. /* The actual Redis Object */
  3. #define OBJ_STRING 0
  4. #define OBJ_LIST 1
  5. #define OBJ_SET 2
  6. #define OBJ_ZSET 3
  7. #define OBJ_HASH 4

  8. /* Objects encoding. Some kind of objects like Strings and Hashes can be
  9.  * internally represented in multiple ways. The 'encoding' field of the object
  10.  * is set to one of this fields for this object. */
  11. #define OBJ_ENCODING_RAW 0 /* Raw representation */
  12. #define OBJ_ENCODING_INT 1 /* Encoded as integer */
  13. #define OBJ_ENCODING_HT 2 /* Encoded as hash table */
  14. #define OBJ_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
  15. #define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
  16. #define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
  17. #define OBJ_ENCODING_INTSET 6 /* Encoded as intset */
  18. #define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
  19. #define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */
  20. #define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */

lru记录的是内存置换策略,refcount是引用当前对象的个数,ptr则指向底层的具体实现,具体的一些限制如下:

点击(此处)折叠或打开

  1. #define LRU_BITS 24
  2. #define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */
  3. #define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */

  4. #define OBJ_SHARED_REFCOUNT INT_MAX
后续会单独介绍单独对象的实现方式~~~
阅读(9597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~