Redis中String的底層實現


查看Key的內部編碼

object encoding key

String的三種編碼

  1. int
  2. embstr
  3. raw

int

存儲長整型,且長度不能超過2^64-1

emstr

存儲字符串。內存是連續的,有長度限制(39/44個字節,不同版本有差異),且是只讀。

raw

存儲字符串。內存是非連續的,長度超出限制時使用。需要注意的是,如果使用append追加key的value,不論其是否int或者embstr的長度是否超出限制,編碼會變成raw。

String經典應用場景

  1. 緩存
  2. 用戶會話
  3. 計數器(increment decrement)

參考:
object.c 中,長度限制(版本:3.2)

/* Create a string object with EMBSTR encoding if it is smaller than
 * REIDS_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is
 * used.
 *
 * The current limit of 39 is chosen so that the biggest string object
 * we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */
#define OBJ_ENCODING_EMBSTR_SIZE_LIMIT 44
robj *createStringObject(const char *ptr, size_t len) {
    if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT)
        return createEmbeddedStringObject(ptr,len);
    else
        return createRawStringObject(ptr,len);
}



Redis內部存儲原理大綱


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM