Redis系列2-Redis底层数据结构

RedisDB

typedef struct redisDb {
    dict *dict;                 /* The keyspace for this DB */
    dict *expires;              /* Timeout of keys with a timeout set */
    dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP)*/
    dict *ready_keys;           /* Blocked keys that received a PUSH */
    dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */
    int id;                     /* Database ID */
    long long avg_ttl;          /* Average TTL, just for stats */
    unsigned long expires_cursor; /* Cursor of the active expire cycle. */
    list *defrag_later;         /* List of key names to attempt to defrag one by one, gradually. */
} redisDb;

RedisObject

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:4位(bit),表示对象的类型
  • encoding:4位(bit),表示对象的内部编码,Redis 可以根据不同的使用场景来为对象设置不同的编码,大大提高了 Redis 的灵活性和效率。
  • lru: 24位(bit),高16位存储一个分钟数级别的时间戳,低8位存储访问计数(lfu : 最近访问次数)
  • refcount: 记录的是该对象被引用的次数,类型为整型。refcount 的作用,主要在于对象的引用计数和内存回收。当对象的refcount>1时,称为共享对象
  • ptr: 指针指向具体的数据

7种type

  1. 字符串对象sds
    Redis 使用了 SDS(Simple Dynamic String)。用于存储字符串和整型数据。
  2. 跳跃表skiplist
  3. 字典dict
  4. 压缩列表ziplist
  5. 整数集合intset
  6. 快速列表quicklist
  7. 流对象stream

10种encoding

  1. intset
  2. hashtable
  3. int
  4. embstr
  5. raw
  6. quicklist
  7. ziplist
  8. skiplist
上一篇:Rest Template 常见错误


下一篇:Python的基础详情