Redis 原理

Redis 原理

  • KISS ,Keep It Sample and Stupid

底层数据结构

typedef struct redisObject {
    // 类型 string list set hash zset等 4bit
    unsigned type:4;
    // 编码方式 4bit
    unsigned encoding:4;
    // LRU 时间 24bit
    unsigned lru:LRU_BITS; 
    // 引用计数  4byte
    int refcount;
    // 指向对象的指针  8byte
    void *ptr;
} robj;

4bit+4bit+24bit+4Byte+8Byte=16Byte。

 REDIS_ENCODING_INT(long 类型的整数)
 REDIS_ENCODING_EMBSTR embstr (编码的简单动态字符串)
 REDIS_ENCODING_RAW (简单动态字符串)
 REDIS_ENCODING_HT (字典)
 REDIS_ENCODING_LINKEDLIST (双端链表)
 REDIS_ENCODING_ZIPLIST (压缩列表)
 REDIS_ENCODING_INTSET (整数集合)
 REDIS_ENCODING_SKIPLIST (跳跃表和字典)

常用命令

  1. 《闲扯Redis二》String数据类型之底层解析,http://www.yund.tech/zdetail.html?type=1&id=585ee331353551a44b29a9e9a09a1570
命令:
 object encoding key ,获取数据底层的数据结构
 object idletime命令
 object refcount
 strlen key

  1. Redis如何查看单个key所占用的内存大小,https://blog.csdn.net/st1527960117/article/details/113058990
redis-memory-for-key -s localhost -p 6379 -d 0 key

疑问

  • key=1,value=1,的 数据结构为什么占用空间为40B??

渐进式rehash

过期时间

typedef struct redisDb {
    // ...
    dict *expires;
    // ...
} redisDb;

优化建议

 string
 hash

数据结构

字典

  • Redis底层数据结构之hash
typedef struct dict{
    dictType *type;
    void *privdata;
    dictht ht[2];
    int trehashidx;
} dict;

typedef struct dictht{
    dictEntry **table;
    unsigned long size;
    unsigned long sizemask;
    unsigned long used;
}dictht;

typedef struct dictEntry{
    void *key;
    union{
        void *val;
        uint64_tu64;
        int64_ts64;
    }v;
    struct dictEntry *next;
}dictEntry;
在64位系统中,一个dictEntry对象占24字节(key/val/next各占8字节)。

posted @ 2021-07-07 18:02  WANGHAO229  阅读(36)  评论(0)    收藏  举报