Redis底层是怎样保存一个key和value的?


在了解这个之前,首先要了解一下redis中定义的几种数据结构。

sds(简单动态字符串)

SDS以Redis中定义的一种用于存储字符串的数据结构。redis中保存的key都是以sds的形式存储的。

sds在redis源码中的定义如下:

typedef char *sds;

//sdshdr5 is never used
struct __attribute__ ((__packed__)) sdshdr5 {
   
    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
   
    uint8_t len;  // 当前字符数组长度
    uint8_t alloc; //当前字符数组总共分配的内存大小
    unsigned char flags; //当前字符数组的属性。用来标识到底是sdshdr8 还是sdshdr16 等
    char buf[];  //字符串真正的值
};
struct __attribute__ ((__packed__)) sdshdr16 {
   
    uint16_t len; /* used */
    uint16_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {
   
    uint32_t len; /* used */
    uint32_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr64 {
   
    
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值