{"id":201818,"date":"2025-05-10T12:58:59","date_gmt":"2025-05-10T04:58:59","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201818\/"},"modified":"2025-05-10T12:58:59","modified_gmt":"2025-05-10T04:58:59","slug":"redis%e4%ba%94%e7%a7%8d%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201818\/","title":{"rendered":"Redis\u4e94\u79cd\u6570\u636e\u7c7b\u578b\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e94\u79cd\u6570\u636e\u7c7b\u578b\u8be6\u89e3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRedis\u4e94\u79cd\u6570\u636e\u7c7b\u578b\u8be6\u89e3\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Redis\u6570\u636e\u7c7b\u578b<\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<h2>\u4ec0\u4e48\u662f Redis<\/h2>\n<p>Redis \u662f<strong>\u57fa\u4e8e\u5185\u5b58\u7684 K-V \u6570\u636e\u5e93<\/strong>\uff0c\u5e38\u7528\u4e8e<strong>\u7f13\u5b58<\/strong>\u3001<strong>\u6d88\u606f\u961f\u5217<\/strong>\uff0c<strong>\u5206\u5e03\u5f0f\u9501<\/strong>\u7b49\u573a\u666f\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff1a<strong>\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u3001\u5e26\u6392\u5e8f\u7684\u96c6\u5408<\/strong><\/p>\n<h1>Redis \u6570\u636e\u7c7b\u578b\u8be6\u89e3<\/h1>\n<h3>\u524d\u7f6e\u77e5\u8bc6<\/h3>\n<p>Redis\u4e2d\u7684\u4efb\u610f\u6570\u636e\u7c7b\u578b\u7684\u952e\u548c\u503c\u90fd\u4f1a\u88ab\u5c01\u88c5\u4e3a\u4e00\u4e2a RedisObject<\/p>\n<pre>typedef struct redisObject {\n    unsigned type:4;\n    unsigned encoding:4;\n    unsigned lru:LRU_BITS; \/* LRU time (relative to global lru_clock) or\n                            * LFU data (least significant 8 bits frequency\n                            * and most significant 16 bits access time). *\/\n    int refcount;\n    void *ptr;\n} robj;<\/pre>\n<p><code>type<\/code>\uff1a\u6307\u5177\u4f53\u7684\u5bf9\u8c61\u7c7b\u578b<\/p>\n<p><code>encoding<\/code>\uff1a\u5e95\u5c42\u7f16\u7801\u65b9\u5f0f<\/p>\n<p><code>lru<\/code>\uff1a\u8bb0\u5f55\u6700\u540e\u4e00\u6b21\u88ab\u8bbf\u95ee\u7684\u65f6\u95f4<\/p>\n<p><code>refcount<\/code>\uff1a\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570\u5668\uff0c\u8ba1\u6570\u5668\u4e3a 0 \u65f6\u8868\u793a\u6ca1\u6709\u88ab\u5f15\u7528\uff0c\u53ef\u4ee5\u56de\u6536<\/p>\n<p><code>ptr<\/code>\uff1a\u5b9e\u9645\u5b58\u50a8\u7684\u503c<\/p>\n<p><strong>\u4e94\u79cd\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>Redis\u4e2d\u4f1a\u6839\u636e\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff0c\u9009\u62e9\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u6bcf\u79cd\u6570\u636e\u7c7b\u578b\u7684\u4f7f\u7528\u7684\u7f16\u7801\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<th><strong>\u6570\u636e\u7c7b\u578b<\/strong><\/th>\n<th><strong>\u7f16\u7801\u65b9\u5f0f<\/strong><\/th>\n<\/tr>\n<tr>\n<td>OBJ_STRING<\/td>\n<td>int\u3001embstr\u3001raw<\/td>\n<\/tr>\n<tr>\n<td>OBJ_LIST<\/td>\n<td>LinkedList\u548cZipList(3.2\u4ee5\u524d)\u3001QuickList\uff083.2\u4ee5\u540e\uff09<\/td>\n<\/tr>\n<tr>\n<td>OBJ_SET<\/td>\n<td>intset\u3001HT<\/td>\n<\/tr>\n<tr>\n<td>OBJ_ZSET<\/td>\n<td>ZipList\u3001HT\u3001SkipList<\/td>\n<\/tr>\n<tr>\n<td>OBJ_HASH<\/td>\n<td>ZipList\u3001HT<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Redis \u7684\u7f16\u7801\u65b9\u5f0f<\/strong><\/p>\n<p>Redis \u4e2d\u4f1a\u6839\u636e\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff0c\u9009\u62e9\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u5171\u5305\u542b 11 \u79cd\u4e0d\u540c\u7c7b\u578b\uff1a<\/p>\n<table>\n<tbody>\n<tr>\n<th><strong>\u7f16\u53f7<\/strong><\/th>\n<th><strong>\u7f16\u7801\u65b9\u5f0f<\/strong><\/th>\n<th><strong>\u8bf4\u660e<\/strong><\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>OBJ_ENCODING_RAW<\/td>\n<td>raw\u7f16\u7801\u52a8\u6001\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>OBJ_ENCODING_INT<\/td>\n<td>long\u7c7b\u578b\u7684\u6574\u6570\u7684\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>OBJ_ENCODING_HT<\/td>\n<td>hash\u8868\uff08\u5b57\u5178dict\uff09<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>OBJ_ENCODING_ZIPMAP<\/td>\n<td>\u5df2\u5e9f\u5f03<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>OBJ_ENCODING_LINKEDLIST<\/td>\n<td>\u53cc\u7aef\u94fe\u8868<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>OBJ_ENCODING_ZIPLIST<\/td>\n<td>\u538b\u7f29\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>OBJ_ENCODING_INTSET<\/td>\n<td>\u6574\u6570\u96c6\u5408<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>OBJ_ENCODING_SKIPLIST<\/td>\n<td>\u8df3\u8868<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>OBJ_ENCODING_EMBSTR<\/td>\n<td>embstr\u7684\u52a8\u6001\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>OBJ_ENCODING_QUICKLIST<\/td>\n<td>\u5feb\u901f\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>OBJ_ENCODING_STREAM<\/td>\n<td>Stream\u6d41<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>String<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>\u5b57\u7b26\u4e32 (string) \u5176\u5b9e\u662f\u6700\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u5b58\u50a8\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5b58\u50a8\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u4e8c\u8fdb\u5236\u6570\u636e\u7b49\uff1b<\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<p><code>set key value<\/code>\uff1a\u6dfb\u52a0\u4e00\u4e2a string \u7c7b\u578b\u7684\u952e\u503c\u5bf9<\/p>\n<p><code>get key<\/code>\uff1a\u83b7\u53d6 key \u5bf9\u5e94\u7684 value<\/p>\n<p><code>mset<\/code>\uff1a\u6279\u91cf\u8bbe\u7f6e\u5bf9\u4e2a\u952e\u503c\u5bf9<\/p>\n<p><code>mget<\/code>\uff1a\u6279\u91cf\u83b7\u53d6\u591a\u4e2a\u952e\u503c\u5bf9<\/p>\n<p><code>incr<\/code>\uff1a\u8ba9\u4e00\u4e2a\u6574\u5f62\u81ea\u589e 1<\/p>\n<p><code>setnx<\/code>\uff1a\u6dfb\u52a0\u4e00\u4e2a string \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c<strong>\u524d\u63d0\u662f\u8fd9\u4e2a key \u4e0d\u5b58\u5728<\/strong><\/p>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<p><strong>\u7f13\u5b58\u5bf9\u8c61<\/strong><\/p>\n<p>\u76f4\u63a5\u7f13\u5b58\u6574\u4e2a json \u5b57\u7b26\u4e32\u5bf9\u8c61 <code>SET user:1 '{\"name\":\"ezreal\", \"age\":18}'<\/code><\/p>\n<p>\u7f13\u5b58\u66f4\u65b0\u7b56\u7565<\/p>\n<ul>\n<li>\u4f7f\u7528 redis \u7684<strong>\u5185\u5b58\u6dd8\u6c70\u673a\u5236<\/strong>\uff1b<\/li>\n<li>\u8d85\u65f6\u5254\u9664\uff0c\u7ed9\u6570\u636e<strong>\u6dfb\u52a0 TTL \u8fc7\u671f\u65f6\u95f4<\/strong>\uff1b<\/li>\n<li>\u5ba2\u6237\u7aef<strong>\u4e3b\u52a8\u66f4\u65b0\u7f13\u5b58<\/strong>\uff1b<\/li>\n<\/ul>\n<p><strong>\u5206\u5e03\u5f0f\u9501<\/strong><\/p>\n<p>\u4f7f\u7528 setnx \u6307\u4ee4\u53ef\u4ee5\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff0c\u82e5\u8bbe\u7f6e\u503c\u6210\u529f\u5219\u8868\u793a\u83b7\u53d6\u9501\u6210\u529f\uff0c\u8bbe\u7f6e\u5931\u8d25\u5219\u83b7\u53d6\u9501\u5931\u8d25\uff0c\u6ce8\u610f\u8981\u8bbe\u7f6e\u8be5 key \u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u9632\u6b62\u4e00\u76f4\u62a2\u5360\u8be5\u9501<\/p>\n<p><strong>\u5e38\u89c4\u8ba1\u6570<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7 <code>incr<\/code> \u6307\u4ee4\u6765\u7edf\u8ba1\u6570\u91cf\uff0c\u524d\u63d0\u8bbe\u7f6e\u7684 value \u9700\u8981\u662f\u6574\u5f62<\/p>\n<p><strong>\u5171\u4eab session<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u628a\u591a\u53f0\u670d\u52a1\u5668\u4e0a\u7684 session \u7edf\u4e00\u653e\u5230 redis \u4e2d\uff0c\u65e0\u8bba\u5411\u96c6\u7fa4\u54ea\u53f0\u670d\u52a1\u5668\u53d1\u9001\u6d88\u606f\uff0c\u90fd\u4fdd\u8bc1\u53bb redis \u4e2d\u83b7\u53d6 session \u7684\u4fe1\u606f<\/p>\n<h3>\u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>redis \u7684\u5e95\u5c42\u5b9e\u73b0\u662f SDS \u5b57\u7b26\u4e32<\/p>\n<h3>SDS \u7ed3\u6784\u4f53<\/h3>\n<pre>struct __attribute__ ((__packed__)) sdshdr8 {\n    uint8_t len; \/* buf\u5df2\u4fdd\u5b58\u7684\u5b57\u7b26\u4e32\u5b57\u8282\u6570\uff0c\u4e0d\u5305\u542b\u7ed3\u675f\u6807\u793a *\/\n    uint8_t alloc; \/* buf\u7533\u8bf7\u7684\u603b\u7684\u5b57\u8282\u6570\uff0c\u4e0d\u5305\u542b\u7ed3\u675f\u6807\u793a *\/\n    unsigned char flags; \/* \u4e0d\u540c\u7684SDS\u5934\u7c7b\u578b *\/\n    char buf[];\n};<\/pre>\n<p><code>len<\/code>\uff1a\u8be5\u5b57\u7b26\u4e32\u7684 \u957f\u5ea6<\/p>\n<p><code>alloc<\/code>\uff1abuf \u7533\u8bf7\u7684 \u603b\u5b57\u8282\u6570<\/p>\n<p><code>buf<\/code>\uff1a\u5b9e\u9645\u5b58\u50a8\u6570\u636e\u7684\u4f4d\u7f6e<\/p>\n<p><code>flag<\/code>\uff1aSDS \u5934\u7c7b\u578b\uff0c\u7528\u6765\u63a7\u5236SDS\u5934\u5927\u5c0f \uff1a<code>SDS_TYPE_5<\/code>\uff0c<code>SDS_TYPE_8<\/code>\uff0c<code>SDS_TYPE_16<\/code>\uff0c<code>SDS_TYPE_32<\/code>\uff0c<code>SDS_TYPE_64<\/code><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937632645ed9603bd60.jpg1683937632645ed9603bd73.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u6574\u5f62\u7684\u5b58\u50a8<\/strong><\/p>\n<p>\u5982\u679c\u5b58\u50a8\u7684\u662f\u4e00\u4e2a\u6574\u5f62\uff0c\u5219\u5219\u76f4\u63a5\u5b58\u50a8\u5728 RedisObject \u4e2d\u5373\u53ef\uff1b<\/p>\n<p><strong>SDS \u52a8\u6001\u6269\u5bb9<\/strong><\/p>\n<p>\u5047\u5982\u73b0\u5728\u5728 name \u5b57\u7b26\u4e32\u540e\u52a0\u5165 ezreal\uff0c\u5f62\u6210\u65b0\u7684\u5b57\u7b26\u4e32 &#8216;nameezreal&#8217;<\/p>\n<ul>\n<li>\u82e5<strong>\u65b0\u5b57\u7b26\u4e32<\/strong>\u7684\u957f\u5ea6\u5c0f\u4e8e 1M\uff0c\u5219\u65b0\u7684<strong>\u5185\u5b58\u7a7a\u95f4<\/strong>\u4e3a \u65b0\u5b57\u7b26\u4e32 \u7684<strong>\u957f\u5ea6\u7684\u4e24\u500d + 1<\/strong>\uff1b<\/li>\n<li>\u82e5<strong>\u65b0\u5b57\u7b26\u4e32<\/strong>\u7684\u957f\u5ea6\u5927\u4e8e 1M\uff0c\u5219\u65b0\u7684<strong>\u5185\u5b58\u7a7a\u95f4<\/strong>\u4e3a \u65b0\u5b57\u7b26\u4e32 \u7684<strong>\u957f\u5ea6 + 1M + 1\uff0c\u79f0\u4e3a\u5185\u5b58\u9884\u5206\u914d<\/strong>\uff1b<\/li>\n<\/ul>\n<h2>List<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>\u5217\u8868\u662f\u4e00\u4e2a<strong>\u6709\u5e8f<\/strong>\u7684\u5b57\u7b26\u4e32\u96c6\u5408\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u6765\u8bbf\u95ee\u6307\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5728\u5217\u8868\u5934\u90e8\u6216\u8005\u5c3e\u90e8\u6765\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20<\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<p><code>LPUSH KEY<\/code>\uff1a\u5411\u5217\u8868\u5de6\u4fa7\u63d2\u5165\u5143\u7d20<\/p>\n<p><code>RPUSH KEY<\/code>\uff1a\u5411\u5217\u8868\u53f3\u4fa7\u63d2\u5165\u5143\u7d20<\/p>\n<p><code>LPOP KEY<\/code>\uff1a\u5411\u5217\u8868\u5de6\u4fa7\u5f39\u51fa\u5143\u7d20<\/p>\n<p><code>RPOP KEY<\/code>\uff1a\u5411\u5217\u8868\u53f3\u4fa7\u5f39\u51fa\u5143\u7d20<\/p>\n<p><code>LRANGE KEY star end<\/code>\uff1a\u83b7\u53d6\u6307\u5b9a\u8303\u56f4\u5185\u7684 key<\/p>\n<p><code>BLPOP \/ BRPOP<\/code>\uff1a\u4e0e LPOP \/ RPOP \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u5728\u6ca1\u6709\u5143\u7d20\u65f6\u4f1a\u7b49\u5f85<\/p>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<p><strong>\u6d88\u606f\u961f\u5217<\/strong><\/p>\n<p>\u6d88\u606f\u961f\u5217\u5b9e\u73b0\u7684\u4e09\u5927\u8981\u7d20\uff1a<strong>\u6d88\u606f\u4fdd\u5e8f<\/strong>\uff0c<strong>\u5904\u7406\u91cd\u590d\u7684\u6d88\u606f<\/strong>\uff0c<strong>\u4fdd\u8bc1\u6d88\u606f\u7684\u53ef\u9760\u6027<\/strong><\/p>\n<ul>\n<li><strong>\u6d88\u606f\u4fdd\u5e8f<\/strong><\/li>\n<\/ul>\n<p>\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e LPUSH \/ RPOP \u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e00\u4e2a\u961f\u5217\uff0c\u6d88\u606f\u5148\u8fdb\u5148\u51fa\uff0c\u4fdd\u8bc1\u6d88\u606f\u7684\u987a\u5e8f\uff1b<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937632645ed9606cd24.jpg1683937632645ed9606cd37.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5f53\u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u6d88\u8d39\u8005\u53ea\u80fd\u4e0d\u65ad\u7684\u8f6e\u8be2\u8be5\u961f\u5217\u662f\u5426\u6709\u6d88\u606f\uff0c\u56e0\u4e3a\u6d88\u8d39\u8005\u65b0\u589e\u4e00\u6761\u6d88\u606f\u540e\u662f\u4e0d\u80fd\u901a\u77e5\u6d88\u8d39\u8005\u7684\uff0c\u6240\u4ee5\u6d88\u8d39\u8005\u8981\u4e0d\u65ad\u8f6e\u8be2 <code>while(1)<\/code>\uff0c\u4e0d\u65ad\u7684\u5360\u7528 CPU\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>BRPOP<\/code> \u6765\u4f18\u5316\uff0c<strong>\u6d88\u8d39\u8005\u6ca1\u6709\u83b7\u53d6\u5230\u6d88\u606f\u65f6\uff0c\u4f1a\u81ea\u52a8\u963b\u585e\uff0c\u76f4\u5230\u751f\u4ea7\u8005\u653e\u5165\u65b0\u7684\u6d88\u606f\u3002<\/strong><\/p>\n<ul>\n<li><strong>\u5904\u7406\u91cd\u590d\u7684\u6d88\u606f<\/strong><\/li>\n<\/ul>\n<p>\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6d88\u606f\u751f\u6210\u4e00\u4e2a <strong>\u552f\u4e00 \u7684\u6d88\u606f ID<\/strong>\uff0c\u6bcf\u6b21\u6d88\u8d39\u6d88\u606f\u65f6\uff0c\u4ece\u6570\u636e\u5e93\u4e2d\u5224\u65ad\u8be5\u6d88\u606f\u662f\u5426\u5df2\u7ecf\u88ab\u5904\u7406\u4e86\uff0c\u9632\u6b62\u6d88\u606f\u7684\u91cd\u590d\u6d88\u606f\u3002<\/p>\n<ul>\n<li><strong>\u4fdd\u8bc1\u6d88\u606f\u53ef\u9760\u6027<\/strong><\/li>\n<\/ul>\n<p>\u5f53\u6d88\u606f\u88ab\u6d88\u8d39\u8005\u83b7\u53d6\u540e\uff0c\u6ca1\u6709\u5904\u7406\u5b8c\u6d88\u606f\uff0c\u673a\u5668\u5c31\u5b95\u673a\u4e86\uff0c\u8fd9\u6837\u4e0d\u4ec5\u6d88\u606f\u6ca1\u6709\u5b8c\u5168\u88ab\u6d88\u8d39\uff0cList \u4e2d\u7684\u6d88\u606f\u4e5f\u4e22\u5931\u4e86\uff0c\u4ece\u800c\u5bfc\u81f4\u8fd9\u4e2a\u4efb\u52a1\u6c38\u8fdc\u4e22\u5931\u4e86\uff1b<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Redis \u4e2d\u7684<code>BRPOPLPUSH<\/code>\u547d\u4ee4\uff0c\u4f5c\u7528\u662f\u628a\u8be5\u6d88\u606f\u4ece List \u4e2d\u62ff\u51fa\u6765\uff0c\u540c\u65f6\u8fd8\u4f1a\u628a\u8be5\u6d88\u606f\u653e\u5230\u53e6\u4e00\u4e2a List \u4e2d\u4f5c\u7559\u5b58\u3002<\/p>\n<h3>\u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>\u5728 Redis 3.2 \u4e4b\u524d\uff0cList \u7684\u5e95\u5c42\u5b9e\u73b0\u662f<strong>\u538b\u7f29\u5217\u8868 (<\/strong> <code>ZipList<\/code>) \u6216 <strong>\u53cc\u5411\u94fe\u8868<\/strong><\/p>\n<p>\u5728 Redis 3.2 \u4e4b\u540e\uff0cList \u7684\u5e95\u5c42\u5b9e\u73b0\u662f <code>QuickList<\/code><\/p>\n<h3>ZipList<\/h3>\n<p>ZipList \u53c8\u53eb\u538b\u7f29\u5217\u8868\uff0c\u7c7b\u4f3c &#8220;\u53cc\u5411\u94fe\u8868&#8221;\uff0c\u4f46\u5176\u4e0d\u662f\u94fe\u8868\u3002\u5b83\u7531\u4e00\u6bb5<strong>\u8fde\u7eed\u7684\u5185\u5b58\u5757<\/strong>\u7ec4\u6210\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u5934\u90e8\u6216\u8005\u5c3e\u90e8\u6dfb\u52a0\u6216\u8005\u5220\u9664\u5143\u7d20\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937632645ed9609859b.jpg1683937632645ed960985ac.jpg\" class=\"aligncenter\"><\/p>\n<p><code>zlbytes<\/code>\uff1a\u6574\u4e2a ZipList \u7684 \u5185\u5b58\u5b57\u8282\u6570\uff0c\u5360\u7528 4 \u5b57\u8282<\/p>\n<p><code>zltail<\/code>\uff1a\u4ece ZipList \u7684\u8d77\u59cb\u5730\u5740\u5230\u6700\u540e\u4e00\u4e2a entry \u7684\u5185\u5b58\u5b57\u8282\u6570\uff0c\u5360\u7528 4 \u5b57\u8282<\/p>\n<p><code>zllen<\/code>\uff1aZipList \u4e2d entry \u7684\u4e2a\u6570\uff0c\u5360\u7528 2 \u5b57\u8282<\/p>\n<p><code>zlend<\/code>\uff1a\u7ed3\u675f\u6807\u8bc6 <code>0xff<\/code>\uff0c\u5360\u7528 1 \u5b57\u8282<\/p>\n<p><strong>ZipEntry\u7ed3\u6784<\/strong><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937632645ed960c75bd.jpg1683937632645ed960c75d1.jpg\" class=\"aligncenter\"><\/p>\n<p><code>previous_entry_length<\/code>\uff1a\u4e0a\u4e00\u4e2a entry \u7684\u957f\u5ea6\uff0c\u5360\u7528 1\u4e2a \u6216 5\u4e2a\u5b57\u8282<\/p>\n<ul>\n<li>\u82e5\u4e0a\u4e00\u4e2a entry \u7684\u957f\u5ea6\u5c0f\u4e8e 256 \u5b57\u8282\uff0c\u5219 encoding \u4f7f\u7528 1 \u4e2a\u5b57\u8282\u6765\u5b58\u50a8<\/li>\n<li>\u82e5\u4e0a\u4e00\u4e2a entry \u7684\u957f\u5ea6\u5927\u4e8e 256 \u5b57\u8282\uff0c\u5219 encoding \u4f7f\u7528 5 \u4e2a\u5b57\u8282\u6765\u5b58\u50a8<\/li>\n<\/ul>\n<p><code>encoding<\/code>\uff1a\u8bb0\u5f55 content \u7684\u7c7b\u578b [ \u6574\u5f62 or \u5b57\u7b26\u4e32 ] \u548c\u957f\u5ea6\uff0c\u5360\u7528 1\u4e2a\uff0c2\u4e2a \u6216\u8005 5\u4e2a\u5b57\u8282<\/p>\n<p><code>content<\/code>\uff1a\u5b9e\u9645\u7684\u5185\u5bb9<\/p>\n<p><strong>Encoding \u7f16\u7801<\/strong><\/p>\n<p>Entry \u4e2d\u7684 encoding \u7f16\u7801\u5206\u4e3a\u5b57\u7b26\u4e32\u548c\u6574\u6570\u4e24\u79cd\uff1a<br \/><strong>\u5b57\u7b26\u4e32<\/strong>\uff1a\u5982\u679c encoding \u662f\u4ee5\u201c00\u201d\u201c01\u201d\u6216\u8005\u201c10\u201d\u5f00\u5934\uff0c\u5219\u8bc1\u660e content \u662f\u5b57\u7b26\u4e32<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937632645ed960ef4d5.jpg1683937632645ed960ef4e9.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u6574\u6570<\/strong>\uff1a\u5982\u679cencoding\u662f\u4ee5\u201c11\u201d\u5f00\u59cb\uff0c\u5219\u8bc1\u660econtent\u662f\u6574\u6570\uff0c\u4e14encoding\u56fa\u5b9a\u53ea\u5360\u75281\u4e2a\u5b57\u8282<\/p>\n<table>\n<tbody>\n<tr>\n<th><strong>\u7f16\u7801<\/strong><\/th>\n<th><strong>\u7f16\u7801\u957f\u5ea6<\/strong><\/th>\n<th><strong>\u6574\u6570\u7c7b\u578b<\/strong><\/th>\n<\/tr>\n<tr>\n<td>11000000<\/td>\n<td>1<\/td>\n<td>int16_t\uff082 bytes\uff09<\/td>\n<\/tr>\n<tr>\n<td>11010000<\/td>\n<td>1<\/td>\n<td>int32_t\uff084 bytes\uff09<\/td>\n<\/tr>\n<tr>\n<td>11100000<\/td>\n<td>1<\/td>\n<td>int64_t\uff088 bytes\uff09<\/td>\n<\/tr>\n<tr>\n<td>11110000<\/td>\n<td>1<\/td>\n<td>24\u4f4d\u6709\u7b26\u6574\u6570(3 bytes)<\/td>\n<\/tr>\n<tr>\n<td>11111110<\/td>\n<td>1<\/td>\n<td>8\u4f4d\u6709\u7b26\u6574\u6570(1 bytes)<\/td>\n<\/tr>\n<tr>\n<td>1111xxxx<\/td>\n<td>1<\/td>\n<td>\u76f4\u63a5\u5728xxxx\u4f4d\u7f6e\u4fdd\u5b58\u6570\u503c\uff0c\u8303\u56f4\u4ece0001~1101\uff0c\u51cf1\u540e\u7ed3\u679c\u4e3a\u5b9e\u9645\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u53ef\u89c1\uff0cZipList \u901a\u8fc7\u8bb0\u5f55\u4e0a\u4e00\u4e2aentry \u7684\u957f\u5ea6 \u548c \u81ea\u8eabentry \u7684\u957f\u5ea6\uff08length + encoding + content\uff09\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u5f97\u5230\u4e0a\u4e00\u4e2a entry \u548c\u4e0b\u4e00\u4e2a entry \uff0c\u4ece\u800c\u5b9e\u73b0\u4ece\u5934\u5230\u5c3e \u6216 \u4ece\u5c3e\u5230\u5934\u7684\u904d\u5386\u3002<\/strong><\/p>\n<h3>QuickList<\/h3>\n<p><strong>QuickList<\/strong> \u662f\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\uff0c\u5b83\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u662f <strong>ZipList<\/strong><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937633645ed96160d9b.jpg1683937633645ed96160dad.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4e3a\u4e86\u907f\u514d QuickList \u4e2d\u7684\u6bcf\u4e2a ZipList \u4e2d entry \u8fc7\u591a\uff0cRedis \u63d0\u4f9b\u4e86\u4e00\u4e2a\u914d\u7f6e\u9879\uff1a<code>list-max-ziplist-size<\/code>\u6765\u9650\u5236\u3002<\/p>\n<p>\u5982\u679c\u503c\u4e3a<strong>\u6b63<\/strong>\uff0c\u5219\u4ee3\u8868 ZipList \u7684\u5141\u8bb8\u7684 entry \u4e2a\u6570\u7684\u6700\u5927\u503c\uff1b<\/p>\n<p>\u5982\u679c\u503c\u4e3a<strong>\u8d1f<\/strong>\uff0c\u5219\u4ee3\u8868ZipList\u7684\u6700\u5927\u5185\u5b58\u5927\u5c0f\uff0c\u52065\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>-1\uff1a\u6bcf\u4e2a ZipList \u7684\u5185\u5b58\u5360\u7528\u4e0d\u80fd\u8d85\u8fc7 4kb<\/li>\n<li>-2\uff1a\u6bcf\u4e2a ZipList \u7684\u5185\u5b58\u5360\u7528\u4e0d\u80fd\u8d85\u8fc7 8kb<\/li>\n<li>-3\uff1a\u6bcf\u4e2a ZipList \u7684\u5185\u5b58\u5360\u7528\u4e0d\u80fd\u8d85\u8fc7 16kb<\/li>\n<li>-4\uff1a\u6bcf\u4e2a ZipList \u7684\u5185\u5b58\u5360\u7528\u4e0d\u80fd\u8d85\u8fc7 32kb<\/li>\n<li>-5\uff1a\u6bcf\u4e2a ZipList \u7684\u5185\u5b58\u5360\u7528\u4e0d\u80fd\u8d85\u8fc7 64kb<\/li>\n<\/ul>\n<p>\u5176\u9ed8\u8ba4\u503c\u4e3a -2\uff1a<\/p>\n<p>\u4ee5\u4e0b\u662f QuickList \u7684\u548c QuickListNode \u7684\u7ed3\u6784\u6e90\u7801\uff1a<\/p>\n<p>QuickList<\/p>\n<pre>typedef struct quicklist {\n    quicklistNode *head;\t\t\/\/ \u5934\u7ed3\u70b9\u6307\u9488\n    quicklistNode *tail;\t\t\/\/ \u5c3e\u7ed3\u70b9\u6307\u9488\n    unsigned long count;    \t\/* total count of all entries in all ziplists *\/\n    unsigned long len;          \/* number of quicklistNodes *\/\n    int fill : QL_FILL_BITS;              \/* fill factor for individual nodes *\/\n    unsigned int compress : QL_COMP_BITS; \/* depth of end nodes not to compress;0=off *\/\n    unsigned int bookmark_count: QL_BM_BITS;\n    quicklistBookmark bookmarks[];\n} quicklist;<\/pre>\n<p>QuickListNode<\/p>\n<pre>typedef struct quicklistNode {\n    struct quicklistNode *prev; \/\/ \u524d\u4e00\u4e2a\u6307\u9488\n    struct quicklistNode *next; \/\/ \u540e\u4e00\u4e2a\u6307\u9488\n    unsigned char *zl;  \t\t\/\/ \u5f53\u524d\u7ed3\u70b9\u7684ziplist\u6307\u9488\n    unsigned int sz;             \/* ziplist size in bytes *\/\n    unsigned int count : 16;     \/* count of items in ziplist *\/\n    unsigned int encoding : 2;   \/* RAW==1 or LZF==2 *\/\n    unsigned int container : 2;  \/* NONE==1 or ZIPLIST==2 *\/\n    unsigned int recompress : 1; \/* was this node previous compressed? *\/\n    unsigned int attempted_compress : 1; \/* node can't compress; too small *\/\n    unsigned int extra : 10; \/* more bits to steal for future usage *\/\n} quicklistNode;<\/pre>\n<p>\u6574\u4f53\u7ed3\u6784<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937633645ed961b29ea.jpg1683937633645ed961b29f9.jpg\" class=\"aligncenter\"><\/p>\n<h2>Hash<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>Hash \u662f\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u5f62\u5982\uff1a<code>key\uff1a{field1: value1\uff0cfield1: value1}<\/code>\uff0c\u975e\u5e38\u9002\u5408\u5b58\u50a8\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937633645ed961db6f5.jpg1683937633645ed961db704.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<pre># \u5b58\u50a8\u4e00\u4e2a\u54c8\u5e0c\u8868key\u7684\u952e\u503c\nHSET key field value   \n# \u83b7\u53d6\u54c8\u5e0c\u8868key\u5bf9\u5e94\u7684field\u952e\u503c\nHGET key field\n\n# \u5728\u4e00\u4e2a\u54c8\u5e0c\u8868key\u4e2d\u5b58\u50a8\u591a\u4e2a\u952e\u503c\u5bf9\nHMSET key field value [field value...] \n# \u6279\u91cf\u83b7\u53d6\u54c8\u5e0c\u8868key\u4e2d\u591a\u4e2afield\u952e\u503c\nHMGET key field [field ...]       \n# \u5220\u9664\u54c8\u5e0c\u8868key\u4e2d\u7684field\u952e\u503c\nHDEL key field [field ...]    \n\n# \u8fd4\u56de\u54c8\u5e0c\u8868key\u4e2dfield\u7684\u6570\u91cf\nHLEN key       \n# \u8fd4\u56de\u54c8\u5e0c\u8868key\u4e2d\u6240\u6709\u7684\u952e\u503c\nHGETALL key \n\n# \u4e3a\u54c8\u5e0c\u8868key\u4e2dfield\u952e\u7684\u503c\u52a0\u4e0a\u589e\u91cfn\nHINCRBY key field n<\/pre>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<p><strong>\u7f13\u5b58\u5bf9\u8c61<\/strong><\/p>\n<p>\u4f8b\u5982\u4e0b\u56fe\uff0c\u5bf9\u4e8e\u4f7f\u7528&nbsp;<code>string + json<\/code>&nbsp;\u8fd8\u662f\u7528&nbsp;<code>hash<\/code>&nbsp;\u6765\u5b58\u50a8\uff0c\u5efa\u8bae\u4f18\u5148\u9009\u62e9&nbsp;<code>string + json<\/code>&nbsp;\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u5b57\u6bb5\u7ecf\u5e38\u6539\u53d8\u7684\u8bdd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528&nbsp;<code>hash<\/code><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937634645ed96223ad6.jpg1683937634645ed96223ae4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u8d2d\u7269\u8f66<\/strong><\/p>\n<p>\u4ee5\u7528\u6237 id \u4e3a key\uff0c\u5546\u54c1 id \u4e3a field\uff0c\u5546\u54c1\u6570\u91cf\u4e3a value\uff0c\u6070\u597d\u6784\u6210\u4e86\u8d2d\u7269\u8f66\u7684 3 \u4e2a\u8981\u7d20\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937634645ed9624e985.jpg1683937634645ed9624e996.jpg\" class=\"aligncenter\"><\/p>\n<p>\u6d89\u53ca\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>\u6dfb\u52a0\u5546\u54c1<\/strong>\uff1aHSET cart:{\u7528\u6237id} {\u5546\u54c1id} 1<\/li>\n<li><strong>\u6dfb\u52a0\u6570\u91cf<\/strong>\uff1aHINCRBY cart:{\u7528\u6237id} {\u5546\u54c1id} 1<\/li>\n<li><strong>\u5546\u54c1\u603b\u6570<\/strong>\uff1aHLEN cart:{\u7528\u6237id}<\/li>\n<li><strong>\u5220\u9664\u5546\u54c1<\/strong>\uff1aHDEL cart:{\u7528\u6237id} {\u5546\u54c1id}<\/li>\n<li><strong>\u83b7\u53d6\u8d2d\u7269\u8f66\u6240\u6709\u5546\u54c1<\/strong>\uff1aHGETALL cart:{\u7528\u6237id}<\/li>\n<\/ul>\n<p>\u5f53\u524d\u4ec5\u4ec5\u662f\u5c06\u5546\u54c1 ID \u5b58\u50a8\u5230\u4e86 Redis \u4e2d\uff0c\u5728\u56de\u663e\u5546\u54c1\u5177\u4f53\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u8fd8\u9700\u8981\u62ff\u7740\u5546\u54c1 id \u67e5\u8be2\u4e00\u6b21\u6570\u636e\u5e93\uff0c\u83b7\u53d6\u5b8c\u6574\u7684\u5546\u54c1\u7684\u4fe1\u606f\u3002<\/p>\n<h3>\u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>Hash \u7684\u5e95\u5c42\u5b9e\u73b0\u53ef\u4ee5\u662f <strong>ZipList<\/strong> \u6216\u8005 <strong>Dict<\/strong><\/p>\n<ul>\n<li>Hash\u7ed3\u6784\u9ed8\u8ba4\u91c7\u7528 <code>ZipList<\/code> \u7f16\u7801\uff0c\u7528\u4ee5\u8282\u7701\u5185\u5b58\u3002 <code>ZipList<\/code> \u4e2d\u76f8\u90bb\u7684\u4e24\u4e2a <code>entry<\/code> \u5206\u522b\u4fdd\u5b58 <code>field<\/code> \u548c <code>value<\/code><\/li>\n<li>\u5f53 ZipList \u7684\u5143\u7d20\u4e2a\u6570\u5c0f\u4e8e 512\uff08\u9ed8\u8ba4\uff09\u4e14\u6bcf\u4e2a Entry \u7684\u5927\u5c0f\u5c0f\u4e8e 64 \u5b57\u8282\uff08\u9ed8\u8ba4\uff09\uff0c\u5219\u4f1a\u4f7f\u7528 ZipList \u6765\u4f5c\u4e3a Hash \u7684\u5e95\u5c42\u5b9e\u73b0\uff0c\u5426\u5219\u5219\u4f7f\u7528 Dict<\/li>\n<\/ul>\n<h3>Dict<\/h3>\n<p>Dict \u4e2d\u7ef4\u62a4\u4e00\u4e2a \u54c8\u5e0c\u8868 <code>DitHashTable<\/code>\uff0c\u91cc\u9762\u6709\u4e00\u4e2a DictEntry \u6570\u7ec4\uff0c\u7528\u6765\u5b58\u50a8\u5b9e\u9645\u7684 <code>key-value<\/code>\uff0c\u6bcf\u4e2a\u69fd\u4f4d\u4e0b\u7684 DictEntry \u90fd\u4f1a\u5f62\u6210\u4e00\u4e2a\u94fe\u8868\uff0c\u5904\u7406\u54c8\u5e0c\u51b2\u7a81<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937634645ed9628d85b.jpg1683937634645ed9628d86d.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5411 Dict \u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u65f6\uff0c\u5148\u4f1a\u6839\u636e key \u8ba1\u7b97\u51fa\u4e00\u4e2a\u54c8\u5e0c\u503c h\uff0c\u7136\u540e\u8ba1\u7b97 h &amp; sizemask \u5f97\u5230\u6570\u7ec4\u4e0b\u6807\uff0c\u6700\u540e\u52a0\u5165\u8be5 DictEntry \u5373\u53ef<\/p>\n<p><strong>\u6570\u636e\u7ed3\u6784\u4f53<\/strong><\/p>\n<p><strong>\u5b57\u5178\uff08Dict\uff09<\/strong><\/p>\n<pre>typedef struct dict {\n    dictType *type; \t\t\/\/ dict \u7c7b\u578b\uff0c\u4e0d\u540c\u7684\u54c8\u5e0c\u51fd\u6570\n    void *privdata;  \t\t\/\/ \u79c1\u6709\u6570\u636e\uff0c\u505a\u7279\u6b8ahash\u7684\u65f6\u5019\u7528\n    dictht ht[2]; \t\t\t\/\/ \u5305\u542b\u4e24\u4e2ahash\u8868\uff0c\u4e00\u4e2a\u662f\u5f53\u524d\u6570\u636e\uff0c\u53e6\u4e2a\u4e3a\u7a7a\uff0crehash\u7684\u65f6\u5019\u7528\n    long rehashidx; \t\t\/\/ rehash\u7684\u8fdb\u5ea6\n    int16_t pauserehash;\t\/\/ rehash\u662f\u5426\u6682\u505c \n} dict;<\/pre>\n<p><strong>\u54c8\u5e0c\u8868\uff08DictHashTable\uff09<\/strong><\/p>\n<pre>typedef struct dictht {\n    dictEntry **table;   \t\t\/\/ \u6307\u5411entry \u7684\u6570\u7ec4\n    unsigned long size;  \t\t\/\/ \u54c8\u5e0c\u8868\u5927\u5c0f\n    unsigned long sizemask;   \t\/\/ \u63a9\u7801\uff1asize - 1\n    unsigned long used;  \t\t\/\/ entry \u7684\u4e2a\u6570\n} dictht;<\/pre>\n<p><strong>\u54c8\u5e0c\u8282\u70b9\uff08DictEntry\uff09<\/strong><\/p>\n<pre>typedef struct dictEntry {\n    void *key;  \/\/ \u952e\n    union {\n        void *val;\n        uint64_t u64;\n        int64_t s64;\n        double d;\n    } v;\/\/ \u503c\n    struct dictEntry *next;\/\/ \u4e0b\u4e00\u4e2a\u7ed3\u70b9\n} dictEntry;<\/pre>\n<h3>Dict \u7684 rehash<\/h3>\n<p>\u4e0d\u7ba1\u662f\u6269\u5bb9\u8fd8\u662f\u6536\u7f29\uff0c\u5fc5\u5b9a\u4f1a\u521b\u5efa\u65b0\u7684\u54c8\u5e0c\u8868\uff0c\u5bfc\u81f4\u54c8\u5e0c\u8868\u7684 size \u548c sizemask \u53d8\u5316\uff0c\u800c key \u7684\u67e5\u8be2\u4e0e sizemask \u6709\u5173\u3002\u56e0\u6b64\u5fc5\u987b\u5bf9\u54c8\u5e0c\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a key \u91cd\u65b0\u8ba1\u7b97\u7d22\u5f15\uff0c\u63d2\u5165\u65b0\u7684\u54c8\u5e0c\u8868\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a rehash\u3002\u8fc7\u7a0b\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<ul>\n<li>\u8ba1\u7b97\u65b0hash\u8868\u7684realeSize\uff0c\u503c\u53d6\u51b3\u4e8e\u5f53\u524d\u8981\u505a\u7684\u662f\u6269\u5bb9\u8fd8\u662f\u6536\u7f29\uff1a<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u5982\u679c\u662f\u6269\u5bb9\uff0c\u5219\u65b0size\u4e3a<strong>\u7b2c\u4e00\u4e2a<\/strong>\u5927\u4e8e\u7b49\u4e8e<code>dict.ht[0].used + 1<\/code> \u76842^n<\/li>\n<li>\u5982\u679c\u662f\u6536\u7f29\uff0c\u5219\u65b0size\u4e3a<strong>\u7b2c\u4e00\u4e2a<\/strong>\u5927\u4e8e\u7b49\u4e8e<code>dict.ht[0].used<\/code> \u76842^n \uff08\u4e0d\u5f97\u5c0f\u4e8e4\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\u6309\u7167\u65b0\u7684 realeSize \u7533\u8bf7\u5185\u5b58\u7a7a\u95f4\uff0c\u521b\u5efa dictht\uff0c\u5e76\u8d4b\u503c\u7ed9 dict.ht[1]<\/li>\n<li>\u8bbe\u7f6e dict.rehashidx = 0\uff0c\u6807\u793a\u5f00\u59cb rehash<\/li>\n<li>\u5c06 dict.ht[0] \u4e2d\u7684\u6bcf\u4e00\u4e2a dictEntry \u90fd rehash \u5230 dict.ht[1]<\/li>\n<li>\u5c06 dict.ht[1] \u8d4b\u503c\u7ed9 dict.ht[0]\uff0c\u7ed9 dict.ht[1] \u521d\u59cb\u5316\u4e3a\u7a7a\u54c8\u5e0c\u8868\uff0c\u91ca\u653e\u539f\u6765\u7684dict.ht[0]\u7684\u5185\u5b58<\/li>\n<li>\u5c06 rehashidx \u8d4b\u503c\u4e3a -1\uff0c\u4ee3\u8868 rehash \u7ed3\u675f<\/li>\n<li>\u5728rehash\u8fc7\u7a0b\u4e2d\uff0c<strong>\u65b0\u589e\u64cd\u4f5c\uff0c\u5219\u76f4\u63a5\u5199\u5165ht[1]<\/strong> \uff0c<strong>\u67e5\u8be2\u3001\u4fee\u6539\u548c\u5220\u9664\u5219\u4f1a\u5728dict.ht[0]\u548cdict.ht[1]\u4f9d\u6b21\u67e5\u627e\u5e76\u6267\u884c<\/strong>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd ht[0] \u7684\u6570\u636e\u53ea\u51cf\u4e0d\u589e\uff0c\u968f\u7740 rehash \u6700\u7ec8\u4e3a\u7a7a<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937634645ed962e1887.jpg1683937634645ed962e189c.jpg\" class=\"aligncenter\"><\/p>\n<h2>Set<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>Set \u662f Redis \u4e2d\u7684\u65e0\u5e8f\u96c6\u5408\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u5143\u7d20\u7684\u552f\u4e00\u6027\uff0c\u4f46\u4e0d\u4e00\u5b9a\u4fdd\u8bc1\u5143\u7d20\u7684\u6709\u5e8f\u6027\uff0c\u53ef\u4ee5\u5bf9 set \u53d6\u4ea4\u96c6\uff0c\u5e76\u96c6\u7b49\u64cd\u4f5c<\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<p>Set \u5e38\u7528\u64cd\u4f5c<\/p>\n<pre># \u5f80\u96c6\u5408key\u4e2d\u5b58\u5165\u5143\u7d20\uff0c\u5143\u7d20\u5b58\u5728\u5219\u5ffd\u7565\uff0c\u82e5key\u4e0d\u5b58\u5728\u5219\u65b0\u5efa\nSADD key member [member ...]\n# \u4ece\u96c6\u5408key\u4e2d\u5220\u9664\u5143\u7d20\nSREM key member [member ...] \n# \u83b7\u53d6\u96c6\u5408key\u4e2d\u6240\u6709\u5143\u7d20\nSMEMBERS key\n# \u83b7\u53d6\u96c6\u5408key\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\nSCARD key\n\n# \u5224\u65admember\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u96c6\u5408key\u4e2d\nSISMEMBER key member\n\n# \u4ece\u96c6\u5408key\u4e2d\u968f\u673a\u9009\u51facount\u4e2a\u5143\u7d20\uff0c\u5143\u7d20\u4e0d\u4ecekey\u4e2d\u5220\u9664\nSRANDMEMBER key [count]\n# \u4ece\u96c6\u5408key\u4e2d\u968f\u673a\u9009\u51facount\u4e2a\u5143\u7d20\uff0c\u5143\u7d20\u4ecekey\u4e2d\u5220\u9664\nSPOP key [count]<\/pre>\n<p>Set \u96c6\u5408\u64cd\u4f5c<\/p>\n<pre># \u4ea4\u96c6\u8fd0\u7b97\nSINTER key [key ...]\n# \u5c06\u4ea4\u96c6\u7ed3\u679c\u5b58\u5165\u65b0\u96c6\u5408destination\u4e2d\nSINTERSTORE destination key [key ...]\n\n# \u5e76\u96c6\u8fd0\u7b97\nSUNION key [key ...]\n# \u5c06\u5e76\u96c6\u7ed3\u679c\u5b58\u5165\u65b0\u96c6\u5408destination\u4e2d\nSUNIONSTORE destination key [key ...]\n\n# \u5dee\u96c6\u8fd0\u7b97\nSDIFF key [key ...]\n# \u5c06\u5dee\u96c6\u7ed3\u679c\u5b58\u5165\u65b0\u96c6\u5408destination\u4e2d\nSDIFFSTORE destination key [key ...]<\/pre>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<p><strong>\u70b9\u8d5e<\/strong><\/p>\n<p>\u4f7f\u7528 set \u96c6\u5408\u6765\u5b58\u50a8\uff0ckey \u4e3a\u6587\u7ae0 id\uff0cvalue\u4e3a \u7528\u6237 id \u5373\u53ef\uff0c\u83b7\u53d6\u8be5\u6587\u7ae0\u7684\u70b9\u8d5e\u6570\u91cf\u53d6\u8be5 set \u4e2d id \u7684\u603b\u6570\u5373\u53ef<\/p>\n<p><strong>\u5173\u6ce8<\/strong><\/p>\n<p>\u4f7f\u7528 set \u96c6\u5408\u6765\u5b58\u50a8\uff0ckey \u4e3a\u7528\u6237 id\uff0cvalue \u4e3a\u8be5\u7528\u6237\u5173\u6ce8\u7684\u7528\u6237\u7684 id\uff0c\u53ef\u4ee5\u5bf9\u4e24\u4e2a set \u53d6\u5e76\u96c6\uff0c\u5c31\u53ef\u4ee5\u627e\u51fa\u5b83\u4eec\u7684\u5171\u540c\u5173\u6ce8<\/p>\n<p><strong>\u62bd\u5956<\/strong><\/p>\n<p>\u4f7f\u7528 set \u96c6\u5408\u6765\u5b58\u50a8\uff0ckey \u4e3a\u6d3b\u52a8 id\uff0cvalue \u4e3a\u53c2\u89c1\u8be5\u6d3b\u52a8\u7684\u7528\u6237<\/p>\n<ul>\n<li>\u82e5\u4e0d\u53ef\u91cd\u590d\u62bd\u5956\uff0c\u5219\u4f7f\u7528 <code>SPOP key [count]<\/code> \u6765\u968f\u673a\u83b7\u53d6 <code>count<\/code> \u4e2a\u7528\u6237\uff0c\u5e76\u4ece set \u4e2d\u79fb\u9664<\/li>\n<li>\u82e5\u53ef\u4ee5\u91cd\u590d\u62bd\u5956\uff0c\u5219\u4f7f\u7528 <code>SRANDMEMBER key [count]<\/code> \u6765\u968f\u673a\u83b7\u53d6 count \u4e2a\u7528\u6237\uff0c\u4f46\u4e0d\u4f1a\u4ece set \u4e2d\u79fb\u9664<\/li>\n<\/ul>\n<h3>\u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p><strong>Set \u5e95\u5c42\u662f\u901a\u8fc7 IntSet\uff08\u6574\u6570\u96c6\u5408\uff09\u6216\u8005 Dict \u6765\u5b9e\u73b0\u7684<\/strong><\/p>\n<ul>\n<li>\u7528 Dict \u6765\u5b58\u50a8\u5b9e\u9645\u4e0a\u548c Hash \u7684\u7ed3\u6784\u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u628a value \u503c\u7edf\u4e00\u5b58\u50a8\u4e3a null \u5373\u53ef<\/li>\n<li>\u5f53\u6570\u636e\u90fd\u662f\u6574\u6570\u65f6\uff0c\u4e14\u6570\u636e\u7684\u6570\u91cf\u4e0d\u8d85\u8fc7 <code>set-max-intset-entries<\/code> \u65f6\uff0c\u4f1a\u4f7f\u7528 <code>intset<\/code> \u6765\u5b58\u50a8\u6570\u636e<\/li>\n<li>\u5426\u5219\u5219\u4f7f\u7528 <code>Dict<\/code> \u6765\u5b58\u50a8<\/li>\n<\/ul>\n<h3>Intset<\/h3>\n<p>Intset \u662f Redis \u4e2d\u7684\u4e00\u79cd\u5b9e\u73b0\u6a21\u5f0f\uff0c\u57fa\u4e8e<strong>\u6574\u6570\u6570\u7ec4<\/strong>\u6765\u5b9e\u73b0\uff0c\u5177\u5907<strong>\u957f\u5ea6\u53ef\u53d8<\/strong>\uff0c<strong>\u6709\u5e8f<\/strong>\u7684\u7279\u5f81<\/p>\n<p><strong>\u7ed3\u6784\u4f53<\/strong><\/p>\n<pre>typedef struct intset {\n    uint32_t encoding; \t\/\/ \u7f16\u7801\u65b9\u5f0f\n    uint32_t length; \t\/\/ \u957f\u5ea6\n    int8_t contents[]; \t\/\/ \u5b58\u50a8\u4f4d\u7f6e\n} intset;<\/pre>\n<p><code>encoding<\/code>\uff1a\u8868\u793a\u5b58\u50a8\u6574\u6570\u7684\u5927\u5c0f<\/p>\n<pre>\/* Note that these encodings are ordered, so:\n * INTSET_ENC_INT16 \n<p><code>length<\/code>\uff1a\u6570\u7ec4\u7684\u957f\u5ea6<\/p>\n<p><code>contents<\/code>\uff1a\u5b58\u50a8\u5b9e\u9645\u7684\u5185\u5bb9<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937635645ed963332b8.jpg1683937635645ed963332cb.jpg\" class=\"aligncenter\"><\/p>\n<p><code>encoding<\/code>\uff1a4\u5b57\u8282<\/p>\n<p><code>length<\/code>\uff1a4\u5b57\u8282<\/p>\n<p><code>contents<\/code>\uff1a2\u5b57\u8282 * 3 = 6 \u5b57\u8282<\/p>\n<p><strong>\u6269\u5bb9\u8fc7\u7a0b<\/strong><\/p>\n<p>\u6211\u4eec\u5411\u8be5\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6570\u5b57\uff1a50000\uff0c\u8fd9\u4e2a\u6570\u5b57\u8d85\u51fa\u4e86 int16_t \u7684\u8303\u56f4\uff0cintset \u4f1a\u81ea\u52a8\u5347\u7ea7\u7f16\u7801\u65b9\u5f0f\u5230\u5408\u9002\u7684\u5927\u5c0f\u3002<br>\u4ee5\u5f53\u524d\u6848\u4f8b\u6765\u8bf4\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<\/pre>\n<ul>\n<li>\u5347\u7ea7\u7f16\u7801\u4e3a <code>INTSET_ENC_INT32<\/code> , \u6bcf\u4e2a\u6574\u6570\u5360 4 \u5b57\u8282\uff0c\u5e76\u6309\u7167\u65b0\u7684\u7f16\u7801\u65b9\u5f0f\u53ca\u5143\u7d20\u4e2a\u6570\u6269\u5bb9\u6570\u7ec4<\/li>\n<li>\u5012\u5e8f\u4f9d\u6b21\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u62f7\u8d1d\u5230\u6269\u5bb9\u540e\u7684\u6b63\u786e\u4f4d\u7f6e<\/li>\n<li>\u5c06\u5f85\u6dfb\u52a0\u7684\u5143\u7d20\u653e\u5165\u6570\u7ec4\u672b\u5c3e<\/li>\n<li>\u6700\u540e\uff0c\u5c06 inset \u7684 encoding \u5c5e\u6027\u6539\u4e3a <code>INTSET_ENC_INT32<\/code>\uff0c\u5c06 <code>length<\/code> \u5c5e\u6027\u6539\u4e3a 4<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937635645ed9637e62a.jpg1683937635645ed9637e63f.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5c0f\u7ed3\uff1a<\/p>\n<ul>\n<li>intset \u53ef\u4ee5\u4fdd\u8bc1 contents \u7684\u6574\u6570\u6709\u5e8f\uff0c\u552f\u4e00<\/li>\n<li>\u56e0\u4e3a contents \u6570\u7ec4\u6709\u5e8f\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 <strong>\u4e8c\u5206\u67e5\u627e<\/strong> \u6765\u5feb\u901f\u627e\u5230\u76ee\u6807\u503c<\/li>\n<li>\u5177\u5907\u7c7b\u578b\u5347\u7ea7\u673a\u5236\uff0c\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u7a7a\u95f4<\/li>\n<\/ul>\n<h2>ZSet<\/h2>\n<h3>\u4ecb\u7ecd<\/h3>\n<p>ZSet \u662f\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\u7c7b\u578b\uff0c\u6bd4 Set \u591a\u51fa\u4e86\u4e00\u4e2a score \u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u7528\u6765\u5bf9 value \u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u4fdd\u8bc1 ZSet \u4e2d\u7684\u503c\u662f\u6709\u5e8f\u7684<\/p>\n<p>\u6240\u4ee5\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u6709\u4e24\u4e2a\u503c\uff0c\u4e00\u4e2a score\uff0c\u7528\u6765\u6392\u5e8f\uff0c\u4e00\u4e2a value \u5b58\u50a8\u5b9e\u9645\u7684\u503c<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937635645ed9639e410.jpg1683937635645ed9639e422.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<p>ZSet \u5e38\u7528\u64cd\u4f5c<\/p>\n<pre># \u5f80\u6709\u5e8f\u96c6\u5408key\u4e2d\u52a0\u5165\u5e26\u5206\u503c\u5143\u7d20\nZADD key score member [[score member]...]   \n# \u5f80\u6709\u5e8f\u96c6\u5408key\u4e2d\u5220\u9664\u5143\u7d20\nZREM key member [member...]                 \n# \u8fd4\u56de\u6709\u5e8f\u96c6\u5408key\u4e2d\u5143\u7d20member\u7684\u5206\u503c\nZSCORE key member\n# \u8fd4\u56de\u6709\u5e8f\u96c6\u5408key\u4e2d\u5143\u7d20\u4e2a\u6570\nZCARD key \n\n# \u4e3a\u6709\u5e8f\u96c6\u5408key\u4e2d\u5143\u7d20member\u7684\u5206\u503c\u52a0\u4e0aincrement\nZINCRBY key increment member \n\n# \u6b63\u5e8f\u83b7\u53d6\u6709\u5e8f\u96c6\u5408key\u4ecestart\u4e0b\u6807\u5230stop\u4e0b\u6807\u7684\u5143\u7d20\nZRANGE key start stop [WITHSCORES]\n# \u5012\u5e8f\u83b7\u53d6\u6709\u5e8f\u96c6\u5408key\u4ecestart\u4e0b\u6807\u5230stop\u4e0b\u6807\u7684\u5143\u7d20\nZREVRANGE key start stop [WITHSCORES]\n\n# \u8fd4\u56de\u6709\u5e8f\u96c6\u5408\u4e2d\u6307\u5b9a\u5206\u6570\u533a\u95f4\u5185\u7684\u6210\u5458\uff0c\u5206\u6570\u7531\u4f4e\u5230\u9ad8\u6392\u5e8f\u3002\nZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]\n\n# \u8fd4\u56de\u6307\u5b9a\u6210\u5458\u533a\u95f4\u5185\u7684\u6210\u5458\uff0c\u6309\u5b57\u5178\u6b63\u5e8f\u6392\u5217, \u5206\u6570\u5fc5\u987b\u76f8\u540c\u3002\nZRANGEBYLEX key min max [LIMIT offset count]\n# \u8fd4\u56de\u6307\u5b9a\u6210\u5458\u533a\u95f4\u5185\u7684\u6210\u5458\uff0c\u6309\u5b57\u5178\u5012\u5e8f\u6392\u5217, \u5206\u6570\u5fc5\u987b\u76f8\u540c\nZREVRANGEBYLEX key max min [LIMIT offset count]<\/pre>\n<p>ZSet \u8fd0\u7b97\u64cd\u4f5c<\/p>\n<pre># \u5e76\u96c6\u8ba1\u7b97(\u76f8\u540c\u5143\u7d20\u5206\u503c\u76f8\u52a0)\uff0cnumberkeys\u4e00\u5171\u591a\u5c11\u4e2akey\uff0cWEIGHTS\u6bcf\u4e2akey\u5bf9\u5e94\u7684\u5206\u503c\u4e58\u79ef\nZUNIONSTORE destkey numberkeys key [key...] \n# \u4ea4\u96c6\u8ba1\u7b97(\u76f8\u540c\u5143\u7d20\u5206\u503c\u76f8\u52a0)\uff0cnumberkeys\u4e00\u5171\u591a\u5c11\u4e2akey\uff0cWEIGHTS\u6bcf\u4e2akey\u5bf9\u5e94\u7684\u5206\u503c\u4e58\u79ef\nZINTERSTORE destkey numberkeys key [key...]<\/pre>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<p><strong>\u6392\u884c\u699c<\/strong><\/p>\n<p>\u4ee5\u6587\u7ae0\u6392\u884c\u699c\u4e3a\u4f8b\u5b50\uff0c\u4f7f\u7528 article:rank \u4f5c\u4e3a key\uff0c\u70b9\u8d5e\u6570\u91cf\u4f5c\u4e3a score\uff0c\u6587\u7ae0\u7684 id \u4f5c\u4e3a value \u5373\u53ef\uff0c\u6839\u636e score \u4ece\u5927\u5230\u5c0f\u8fdb\u884c\u6392\u5e8f\uff0c\u5219\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a \u6587\u7ae0\u70ed\u5ea6\u7684\u6392\u884c\u699c\u3002<\/p>\n<h3>\u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>ZSet \u4f7f\u7528 <strong>\u538b\u7f29\u5217\u8868<\/strong> \u6216\u8005 <strong>\u8df3\u8868 + \u5b57\u5178<\/strong>\u6765\u5b9e\u73b0<\/p>\n<ul>\n<li>\u53ef\u4ee5\u6839\u636e key \u627e\u5230 value\uff1b<\/li>\n<li>\u4fdd\u8bc1 value \u662f\u552f\u4e00\u7684\uff1b<\/li>\n<li>\u53ef\u4ee5\u6839\u636e value \u67e5\u627e\u5206\u6570\uff1b<\/li>\n<\/ul>\n<p>Ziplist \u672c\u8eab\u662f\u6ca1\u6709\u6392\u5e8f\u529f\u80fd\u7684\uff0c\u800c\u4e14\u6ca1\u6709\u952e\u503c\u5bf9\u7684\u6982\u5ff5\uff0c\u9700\u8981\u901a\u8fc7\u7f16\u7801\u6765\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li><strong>ziplist<\/strong> \u662f<strong>\u8fde\u7eed\u5185\u5b58<\/strong>\uff0c<strong>score<\/strong> \u548c <strong>value<\/strong> \u662f\u76f8\u4e92\u4f9d\u9760\u7684\u4e24\u4e2a <strong>entry<\/strong>\uff1b<\/li>\n<li><strong>ziplist<\/strong> \u672c\u8eab\u4e0d\u80fd\u6392\u5e8f\uff0c\u6240\u4ee5\u53ea\u80fd\u901a\u8fc7 <strong>score<\/strong> \u6765\u5bf9 <strong>entry<\/strong> <strong>\u4ece\u5c0f\u5230\u5927\u6392\u5e8f<\/strong>\uff1b<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937635645ed963ebe03.jpg1683937635645ed963ebe14.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5f53<strong>\u540c\u65f6<\/strong>\u6ee1\u8db3\u4e00\u4e0b\u6761\u4ef6\u65f6\uff0c\u4f1a\u9009\u62e9\u4f7f\u7528 <strong>Ziplist<\/strong><\/p>\n<ul>\n<li>\u5143\u7d20\u7684\u6570\u91cf\u5c0f\u4e8e <code>zset_max_ziplist_entries<\/code> \uff0c\u9ed8\u8ba4\u662f 256<\/li>\n<li>\u6bcf\u4e2a\u5143\u7d20\u7684\u5185\u5b58\u5927\u5c0f\u5c0f\u4e8e <code>zset_max_ziplist_value<\/code> \u5b57\u8282\uff0c\u9ed8\u8ba4\u662f 64 KB<\/li>\n<\/ul>\n<p>\u5426\u5219\u4f7f\u7528 <strong>\u8df3\u8868 +Dict<\/strong><\/p>\n<p>\u4f7f\u7528 Dict \u5b58\u50a8 <strong>value<\/strong> \u548c <strong>score<\/strong> \u7684\u6620\u5c04\u5173\u7cfb\uff1b<\/p>\n<p>\u4f7f\u7528 \u8df3\u8868 \u6765<strong>\u6709\u5e8f<\/strong>\u7684\u5b58\u50a8 score \u548c value \u503c\uff0c\u4fbf\u4e8e\u8303\u56f4\u67e5\u627e<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937636645ed9643948d.jpg1683937636645ed9643949f.jpg\" class=\"aligncenter\"><\/p>\n<h3>SkipList<\/h3>\n<p>\u8df3\u8868\u662f\u4e00\u4e2a\u5e76\u8054\u591a\u4e2a\u6709\u5e8f\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u53ef\u4ee5\u5728 O(logn) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5185\u5b8c\u6210\u63d2\u5165\u3001\u5220\u9664\u3001\u589e\u52a0\u7684\u64cd\u4f5c\uff1b<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230513\/1683937636645ed96478824.jpg1683937636645ed96478833.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7279\u70b9<\/strong><\/p>\n<ul>\n<li>\u8df3\u8868\u662f\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b <strong>score<\/strong> \u548c <strong>element<\/strong> \u503c<\/li>\n<li>\u8282\u70b9\u6309 <strong>score<\/strong> \u8fdb\u884c\u6392\u5e8f<\/li>\n<li>\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u5305\u542b\u591a\u5c42\u6307\u9488\uff0c\u5c42\u6570\u662f 1 \u5230 32 \u4e4b\u95f4\u7684\u968f\u673a\u6570<\/li>\n<li>\u4e0d\u540c\u5c42\u6307\u9488\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u8de8\u5ea6\u4e0d\u540c\uff0c\u5c42\u7ea7\u8d8a\u9ad8\uff0c\u8de8\u5ea6\u8d8a\u5927<\/li>\n<li>\u589e\u5220\u6539\u67e5\u6548\u7387\u4e0e<strong>\u7ea2\u9ed1\u6811<\/strong>\u57fa\u672c\u4e00\u81f4\uff0c\u5b9e\u73b0\u5374\u66f4\u7b80\u5355<\/li>\n<\/ul>\n<p><strong>\u8df3\u8868\u4ee3\u7801<\/strong><\/p>\n<pre>public class SkipList {\n\n    private final int MAX_LEVEL = 32;\n\n    private final double factor = 0.25;\n\n    private int level = 1;\n\n    \/**\n     * \u5934\u7ed3\u70b9\n     *\/\n    private final Node head = new Node(null, MAX_LEVEL);\n\n    static class Node {\n        Integer value;\n        Node[] next;\n        int size;\n\n        public Node(Integer value, int size) {\n            this.value = value;\n            this.size = size;\n            this.next = new Node[size];\n        }\n    }\n\n    public boolean search(int value) {\n\n        Node p = head;\n        for (int i = level - 1; i &gt;= 0; i--) {\n            while (p.next[i] != null &amp;&amp; p.next[i].value = 0; i--) {\n            while (p.next[i] != null &amp;&amp; p.next[i].value  level) {\n            for (int i = level ; i = 0; i--) {\n                while (p.next[i] != null &amp;&amp; p.next[i].value \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e94\u79cd\u6570\u636e\u7c7b\u578b\u8be6\u89e3 \u6536\u85cf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201818","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201818"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201818\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}