关于redis:redis对象

4次阅读

共计 906 个字符,预计需要花费 3 分钟才能阅读完成。

redis 中每个对象,都用 redisObject 示意:

struct {
  // 5 大数据类型
  unsigned type:4;![](/img/bVcRMBj)
  // 9 大编码类型
  unsigned encoding:4;
  // 最初拜访工夫
  unsigned lru:24;
  // 援用计数
  int refcount;
  // 底层数据结构指针
  void *ptr;
}

数据类型 (type)

type 命令能够输入某个键的对象类型

编码类型

object encoding 命令能够输入某个键的编码类型

#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 /* Encoded as regular linked list */
#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 */

数据类型对应编码类型利用

拜访工夫 lru

lru 示意该对象最初一次被拜访的工夫,其占用 24 个 bit 位。保留该值的目标是为了计算该对象的空转时长,便于后续依据空转时长来决定是否开释该键,回收内存。

援用计数 refcount

Redis 对每一个对象设定了援用计数 refcount 字段,程序通过该字段的信息,在适当的时候主动开释内存进行内存回收。此性能与 C ++ 的智能指针类似

正文完
 0