# string 182.168.168.226:6379> set age 18 -> Redirected to slot [741] located at 182.168.106.129:6379 OK 182.168.106.129:6379> type age string # list 182.168.106.129:6379> lpush friends a b c (integer) 3 182.168.106.129:6379> type friends list # set 182.168.106.129:6379> sadd ids 1 2 3 (integer) 3 182.168.106.129:6379> type ids set # zset 182.168.106.129:6379> zadd info 100 mqray 90 lmq (integer) 2 182.168.168.226:6379> type info zset # hash 182.168.168.226:6379> hset myhset name mqray (integer) 1 182.168.50.160:6379> type myhset hash
encoding
在客户端中,可以使用object encoding key 获取到对象所使用的编码格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
182.168.106.129:6379> object encoding age "int" 182.168.106.129:6379> object encoding name -> Redirected to slot [5798] located at 182.168.168.226:6379 "embstr" 182.168.168.226:6379> object encoding friends -> Redirected to slot [420] located at 182.168.106.129:6379 "quicklist" 182.168.50.160:6379> object encoding ids -> Redirected to slot [3296] located at 182.168.106.129:6379 "intset" 182.168.106.129:6379> object encoding info -> Redirected to slot [5642] located at 182.168.168.226:6379 "ziplist" 182.168.168.226:6379> object encoding myhset -> Redirected to slot [13092] located at 182.168.50.160:6379 "ziplist"
redisObject.encoding取值如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Objects encoding. Some kind of objects like Strings and Hashes can be * internally represented in multiple ways. The 'encoding' field of the object * is set to one of this fields for this object. */ #define OBJ_ENCODING_RAW 0 /* Raw representation */ #define OBJ_ENCODING_INT 1 /* Encoded as integer */ #define OBJ_ENCODING_HT 2 /* Encoded as hash table */ #define OBJ_ENCODING_ZIPMAP 3 /* No longer used: old hash encoding. */ #define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */ #define OBJ_ENCODING_ZIPLIST 5 /* No longer used: old list/hash/zset encoding. */ #define OBJ_ENCODING_INTSET 6 /* Encoded as intset */ #define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */ #define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */ #define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of listpacks */ #define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */ #define OBJ_ENCODING_LISTPACK 11 /* Encoded as a listpack */
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached. You can select one from the following behaviors: # # volatile-lru -> Evict using approximated LRU, only keys with an expire set. # allkeys-lru -> Evict any key using approximated LRU. # volatile-lfu -> Evict using approximated LFU, only keys with an expire set. # allkeys-lfu -> Evict any key using approximated LFU. # volatile-random -> Remove a random key having an expire set. # allkeys-random -> Remove a random key, any key. # volatile-ttl -> Remove the key with the nearest expire time (minor TTL) # noeviction -> Don't evict anything, just return an error on write operations.
set key value // 为多个键设置值 mset key value [key value] // 只有在key不尊在的情况下将key的值设置为value setnx key value // 将键key的值设置为value,并将key的过期时间设置为 seconds;如果key已存在,则覆盖已有值 setex key seconds value
182.168.168.226:6379> set age 18 OK 182.168.106.129:6379> object encoding age "int"
对应的redisObject如图所示:
raw
如果该字符串的长度大于44个字节,则将其对象编码设置为raw
1 2 3 4 5 6
182.168.106.129:6379> set long org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert OK 182.168.106.129:6379> strlen long (integer) 83 182.168.168.226:6379> object encoding long "raw"
/* Create a string object with encoding OBJ_ENCODING_EMBSTR, that is * an object where the sds string is actually an unmodifiable string * allocated in the same chunk as the object itself. */ robj *createEmbeddedStringObject(constchar *ptr, size_t len) { robj *o = zmalloc(sizeof(robj)+sizeof(struct sdshdr8)+len+1); structsdshdr8 *sh = (void*)(o+1);
/* Create a string object with encoding OBJ_ENCODING_RAW, that is a plain * string object where o->ptr points to a proper sds string. */ robj *createRawStringObject(constchar *ptr, size_t len) { return createObject(OBJ_STRING, sdsnewlen(ptr,len)); }
/* quicklist is a 40 byte struct (on 64-bit systems) describing a quicklist. * 'count' is the number of total entries. * 'len' is the number of quicklist nodes. * 'compress' is: 0 if compression disabled, otherwise it's the number * of quicklistNodes to leave uncompressed at ends of quicklist. * 'fill' is the user-requested (or default) fill factor. * 'bookmarks are an optional feature that is used by realloc this struct, * so that they don't consume memory when not used. */ typedefstructquicklist { quicklistNode *head; quicklistNode *tail; unsignedlong count; /* total count of all entries in all listpacks */ unsignedlong len; /* number of quicklistNodes */ signedint fill : QL_FILL_BITS; /* fill factor for individual nodes */ unsignedint compress : QL_COMP_BITS; /* depth of end nodes not to compress;0=off */ unsignedint bookmark_count: QL_BM_BITS; quicklistBookmark bookmarks[]; } quicklist;
```C /* quicklistNode is a 32 byte struct describing a listpack for a quicklist. * We use bit fields keep the quicklistNode at 32 bytes. * count: 16 bits, max 65536 (max lp bytes is 65k, so max count actually < 32k). * encoding: 2 bits, RAW=1, LZF=2. * container: 2 bits, PLAIN=1 (a single item as char array), PACKED=2 (listpack with multiple items). * recompress: 1 bit, bool, true if node is temporary decompressed for usage. * attempted_compress: 1 bit, boolean, used for verifying during testing. * extra: 10 bits, free for future use; pads out the remainder of 32 bits */ typedefstructquicklistNode { structquicklistNode *prev; structquicklistNode *next; unsignedchar *entry; size_t sz; /* entry size in bytes */ unsignedint count : 16; /* count of items in listpack */ unsignedint encoding : 2; /* RAW==1 or LZF==2 */ unsignedint container : 2; /* PLAIN==1 or PACKED==2 */ unsignedint recompress : 1; /* was this node previous compressed? */ unsignedint attempted_compress : 1; /* node can't compress; too small */ unsignedint dont_compress : 1; /* prevent compression of entry that will be used later */ unsignedint extra : 9; /* more bits to steal for future usage */ } quicklistNode;
1 2 3 4 5 6 7 8 9
/* quicklistLZF is a 8+N byte struct holding 'sz' followed by 'compressed'. * 'sz' is byte length of 'compressed' field. * 'compressed' is LZF data with total (compressed) length 'sz' * NOTE: uncompressed length is stored in quicklistNode->sz. * When quicklistNode->entry is compressed, node->entry points to a quicklistLZF */ typedefstructquicklistLZF { size_t sz; /* LZF size in bytes*/ char compressed[]; } quicklistLZF;
// 将 sdiff key [key1] 结果 保存至 dest sdiffstore dest key [key1]
实际操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
182.168.168.226:6379> sadd dep xdr -> Redirected to slot [3555] located at 182.168.106.129:6379 (integer) 1 182.168.106.129:6379> sadd dep edr (integer) 1 182.168.106.129:6379> smembers dep 1) "xdr" 2) "edr" 182.168.106.129:6379> sadd dep af (integer) 1 182.168.106.129:6379> sadd dep cwpp (integer) 1 182.168.50.160:6379> SMEMBERS dep 1) "cwpp" 2) "xdr" 3) "af" 4) "edr