查看Key的內部編碼
object encoding key
String的三種編碼
- int
- embstr
- raw
int
存儲長整型,且長度不能超過2^64-1
emstr
存儲字符串。內存是連續的,有長度限制(39/44個字節,不同版本有差異),且是只讀。
raw
存儲字符串。內存是非連續的,長度超出限制時使用。需要注意的是,如果使用append追加key的value,不論其是否int或者embstr的長度是否超出限制,編碼會變成raw。
String經典應用場景
- 緩存
- 用戶會話
- 計數器(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);
}