{"id":201641,"date":"2025-05-10T11:10:50","date_gmt":"2025-05-10T03:10:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201641\/"},"modified":"2025-05-10T11:10:50","modified_gmt":"2025-05-10T03:10:50","slug":"redis%e5%af%b9%e8%b1%a1%e4%b8%8eredisobject%e8%b6%85%e8%af%a6%e7%bb%86%e5%88%86%e6%9e%90%e6%ba%90%e7%a0%81%e5%b1%82","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201641\/","title":{"rendered":"Redis\u5bf9\u8c61\u4e0eredisObject\u8d85\u8be6\u7ec6\u5206\u6790\u6e90\u7801\u5c42"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5bf9\u8c61\u4e0eredisObject\u8d85\u8be6\u7ec6\u5206\u6790\u6e90\u7801\u5c42<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aRedis\u5bf9\u8c61\u4e0eredisObject\u8d85\u8be6\u7ec6\u5206\u6790\u6e90\u7801\u5c42\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>\u4ee5\u4e0b\u5185\u5bb9\u662f\u57fa\u4e8eRedis 6.2.6 \u7248\u672c\u6574\u7406\u603b\u7ed3<\/p>\n<h2>\u4e00\u3001\u5bf9\u8c61<\/h2>\n<p>\u524d\u9762\u51e0\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86Redis\u7528\u5230\u7684\u4e3b\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\uff1asds\u3001list\u3001dict\u3001ziplist\u3001skiplist\u3001inset\u7b49\u3002<\/p>\n<p>\u4f46\u662f\uff0cRedis\u5e76\u6ca1\u6709\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0key-value\u6570\u636e\u5e93\uff0c\u800c\u662f\u57fa\u4e8e\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u6784\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61\u7cfb\u7edf\u3002\u5305\u62ec\u5b57\u7b26\u4e32\u5bf9\u8c61\u3001\u5217\u8868\u5bf9\u8c61\u3001\u54c8\u5e0c\u5bf9\u8c61\u3001\u96c6\u5408\u5bf9\u8c61\u548c\u6709\u5e8f\u96c6\u5408\u5bf9\u8c61\u4e94\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u6bcf\u79cd\u5bf9\u8c61\u90fd\u4f7f\u7528\u4e86\u81f3\u5c11\u4e00\u79cd\u524d\u9762\u63d0\u5230\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u901a\u8fc7\u5bf9\u5bf9\u8c61\u7684\u533a\u5206\uff0cRedis\u53ef\u4ee5\u5728\u6267\u884c\u547d\u4ee4\u524d\u5224\u65ad\u8be5\u5bf9\u8c61\u662f\u5426\u80fd\u591f\u6267\u884c\u8be5\u6761\u547d\u4ee4\u3002\u4e3a\u5bf9\u8c61\u8bbe\u7f6e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u53ea\u8981\u662f\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<h2>\u4e8c\u3001\u5bf9\u8c61\u7684\u7c7b\u578b\u53ca\u7f16\u7801<\/h2>\n<p>Redis\u4f7f\u7528\u5bf9\u8c61\u6765\u8868\u793a\u6570\u636e\u4e2d\u7684key\u548cvalue\uff0c\u6bcf\u5f53\u6211\u4eec\u5728Redis\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u65f6\uff0c\u81f3\u5c11\u4f1a\u521b\u5efa\u4e24\u4e2a\u5bf9\u8c61\uff0c\u4e00\u4e2a\u4f5c\u7528\u57dfkey\uff0c\u53e6\u4e00\u4e2a\u4f5c\u7528\u4e8evalue\u3002<\/p>\n<p>\u4e3e\u4e2a\u6817\u5b50\uff1aset msg \u201chello world\u201d \u8868\u793a\u5206\u522b\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u4fdd\u5b58\u201cmsg\u201d\uff0c\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u4fdd\u5b58\u201chello world\u201d:<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310663f976325fb1a.png\" class=\"aligncenter\"><\/p>\n<h3>redisObject \u7ed3\u6784\u4f53<\/h3>\n<p>Redis\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61\u7531 redisObject \u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\uff0c\u5bf9\u8c61\u7684\u7c7b\u578b\u3001\u7f16\u7801\u3001\u5185\u5b58\u56de\u6536\u3001\u5171\u4eab\u5bf9\u8c61\u90fd\u9700\u8981redisObject\u7684\u652f\u6301\uff0credisObject \u7ed3\u6784\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre>#define LRU_BITS 24\ntypedef struct redisObject {\n    unsigned type:4;   \/\/ \u7c7b\u578b\n    unsigned encoding:4; \/\/ \u7f16\u7801\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;\n<\/pre>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770b\u6bcf\u4e2a\u5b57\u6bb5\u7684\u542b\u4e49:<\/p>\n<p>\uff081\uff09type\uff1a \u53604\u4e2a\u6bd4\u7279\u4f4d\uff0c\u8868\u793a\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u6709\u4e94\u79cd\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u6267\u884ctype\u547d\u4ee4\u65f6\uff0c\u4fbf\u662f\u901a\u8fc7type\u5b57\u6bb5\u83b7\u53d6\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/p>\n<pre>\/* The actual Redis Object *\/\n#define OBJ_STRING 0    \/* String object. *\/\n#define OBJ_LIST 1      \/* List object. *\/\n#define OBJ_SET 2       \/* Set object. *\/\n#define OBJ_ZSET 3      \/* Sorted set object. *\/\n#define OBJ_HASH 4      \/* Hash object. *\/\n<\/pre>\n<p>type\u547d\u4ee4\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310663f97632bcd13.png\" class=\"aligncenter\"><\/p>\n<p>\uff082\uff09encoding\uff1a \u53604\u4e2a\u6bd4\u7279\u4f4d\uff0c\u8868\u793a\u5bf9\u8c61\u4f7f\u7528\u54ea\u79cd\u7f16\u7801\uff0credis\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\uff0c\u5927\u5927\u63d0\u9ad8\u4e86redis\u7684\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002<\/p>\n<p>\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e0d\u540c\u7f16\u7801\u7c7b\u578b\u793a\u4f8b\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310763f976330ff13.png\" class=\"aligncenter\"><\/p>\n<p>\uff083\uff09lru\uff1a \u5360 24 \u4e2a\u6bd4\u7279\u4f4d\uff0c\u8bb0\u5f55\u8be5\u5bf9\u8c61\u6700\u540e\u4e00\u6b21\u88ab\u8bbf\u95ee\u7684\u65f6\u95f4\u3002\u5343\u4e07\u522b\u4ee5\u4e3a\u8fd9\u53ea\u80fd\u5728LRU\u6dd8\u6c70\u7b56\u7565\u4e2d\u624d\u7528\uff0cLFU\u4e5f\u662f\u590d\u7528\u7684\u4e2a\u5b57\u6bb5\u3002\u5f53\u4f7f\u7528LRU\u65f6\uff0c\u5b83\u4fdd\u5b58\u7684\u4e0a\u6b21\u8bfb\u5199\u768424\u4f4dunix\u65f6\u95f4\u6233(\u79d2\u7ea7)\uff1b\u4f7f\u7528LFU\u65f6\uff0c24\u4f4d\u4f1a\u88ab\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c16\u4f4d\u7684\u5206\u949f\u7ea7\u65f6\u95f4\u6233\u548c8\u4f4d\u7279\u6b8a\u8ba1\u6570\u5668\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5c55\u5f00\u4e86\uff0c\u8be6\u7ec6\u53ef\u4ee5\u6ce8\u610f\u6211\u540e\u7eed\u7684\u6587\u7ae0\u3002<\/p>\n<p>\uff084\uff09refcount\uff1a \u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u7c7b\u4f3c\u4e8eshared_ptr \u667a\u80fd\u6307\u9488\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5f53refcount\u4e3a0\u65f6\uff0c\u91ca\u653e\u8be5\u5bf9\u8c61\u3002<\/p>\n<p>\uff085\uff09ptr: \u6307\u5411\u5bf9\u8c61\u5177\u4f53\u7684\u5e95\u5c42\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<h2>\u4e09\u3001\u4e0d\u540c\u5bf9\u8c61\u7f16\u7801\u89c4\u5219<\/h2>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310763f9763361f05.png\" class=\"aligncenter\"><\/p>\n<h2>\u56db\u3001redisObject\u7ed3\u6784\u5404\u5b57\u6bb5\u4f7f\u7528\u8303\u4f8b<\/h2>\n<p>Redis\u4e2d\u64cd\u4f5ckey\u7684\u547d\u4ee4\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a\u4e00\u79cd\u662f\u53ef\u4ee5\u64cd\u4f5c\u4efb\u4f55\u7c7b\u578b\u7684key\uff0c\u5982\uff1adel type object\u7b49\u7b49\uff0c\u53e6\u5916\u4e00\u79cd\u662f\u9488\u5bf9\u7279\u5b9a\u7c7b\u578b\u7684key\u53ea\u80fd\u4f7f\u7528\u7279\u5b9a\u7684\u547d\u4ee4\u3002\u5982\uff1aLLEN\u53ea\u80fd\u7528\u6765\u83b7\u53d6\u5217\u8868\u5bf9\u8c61\u7684\u957f\u5ea6\u3002<\/p>\n<h3>4.1 \u7c7b\u578b\u68c0\u67e5\uff08type\u5b57\u6bb5\uff09<\/h3>\n<p>\u6bd4\u5982\u5bf9\u4e8eLLEN\u547d\u4ee4\uff0cRedis\u670d\u52a1\u5668\u5728\u6267\u884c\u547d\u4ee4\u4e4b\u524d\u4f1a\u5148\u68c0\u67e5\u8f93\u5165\u7684key\u5bf9\u5e94\u7684\u7684value\u5bf9\u8c61\u662f\u5426\u4e3a\u5217\u8868\u7c7b\u578b\uff0c\u5373\u68c0\u67e5\u8be5value\u5bf9\u8c61\u7684type\u7c7b\u578b\u662f\u4e0d\u662fOBJ_LIST\uff0c\u5982\u679c\u662f\u624d\u4f1a\u6267\u884cLLEN\u547d\u4ee4\u3002\u5426\u5219\u5c31\u62d2\u7edd\u6267\u884c\u547d\u4ee4\u5e76\u8fd4\u56de\u64cd\u4f5c\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310763f97633aac88.png\" class=\"aligncenter\"><\/p>\n<h3>4.2 \u591a\u6001\u547d\u4ee4\u7684\u5b9e\u73b0\uff08encoding\uff09<\/h3>\n<p>Redis\u9664\u4e86\u4f1a\u6839\u636evalue\u5bf9\u8c61\u7684\u7c7b\u578b\u6765\u5224\u65ad\u5bf9\u5e94key\u80fd\u5426\u6267\u884c\u6267\u884c\u547d\u4ee4\u5916\uff0c\u8fd8\u4f1a\u6839\u636evalue\u5bf9\u8c61\u7684**\u7f16\u7801\u65b9\u5f0f\uff08encoding\u5b57\u6bb5\uff09**\u9009\u62e9\u6b63\u786e\u7684\u65b9\u5f0f\u6765\u6267\u884c\u547d\u4ee4\u3002\u6bd4\u5982\uff1a\u5217\u8868\u5bf9\u8c61\u7684\u7f16\u7801\u65b9\u5f0f\u6709quicklist \u548c ziplist\u4e24\u79cd\uff0cRedis\u670d\u52a1\u5668\u9664\u4e86\u5224\u65ad\u5bf9\u5e94value\u5bf9\u8c61\u7684\u7c7b\u578b\u4e3a\u5217\u8868\u5bf9\u8c61\u8fd8\u8981\u6839\u636e\u5177\u4f53\u7684\u7f16\u7801\u9009\u62e9\u6b63\u786e\u7684LLEN\u6267\u884c\u3002<\/p>\n<p>\u501f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u672f\u8bed\u6765\u8bf4\uff0c\u53ef\u4ee5\u8ba4\u4e3aLLEN\u547d\u4ee4\u662f\u591a\u6001\u7684\u3002\u53ea\u8981\u6267\u884cLLEN\u547d\u4ee4\u7684\u5217\u8868\u952e\uff0c\u65e0\u8bbavalue\u5bf9\u8c61\u7684\u7f16\u7801\u662f\u54ea\u79cd\u65b9\u5f0f\uff0cLLEN\u547d\u4ee4\u90fd\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u3002\u5b9e\u9645\u4e0adel type \u7b49\u4e5f\u662f\u591a\u6001\u547d\u4ee4\u3002\u4ed6\u4eec\u548cLLEN\u7684\u533a\u522b\u5728\u4e8e\uff0c\u524d\u8005\u662f\u57fa\u4e8e\u7c7b\u578b\u7684\u591a\u6001\uff0c\u540e\u8005\u662f\u57fa\u4e8e\u7f16\u7801\u7684\u591a\u6001\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310863f976342e721.png\" class=\"aligncenter\"><\/p>\n<h3>4.3 \u5185\u5b58\u56de\u6536\u548c\u5171\u4eab\u5bf9\u8c61\uff08refcount\uff09<\/h3>\n<p>C\u8bed\u8a00\u4e0d\u5177\u5907\u81ea\u52a8\u56de\u6536\u529f\u80fd\uff0cRedis\u5c31\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u5b9e\u73b0\u4e86\u81ea\u5df1\u7684\u5185\u5b58\u56de\u6536\u673a\u5236\u3002\u5177\u4f53\u662f\u7531redisObject\u7ed3\u6784\u4e2d\u7684refcount\u5b57\u6bb5\u8bb0\u5f55\u3002\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u4f1a\u968f\u7740\u5bf9\u8c61\u7684\u4f7f\u7528\u72b6\u6001\u800c\u4e0d\u65ad\u53d8\u5316\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u65f6\uff0crefcount\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a1,\uff1b\u5f53\u5bf9\u8c61\u88ab\u53e6\u4e00\u4e2a\u65b0\u7a0b\u5e8f\u4f7f\u7528\u65f6 refcount\u52a01\uff1b\u4e0d\u88ab\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u65f6\u51cf1\uff1b\u5f53refcount==0\u65f6\uff0c\u8be5\u5bf9\u8c61\u6240\u5360\u7684\u7a7a\u95f4\u4f1a\u88ab\u56de\u6536\u3002<\/p>\n<p>\u5f15\u7528\u8ba1\u6570\u9664\u4e86\u88ab\u7528\u6765\u5b9e\u73b0\u5185\u5b58\u56de\u6536\u5916\uff0c\u8fd8\u88ab\u7528\u6765\u5b9e\u73b0\u5bf9\u8c61\u5171\u4eab\u3002\u6bd4\u5982\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310863f9763474937.png\" class=\"aligncenter\"><\/p>\n<p>\u4e0a\u9762\u6211\u4eec\u521b\u5efa\u53ef\u4e00\u4e2avalue\u4e3a100\u7684key A\uff0c\u5e76\u4f7f\u7528object refcount\u6765\u67e5\u770bkey A\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u4f1a\u770b\u5230\u6b64\u65f6\u7684refcount\u4e3a2\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u6b64\u65f6\u6709\u4e24\u4e2a\u5730\u65b9\u5f15\u7528\u4e86\u8fd9\u4e2avalue\u5bf9\u8c61\uff0c\u4e00\u4e2a\u662f\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\uff0c\u53e6\u5916\u4e00\u4e2a\u662f\u5171\u4eab\u8be5value\u5bf9\u8c61\u7684key A\u3002\u5982\u679c\uff0c\u6211\u4eec\u518d\u521b\u5efa\u4e00\u4e2avalue\u4e3a100 \u7684 key B\uff0c\u90a3\u4e48\u952eB\u4e5f\u4f1a\u6307\u5411\u8fd9\u4e2avalue\u5bf9\u8c61\uff0c\u4f7f\u5f97\u8be5\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a3\u3002\u7531\u6b64\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u5171\u4eabvalue\u5bf9\u8c61\u7684\u952e\u8d8a\u591a\uff0c\u8282\u7ea6\u7684\u5185\u5b58\u5c31\u8d8a\u591a\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230225\/167729310863f97634c028e.png\" class=\"aligncenter\"><\/p>\n<p>\u5728\u521b\u5efa\u952eB\u7684\u65f6\u5019\uff0c\u670d\u52a1\u5668\u8003\u5230\u952eB\u8981\u521b\u5efa\u7684\u5bf9\u8c61\u662fint\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61100\uff0c\u800c\u521a\u597d\u6709\u4e2avalue\u4e3a100\u7684\u5171\u4eab\u5bf9\u8c61\u5339\u914d\uff0c\u5c31\u76f4\u63a5\u5c06\u952eB\u6307\u5411\u8be5\u5171\u4eab\u5bf9\u8c61\u3002\u56e0\u4e3a\u662f\u6574\u6570\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u76f4\u63a5\u6bd4\u8f83\u5373\u53ef\uff0c\u5982\u679c\u5171\u4eab\u5bf9\u8c61\u662f\u5b57\u7b26\u4e32\u503c\u7684\u5bf9\u8c61\uff0c\u8981\u4ece\u5934\u5230\u5c3e\u6bd4\u8f83\u6bcf\u4e2a\u5b57\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6O(n)\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff0c\u8981\u80fd\u4f7f\u7528\u5171\u4eab\u5bf9\u8c61\uff0c\u9700\u8981\u5148\u9a8c\u8bc1\u8be5\u5171\u4eab\u5bf9\u8c61\u548c\u8981\u521b\u5efa\u7684\u76ee\u6807\u5bf9\u8c61\u662f\u4e0d\u662f\u5b8c\u5168\u4e00\u81f4\uff0c\u5982\u679c\u5171\u4eab\u5bf9\u8c61\u4fdd\u5b58\u7684\u503c\u8d8a\u590d\u6742\uff0c\u6d88\u8017\u7684CPU\u4e5f\u5c31\u8d8a\u591a\uff0c\u6240\u4ee5Redis\u503c\u5bf9\u6574\u6570\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u505a\u4e86\u5171\u4eab\u3002\u6ca1\u6709\u5171\u4eab\u4fdd\u5b58\u5b57\u7b26\u4e32\u503c\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002<\/p>\n<p>Redis\u5728\u521d\u59cb\u5316\u670d\u52a1\u5668\u662f\uff0c\u5c31\u521b\u5efa\u4e86\u4e00\u4e07\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u5305\u542b\u4e860~9999\u7684\u6240\u6709\u6574\u6570\u503c\u3002\u5f53\u4f60\u521b\u5efa\u4e86\u8fd9\u4e2a\u8303\u56f4\u5185\u7684 \u5b57\u7b26\u4e32\u5bf9\u8c61\u65f6\uff0c\u670d\u52a1\u5668\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e9b\u5171\u4eab\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u65b0\u5bf9\u8c61\uff0c\u4ee5\u8282\u7ea6\u5185\u5b58\u3002<\/p>\n<h3>4.4 \u5bf9\u8c61\u7684\u7a7a\u8f6c\u65f6\u957f\uff08lru\uff09<\/h3>\n<p>redisObject\u7ed3\u6784\u4e2d\u7684lru\u5b57\u6bb5\u4fdd\u5b58\uff0c\u8be5\u5bf9\u8c61\u6700\u540e\u4e00\u6b21\u88ab\u8bbf\u95ee\u7684\u65f6\u95f4\u3002 \u4f7f\u7528object idletime \u6765\u67e5\u770b\uff0c\u6ce8\u610f\u8fd9\u4e2a\u547d\u4ee4\u4e0d\u4f1a\u4fee\u6539\u5bf9\u8c61\u7684lru\u5c5e\u6027\u3002<\/p>\n<p>\u5f53Redis\u5f00\u542f\u6700\u5927\u5185\u5b58\u9650\u5236\uff0c\u4e00\u822c\u4e3a\u673a\u5668\u5185\u5b58\u7684\u4e00\u534a\uff0c\u5982\u679credis\u4f7f\u7528\u7684\u5185\u5b58\u8fbe\u5230\u8fd9\u4e2a\u503c\uff0c\u4e14\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u4f7f\u7528\u7684\u662fvolatile-lru \u6216\u8005 allkeys-lru\uff0c\u7a7a\u8f6c\u65f6\u957f\u8f83\u9ad8\u7684\u90a3\u4e9b\u952e\u4f1a\u88ab\u4f18\u5148\u91ca\u653e\u3002<\/p>\n<p>\u4f7f\u7528object idletime \u67e5\u770b\u952e\u7684\u7a7a\u95f4\u65f6\u95f4\uff0c\u5355\u4f4d\uff1a\u79d2\uff1a<\/p>\n<blockquote>\n<p>127.0.0.1:6379[1]&gt; keys *<br \/>1) &#8220;msg&#8221;<br \/>2) &#8220;teacher&#8221;<br \/>127.0.0.1:6379[1]&gt; object idletime msg<br \/>(integer) 71166<br \/>127.0.0.1:6379[1]&gt;<\/p>\n<\/blockquote>\n<h2>\u4e94\u3001\u5bf9\u8c61\u5728\u6e90\u7801\u4e2d\u7684\u4f7f\u7528<\/h2>\n<h3>5.1 \u5b57\u7b26\u4e32\u5bf9\u8c61<\/h3>\n<h4>5.1.1\u5b57\u7b26\u4e32\u5bf9\u8c61\u521b\u5efa<\/h4>\n<pre>\/\/ code location\uff1a src\/object.c  \n#define OBJ_ENCODING_EMBSTR_SIZE_LIMIT 44\n\/\/ \u521b\u5efa strintg \u5bf9\u8c61\nrobj *createStringObject(const char *ptr, size_t len) {\n\t\/\/ \u5982\u679c\u5f85\u4fdd\u5b58\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e44\uff0c\u4f7f\u7528 embstr \u7f16\u7801\n    if (len type = OBJ_STRING;      \/\/ \u8bbe\u7f6e\u7c7b\u578b\n    o-&gt;encoding = OBJ_ENCODING_EMBSTR; \/\/ \u8bbe\u7f6e\u7f16\u7801\n    o-&gt;ptr = sh+1;\n    o-&gt;refcount = 1;\n    if (server.maxmemory_policy &amp; MAXMEMORY_FLAG_LFU) {\n        o-&gt;lru = (LFUGetTimeInMinutes()lru = LRU_CLOCK();\n    }\n    sh-&gt;len = len;\n    sh-&gt;alloc = len;\n    sh-&gt;flags = SDS_TYPE_8;\n    if (ptr == SDS_NOINIT)\n        sh-&gt;buf[len] = '\\0';\n    else if (ptr) {\n        memcpy(sh-&gt;buf,ptr,len);\n        sh-&gt;buf[len] = '\\0';\n    } else {\n        memset(sh-&gt;buf,0,len+1);\n    }\n    return o;\n}<\/pre>\n<p>\u4ece createEmbeddedStringObject \u51fd\u6570\u53ef\u4ee5\u770b\u5230\uff0c\u8be5\u5bf9\u8c61\u662frobj\u548csds\u7684\u7ed3\u5408\u4f53\uff0c\u5c06sds\u76f4\u63a5\u653e\u5165\u5230robj\u91cc\uff0c\u8fd9\u4e5f\u662f\u5d4c\u5165\u5f0f\u7f16\u7801embstr\u7684\u7531\u6765\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u9650\u523644\u5b57\u8282\u5462\uff1f\u56e0\u4e3arobj\u7ed3\u6784\u4f53\u536016\u4e2a\u5b57\u8282\uff0csdshdr\u7ed3\u6784\u4f53\u53603\u4e2a\u5b57\u8282\uff0c\u6700\u540e\u7ed3\u5c3e\u7684\u2018\\0\u2019\u5360\u4e00\u4e2a\u5b57\u8282\uff0c\u9650\u523644\u4e2a\u5b57\u8282\uff0c\u5c31\u80fd\u4fdd\u8bc164\u4e2a\u5b57\u8282\u91cc\u80fd\u653e\u4e0b\u6240\u6709\u5185\u5bb9\uff0816+3+1+44 = 64\uff09\u3002<\/p>\n<h4>5.1.2 \u5b57\u7b26\u4e32\u5bf9\u8c61\u7f16\u7801<\/h4>\n<p>Redis\u5c06\u8282\u7701\u5185\u5b58\u505a\u5230\u4e86\u6781\u81f4\uff0c\u5b83\u7684\u4f5c\u8005\u5bf9\u5b57\u7b26\u4e32\u5bf9\u8c61\u53c8\u505a\u4e86\u7279\u6b8a\u7684\u7f16\u7801\u5904\u7406\uff0c\u4ee5\u8fdb\u4e00\u6b65\u8fbe\u5230\u8282\u7701\u7a7a\u95f4\u7684\u76ee\u7684\u3002\u7f16\u7801\u5904\u7406\u8fc7\u7a0b\u53ca\u4ee3\u7801\u6ce8\u91ca\u5982\u4e0b\uff1a<\/p>\n<pre>\/* Try to encode a string object in order to save space *\/\nrobj *tryObjectEncoding(robj *o) {\n    long value;\n    sds s = o-&gt;ptr;\n    size_t len;\n    \/* Make sure this is a string object, the only type we encode\n     * in this function. Other types use encoded memory efficient\n     * representations but are handled by the commands implementing\n     * the type. *\/\n    \/\/ \u8fd9\u91cc\u53ea\u5bf9string\u5bf9\u8c61\u8fdb\u884c\u7f16\u7801\uff0c\u5176\u4ed6\u7c7b\u578b\u7684\u7f16\u7801\u90fd\u6709\u5bf9\u5e94\u7684\u5177\u4f53\u5b9e\u73b0\u5904\u7406\n    serverAssertWithInfo(NULL,o,o-&gt;type == OBJ_STRING);\n    \/* We try some specialized encoding only for objects that are\n     * RAW or EMBSTR encoded, in other words objects that are still\n     * in represented by an actually array of chars. *\/\n    \/\/ \u975esds string\u5bf9\u8c61\uff0c\u76f4\u63a5\u8fd4\u56de\u539f\u5bf9\u8c61\n    if (!sdsEncodedObject(o)) return o;\n    \/* It's not safe to encode shared objects: shared objects can be shared\n     * everywhere in the \"object space\" of Redis and may end in places where\n     * they are not handled. We handle them only as values in the keyspace. *\/\n    \/\/ \u5982\u679c\u8be5\u5bf9\u8c61\u7531\u5176\u4ed6\u5bf9\u8c61\u5171\u4eab\uff0c\u4e0d\u80fd\u7f16\u7801\uff0c\u5982\u679c\u7f16\u7801\u53ef\u80fd\u5f71\u54cd\u5230\u5176\u4ed6\u5bf9\u8c61\u7684\u4f7f\u7528\n     if (o-&gt;refcount &gt; 1) return o;\n    \/* Check if we can represent this string as a long integer.\n     * Note that we are sure that a string larger than 20 chars is not\n     * representable as a 32 nor 64 bit integer. *\/\n    \/\/ \u68c0\u67e5\u80fd\u5426\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u793a\u4e3a\u957f\u6574\u578b\u6570\uff0c\u957f\u5ea6\u8981\u5c0f\u4e8e\u7b49\u4e8e20\n    len = sdslen(s);\n    if (len = 0 &amp;&amp;\n            value encoding == OBJ_ENCODING_RAW) {\n                sdsfree(o-&gt;ptr);\n                o-&gt;encoding = OBJ_ENCODING_INT;\n                o-&gt;ptr = (void*) value;\n                return o;\n            } else if (o-&gt;encoding == OBJ_ENCODING_EMBSTR) {\n                decrRefCount(o);\n                return createStringObjectFromLongLongForValue(value);\n            }\n        }\n    }\n    \/\/ \u4e0d\u80fd\u8f6c\u4e3along\u7684\u5b57\u7b26\u4e32\n    \/* If the string is small and is still RAW encoded,\n     * try the EMBSTR encoding which is more efficient.\n     * In this representation the object and the SDS string are allocated\n     * in the same chunk of memory to save space and cache misses. *\/\n    \/\/ \u5982\u679c\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u592a\u5c0f\uff0c\u5c0f\u4e8e\u7b49\u4e8e44\n    if (len encoding == OBJ_ENCODING_EMBSTR) return o;\n        emb = createEmbeddedStringObject(s,sdslen(s));\n        decrRefCount(o);\n        return emb;\n    }\n    \/* We can't encode the object...\n     *\n     * Do the last try, and at least optimize the SDS string inside\n     * the string object to require little space, in case there\n     * is more than 10% of free space at the end of the SDS string.\n     *\n     * We do that only for relatively large strings as this branch\n     * is only entered if the length of the string is greater than\n     * OBJ_ENCODING_EMBSTR_SIZE_LIMIT. *\/\n    \/\/ \u5982\u679c\u524d\u9762\u7f16\u7801\u6ca1\u6709\u6210\u529f\uff0c\u8fd9\u91cc\u505a\u6700\u540e\u4e00\u6b65\uff0c\u5f53\u7f16\u7801\u7c7b\u578b\u4e3araw\uff0c\u4e14\u5b83\u7684sds\u53ef\u7528\u7a7a\u95f4\u8d85\u8fc710%\uff0c\n    \/\/ \u5c1d\u8bd5\u91ca\u653e\u8c03\u8fd9\u90e8\u5206\u5185\u5b58\n    trimStringObjectIfNeeded(o);\n    \/* Return the original object. *\/\n    \/\/ \u8fd4\u56de\u539f\u5bf9\u8c61\n    return o;\n}<\/pre>\n<h4>5.1.3 \u5b57\u7b26\u4e32\u5bf9\u8c61\u89e3\u7801<\/h4>\n<p>\u6709\u7f16\u7801\u5c31\u6709\u89e3\u7801\uff0c\u5b9e\u9645\u4e0a\u53ea\u9700\u8981\u90a3\u4e9b\u53ef\u4ee5\u8f6c\u4e3a\u6574\u578b\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u4f20\u8fdb\u884c\u89e3\u7801\uff0c\u89e3\u7801\u4ee3\u7801\u53ca\u6ce8\u91ca\u5982\u4e0b\uff1a<\/p>\n<pre>robj *getDecodedObject(robj *o) {\n    robj *dec;\n\t\/\/ \u5982\u679c\u7f16\u7801\u662f embstr \u548c raw \uff0c\u53ea\u662f\u628a\u5f15\u7528\u8ba1\u6570\u52a0\u4e00\uff0c\u7136\u540e\u8fd4\u56de\u539f\u5bf9\u8c61\n    if (sdsEncodedObject(o)) {\n        incrRefCount(o);\n        return o;\n    }\n    \/\/ \u5982\u679c\u7f16\u7801\u662f int \u8fdb\u884c\u89e3\u7801\uff0c\u8fd4\u56de\u65b0\u7684\u5bf9\u8c61\n    if (o-&gt;type == OBJ_STRING &amp;&amp; o-&gt;encoding == OBJ_ENCODING_INT) {\n        char buf[32];\n        ll2string(buf,32,(long)o-&gt;ptr);\n        dec = createStringObject(buf,strlen(buf));\n        return dec;\n    } else {\n        serverPanic(\"Unknown encoding type\");\n    }\n}\n<\/pre>\n<h4>5.1.4 redis\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570\u53ca\u81ea\u52a8\u6e05\u7406<\/h4>\n<pre>void incrRefCount(robj *o) {\n    if (o-&gt;refcount refcount++; \/\/ \u5f15\u7528\u8ba1\u6570\u52a0\u4e00\n    } else {\n        if (o-&gt;refcount == OBJ_SHARED_REFCOUNT) {\n            \/* Nothing to do: this refcount is immutable. *\/\n        } else if (o-&gt;refcount == OBJ_STATIC_REFCOUNT) {\n            serverPanic(\"You tried to retain an object allocated in the stack\");\n        }\n    }\n}\n\/\/ \u51cf\u5c11\u5f15\u7528\u8ba1\u6570\nvoid decrRefCount(robj *o) {\n\t\/\/ \u91ca\u653e\u7a7a\u95f4\n    if (o-&gt;refcount == 1) {\n        switch(o-&gt;type) {\n        case OBJ_STRING: freeStringObject(o); break;\n        case OBJ_LIST: freeListObject(o); break;\n        case OBJ_SET: freeSetObject(o); break;\n        case OBJ_ZSET: freeZsetObject(o); break;\n        case OBJ_HASH: freeHashObject(o); break;\n        case OBJ_MODULE: freeModuleObject(o); break;\n        case OBJ_STREAM: freeStreamObject(o); break;\n        default: serverPanic(\"Unknown object type\"); break;\n        }\n        zfree(o);\n    } else {\n        if (o-&gt;refcount refcount != OBJ_SHARED_REFCOUNT) o-&gt;refcount--; \/\/ \u8ba1\u6570\u51cf\u4e00\n    }\n}\n<\/pre>\n<h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<ul>\n<li>redis\u5bf9\u8c61\u4e3a\u6240\u6709\u7c7b\u578b\u7684value\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684\u5c01\u88c5<\/li>\n<li>\u4e3a\u5bf9\u8c61\u7684\u6dd8\u6c70\u7b56\u7565\u4fdd\u5b58\u76f8\u5173\u4fe1\u606f<\/li>\n<li>\u5b9e\u73b0\u5f15\u7528\u8ba1\u6570\u53ca\u5185\u5b58\u81ea\u52a8\u91ca\u653e\u529f\u80fd<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5bf9\u8c61\u4e0eredisObj&#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-201641","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201641","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=201641"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201641\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}