{"id":201611,"date":"2025-05-10T12:17:26","date_gmt":"2025-05-10T04:17:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201611\/"},"modified":"2025-05-10T12:17:26","modified_gmt":"2025-05-10T04:17:26","slug":"redis%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%8e%9f%e7%90%86%e6%b5%85%e6%9e%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201611\/","title":{"rendered":"Redis\u6570\u636e\u7ed3\u6784\u539f\u7406\u6d45\u6790"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u6570\u636e\u7ed3\u6784\u539f\u7406\u6d45\u6790<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728##column_title##\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aRedis\u6570\u636e\u7ed3\u6784\u539f\u7406\u6d45\u6790\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0bRedis\u6570\u636e\u7ed3\u6784\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<h2>RedisDb<\/h2>\n<p>Redis\u670d\u52a1\u5668\u9ed8\u8ba4\u670916\u4e2a\u6570\u636e\u5e93\uff0c\u4e00\u4e2a\u6570\u636e\u5e93\u5bf9\u5e94\u4e00\u4e2aRedisDB\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<pre>typedef struct redisDb {\n    dict *dict;\n    dict *expires;\n    dict * blocking_keys;\n    dict * ready_keys;\n    dict * watched_keys;\n    ......\n}\n\n<\/pre>\n<ul>\n<li>dict:\u952e\u7a7a\u95f4\u6563\u5217\u8868\uff0c\u7528\u4e8e\u5b58\u653e\u6240\u6709\u952e\u503c\u5bf9<\/li>\n<li>expires:\u8fc7\u671f\u65f6\u95f4\u6563\u5217\u8868\uff0c\u5b58\u653e\u952e\u7684\u8fc7\u671f\u65f6\u95f4<\/li>\n<li>blocking_keys:\u5904\u4e8e\u963b\u585e\u72b6\u6001\u7684\u952e\u548c\u5bf9\u5e94\u7684client<\/li>\n<li>ready_keys:\u89e3\u9664\u963b\u585e\u72b6\u6001\u7684\u952e\u548c\u5bf9\u5e94\u7684client\uff0c\u4e0eblocking_keys\u5c5e\u6027\u76f8\u5bf9<\/li>\n<li>watched_keys:watch\u7684\u952e\u548c\u5bf9\u5e94\u7684client\uff0c\u4e3b\u8981\u7528\u4e8e\u4e8b\u52a1<\/li>\n<\/ul>\n<h2>RedisObject<\/h2>\n<p>Redis\u7684\u952e\u503c\u90fd\u662fredisObject\u5bf9\u8c61\uff0c\u6bcf\u6b21\u5f53\u6211\u4eec\u5728Redis\u7684\u6570\u636e\u5e93\u4e2d\u65b0\u521b\u5efa\u4e00\u4e2a\u952e\u503c\u5bf9\u65f6\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u7528\u4e8e\u952e\u540d\u7684redisObject\u5bf9\u8c61\u548c\u4e00\u4e2a\u7528\u4e8e\u952e\u503c\u7684redisObject\u5bf9\u8c61<\/p>\n<pre>trpedef struct RedisObject {\n    int4 type;\n    int4 encoding;\n    void *ptr;\n    int24 lru;\n    int32 refcount;\n}\n<\/pre>\n<table>\n<tbody>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td>type<\/td>\n<td>\u7528\u4e8e\u8868\u793aRedis\u5bf9\u5e94\u7684\u7c7b\u578b<\/td>\n<td>string\u3001list\u3001hash\u3001set\u3001zset\u3001stream\u7b49\uff0c\u7528\u679a\u4e3e\u8868\u793a<\/td>\n<\/tr>\n<tr>\n<td>encoding<\/td>\n<td>\u5185\u90e8\u7f16\u7801<\/td>\n<td>int\uff0cembstr\uff0craw\uff0chashtable\uff0cquicklist\uff0c ziplist\uff0cintset\uff0cskiplist\u7b49\uff0c\u7528\u679a\u4e3e\u8868\u793a<\/td>\n<\/tr>\n<tr>\n<td>lru<\/td>\n<td>24\u4f4d\uff0c\u53ef\u9009LFU\u6216LRU<\/td>\n<td>\u5f53\u4e3aLRU\u65f6\uff0c\u8868\u793a\u6700\u540e\u4e00\u6b21\u8bbf\u95ee\u65f6\u95f4\uff1b\u5f53\u4e3aLFU\u65f6\uff0c\u9ad816\u4f4d\u7528\u6765\u8868\u793a\u5206\u949f\u7ea7\u522b\u7684\u8bbf\u95ee\u65f6\u95f4\uff0c\u4f4e8\u4f4d\u7528\u6765\u8868\u793a\u8bbf\u95ee\u9891\u6b21\uff0c\u9891\u6b21\u7684\u589e\u52a0\u4f7f\u7528\u7684\u662f\u6982\u7387\u7b97\u6cd5\uff0c\u57fa\u6570\u8d8a\u5927\u8d8a\u96be\u589e\u52a0\uff1b\u8bbf\u95ee\u65f6\u95f4\u66f4\u65b0\u65f6\uff0c\u5b58\u5728\u4e00\u5b9a\u6982\u7387\u5c06\u8bbf\u95ee\u9891\u6b21\u8870\u51cf\u3002\uff08\u4e24\u8005\u5171\u6709\uff09\u8bbf\u95ee\u65f6\u95f4\u662f\u5bf9\u4e00\u4e2a\u6570\u53d6\u6a21\uff0c\u5f53\u524d\u65f6\u95f4\u4e5f\u53d6\u6a21\uff0c \u5f53\u524d\u65f6\u95f4\u5927\u4e8e\u8bbf\u95ee\u65f6\u95f4\uff0c\u5219\u4e3a\u4e24\u6570\u4e4b\u5dee\uff1b\u5f53\u524d\u65f6\u95f4\u5c0f\u4e8e\u8bbf\u95ee\u65f6\u95f4\uff0c\u5219\u4e3a\u5f53\u524d\u65f6\u95f4\u52a0\u4e0a\u6a21\u6570\u4e0e\u8bbf\u95ee\u65f6\u95f4\u4e4b\u5dee<\/td>\n<\/tr>\n<tr>\n<td>refcount<\/td>\n<td>\u5f15\u7528\u8ba1\u6570<\/td>\n<td>\u521d\u59cb\u503c\u4e3a1\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u53c2\u8003\u610f\u4e49\u4e0d\u5927<\/td>\n<\/tr>\n<tr>\n<td>ptr<\/td>\n<td>\u6307\u9488\uff0c\u53608\u4e2a\u5b57\u8282\uff0c\u6307\u5411\u6570\u636e\u7684\u5730\u5740<\/td>\n<td>dict\u3001expires\u7b49\uff0c\u6307\u9488\u6307\u5411\u540c\u4e00\u4e2a\u5730\u5740<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>object<\/code>\u547d\u4ee4\uff0c\u5c31\u662f\u5bf9RedisObject\u7684\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n<h3>\u4fee\u6539\u5185\u5b58\u6dd8\u6c70\u7b56\u7565<\/h3>\n<p>object idletime key # \u8fd4\u56dekey\u7684\u7a7a\u95f2\u65f6\u95f4\uff0c\u5373\u4e0a\u6b21\u8bfb\u5199\u952e\u4ee5\u6765\u7ecf\u8fc7\u7684\u8fd1\u4f3c\u63cf\u8ff0\uff0c\u5728lfu\u6a21\u5f0f\u4e0b\u4e0d\u53ef\u7528<\/p>\n<pre>config set maxmemory-policy volatile-lfu # \u4fee\u6539\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\nset name zhangsan\nobject freq name # \u83b7\u53d6\u8ba1\u6570\u503c\uff0c\u4ec5lfu\u6a21\u5f0f\u4e0b\u53ef\u7528\uff0c\u521d\u59cb\u5316\u4e3a5\n\nget name\n\nobject freq name # \u518d\u6b21\u8bbf\u95ee\uff0c\u8fd4\u56de\u4e3a6\n<\/pre>\n<h2>int<\/h2>\n<p>\u5f53string\u503c\u4e3a\u6574\u6570\u5e76\u4e14\u5c0f\u4e8e\u7b49\u4e8elong\u7684\u6700\u5927\u503c\u65f6\uff0cencoding\u4e3aint\u7c7b\u578b\uff0cptr\u76f4\u63a5\u6307\u5411\u8be5int\u578b\u5730\u5740<\/p>\n<h2>embstr\u4e0eraw<\/h2>\n<p>Redis\u7684\u5b57\u7b26\u4e32\u53ebSDS\uff08Simple Dynamic String\uff0c\u7b80\u5355\u5b57\u7b26\u4e32\uff09\uff0c\u5bf9\u5e94key\uff0c\u975e\u6574\u6570\u578b\u7684String\u503c<\/p>\n<pre>trpedef struct SDS {\n    int8 capacity; \/\/ \u6570\u7ec4\u5bb9\u91cf\n    int8 len; \/\/ \u5b9e\u9645\u957f\u5ea6\n    int8 flags;\n    byte[] content; \/\/ \u6570\u7ec4\u5185\u5bb9\n}\n<\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c<strong>SDS\u4e0eJava\u7684ArrayList\u7ed3\u6784\u7c7b\u4f3c<\/strong>\uff0c\u4e5f\u662f\u5206\u914d\u521d\u59cb\u957f\u5ea6\uff0c\u957f\u5ea6\u8d85\u51fa\u65f6\u6269\u5bb9\u3002<strong>Redis\u89c4\u5b9a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc7512M\u3002<\/strong><\/p>\n<p>\u5f53\u957f\u5ea6\u7279\u522b\u77ed\u65f6\uff0c\u4f7f\u7528embstr\u5f62\u5f0f\u5b58\u50a8\uff1b\u5f53\u957f\u5ea6\u8d85\u51fa44\u5b57\u8282\u65f6\uff0c\u4f7f\u7528raw\u5f62\u5f0f\u5b58\u50a8\u3002<\/p>\n<p>\u5df2\u77e5\u5185\u5b58\u5206\u914d\u5668\u6700\u5927\u5206\u914d\u5355\u4f4d\u662f64\u5b57\u8282\uff0cRedisObject\u536016\u4e2a\u5b57\u8282\uff0cSDS\u6807\u8bc6\u53603\u4e2a\u5b57\u8282\uff0c\u5b57\u7b26\u4e32\u4ee5NULL\u7ed3\u5c3e\u9700\u8981\u5360\u7528\u4e00\u4e2a\u5b57\u8282\uff0c\u56e0\u6b64\u5f53\u5b57\u7b26\u4e32\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e44\u65f6\uff0c\u53ea\u9700\u8981\u5206\u914d\u4e00\u6b21\u5185\u5b58\u3002RedisObject\u4e0eSDS\u5728\u540c\u4e00\u5185\u5b58\u5355\u4f4d\uff0c\u6211\u4eec\u5c06\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u79f0\u4e3aembstr\uff0c\u800c\u4e0d\u5728\u540c\u4e00\u5185\u5b58\u5355\u4f4d\u7684\uff0c\u79f0\u4e3araw\u3002<\/p>\n<h2>dict<\/h2>\n<p>dict\uff08encoding\u7f16\u7801\u4e3ahashtable\u7c7b\u578b\uff0c\u5b57\u5178\uff09\u5bf9\u5e94hash\u3001set\u3001zset\uff08\u7528\u4e8e\u5b58\u50a8value\u4e0escore\u7684\u6620\u5c04\uff09\u96c6\u5408\u3002<\/p>\n<p><strong>dict\u4e0eJava\u7684HashMap\u7ed3\u6784\u7c7b\u4f3c<\/strong>\uff0c\u4e0d\u540c\u7684\u662fHashMap\u6269\u5bb9\u662f\u7533\u8bf7\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\uff0c\u5c06\u65e7\u6570\u636e\u91cd\u65b0hash\u540e\u6302\u5230\u6570\u7ec4\u4e0b\u9762\uff0c\u4f5c\u4e3a\u5355\u7ebf\u7a0b\u7684Redis\u5f88\u96be\u627f\u53d7\u8fd9\u6837\u8017\u65f6\u7684\u8fc7\u7a0b\uff0c\u6240\u4ee5\u5b83\u4f7f\u7528\u4e86\u4e24\u4e2a\u6570\u7ec4\uff0c\u5148\u8fd4\u56de\uff0c\u7136\u540e\u7a7a\u95f2\u7684\u65f6\u5019\u4e00\u70b9\u4e00\u70b9\u642c\u6570\u636e\uff0c\u642c\u5b8c\u4e4b\u540e\u518d\u5c06\u65e7\u6570\u636e\u6e05\u7a7a\uff0c\u6211\u4eec\u5c06\u8fd9\u6837\u7684\u8fc7\u7a0b\u6210\u4e3a<strong>\u6e10\u8fdb\u5f0frehash<\/strong>\u3002<\/p>\n<pre>typedef struct dict {\n    dictht ht[2];\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u6570\u636e\u7ed3\u6784\u539f\u7406\u6d45\u6790 \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-201611","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201611","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=201611"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201611\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}