{"id":201845,"date":"2025-05-10T11:55:13","date_gmt":"2025-05-10T03:55:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201845\/"},"modified":"2025-05-10T11:55:13","modified_gmt":"2025-05-10T03:55:13","slug":"%e6%b7%b1%e5%ba%a6%e5%9b%be%e8%a7%a3-redis-hash%ef%bc%88%e6%95%a3%e5%88%97%e8%a1%a8%ef%bc%89%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201845\/","title":{"rendered":"\u6df1\u5ea6\u56fe\u89e3 Redis Hash\uff08\u6563\u5217\u8868\uff09\u5b9e\u73b0\u539f\u7406"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u6df1\u5ea6\u56fe\u89e3 Redis Hash\uff08\u6563\u5217\u8868\uff09\u5b9e\u73b0\u539f\u7406<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230golang\u5b66\u4e60\u7f51\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6df1\u5ea6\u56fe\u89e3 Redis Hash\uff08\u6563\u5217\u8868\uff09\u5b9e\u73b0\u539f\u7406\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Redis\u3001\u6563\u5217\u8868<\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230529\/1685336808647432e8a2b90.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<h2><span style=\"color: #515151\">1\u3001\u662f\u4ec0\u4e48<\/span><\/h2>\n<p>Redis Hash\uff08\u6563\u5217\u8868\uff09\u662f\u4e00\u79cd field-value pairs\uff08\u952e\u503c\u5bf9\uff09\u96c6\u5408\u7c7b\u578b\uff0c\u7c7b\u4f3c\u4e8e Python \u4e2d\u7684\u5b57\u5178\u3001Java \u4e2d\u7684 HashMap\u3002\u4e00\u4e2a field \u5bf9\u5e94\u4e00\u4e2a value\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 field \u5728 O(1) \u65f6\u95f4\u590d\u6742\u5ea6\u67e5 field \u627e\u5173\u8054\u7684 field\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 field \u6765\u66f4\u65b0\u6216\u8005\u5220\u9664\u8fd9\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<p>Redis \u7684\u6563\u5217\u8868 dict \u7531\u6570\u7ec4 + \u94fe\u8868\u6784\u6210\uff0c\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5360\u7528\u7684\u69fd\u4f4d\u53eb\u505a\u54c8\u5e0c\u6876\uff0c\u5f53\u51fa\u73b0\u6563\u5217\u51b2\u7a81\u7684\u65f6\u5019\u5c31\u4f1a\u5728\u8fd9\u4e2a\u6876\u4e0b\u6302\u4e00\u4e2a\u94fe\u8868\uff0c\u7528\u201c\u62c9\u94fe\u6cd5\u201d\u89e3\u51b3\u6563\u5217\u51b2\u7a81\u7684\u95ee\u9898\u3002<\/p>\n<p>\u7b80\u5355\u5730\u8bf4\u5c31\u662f\u5c06\u4e00\u4e2a key \u7ecf\u8fc7\u6563\u5217\u8ba1\u7b97\u5747\u5300\u7684\u6620\u5c04\u5230\u6563\u5217\u8868\u4e0a\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230529\/1685336809647432e91ee4a.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p><span style=\"font-size: 14px\"><span style=\"color: #dda52d\">\u56fe 2-18<\/span><\/span><\/p>\n<h2><span style=\"color: #515151\">2\u3001\u4fee\u70bc\u5fc3\u6cd5<\/span><\/h2>\n<p>Hash \u6570\u636e\u7c7b\u578b\u5e95\u5c42\u5b58\u50a8\u6570\u636e\u7ed3\u6784\u5b9e\u9645\u4e0a\u6709\u4e24\u79cd\u3002<\/p>\n<ol>\n<li>dict \u7ed3\u6784\u3002<\/li>\n<li>\u5728 7.0 \u7248\u672c\u4e4b\u524d\u4f7f\u7528 ziplist\uff0c\u4e4b\u540e\u88ab listpack \u4ee3\u66ff\u3002<\/li>\n<\/ol>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\u4f7f\u7528 dict \u6570\u636e\u7ed3\u6784\u5b58\u50a8\u6570\u636e\uff0c\u6bcf\u4e2a&nbsp;field-value pairs&nbsp;\u6784\u6210\u4e00\u4e2a dictEntry \u8282\u70b9\u6765\u4fdd\u5b58\u3002<\/p>\n<p>\u53ea\u6709\u540c\u65f6\u6ee1\u8db3\u4ee5\u4e0b\u4e24\u4e2a\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u4f7f\u7528 listpack\uff087.0 \u7248\u672c\u4e4b\u524d\u4f7f\u7528 ziplist\uff09\u6570\u636e\u7ed3\u6784\u6765\u4ee3\u66ff dict \u5b58\u50a8\uff0c&nbsp;\u628a key-value \u952e\u503c\u5bf9\u6309\u7167 field \u5728\u524d value \u5728\u540e\uff0c\u7d27\u5bc6\u76f8\u8fde\u7684\u65b9\u5f0f\u653e\u5230\u4e00\u6b21\u628a\u6bcf\u4e2a\u952e\u503c\u5bf9\u653e\u5230\u5217\u8868\u7684\u8868\u5c3e\u3002<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u952e\u503c\u5bf9\u4e2d\u7684 field \u548c value \u7684\u5b57\u7b26\u4e32\u5b57\u8282\u5927\u5c0f\u90fd\u5c0f\u4e8ehash-max-listpack-value&nbsp;\u914d\u7f6e\u7684\u503c\uff08\u9ed8\u8ba4 64\uff09\u3002<\/li>\n<li>field-value pairs \u952e\u503c\u5bf9\u6570\u91cf\u5c0f\u4e8e&nbsp;hash-max-listpack-entries\u914d\u7f6e\u7684\u503c\uff08\u9ed8\u8ba4 512\uff09\u3002<\/li>\n<\/ul>\n<p>\u6bcf\u6b21\u5411\u6563\u5217\u8868\u5199\u6570\u636e\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u8c03\u7528&nbsp;t_hash.c&nbsp;\u4e2d\u7684hashTypeConvertListpack()\u51fd\u6570\u6765\u5224\u65ad\u662f\u5426\u9700\u8981\u8f6c\u6362\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u5f53\u63d2\u5165\u548c\u4fee\u6539\u7684\u6570\u636e\u4e0d\u6ee1\u8db3\u4ee5\u4e0a\u4e24\u4e2a\u6761\u4ef6\u65f6\uff0c\u5c31\u628a\u6563\u5217\u8868\u5e95\u5c42\u5b58\u50a8\u7ed3\u6784\u8f6c\u6362\u6210&nbsp;dict\u7ed3\u6784\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u80fd\u7531 dict \u9000\u5316\u6210 listpack\u3002<\/p>\n<p>\u867d\u7136\u4f7f\u7528\u4e86 listpack \u5c31\u65e0\u6cd5\u5b9e\u73b0 O(1) \u65f6\u95f4\u590d\u6742\u5ea6\u64cd\u4f5c\u6570\u636e\uff0c\u4f46\u662f\u4f7f\u7528 listpack \u80fd\u5927\u5927\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u800c\u4e14\u6570\u636e\u91cf\u6bd4\u8f83\u5c0f\uff0c\u6027\u80fd\u5e76\u4e0d\u662f\u6709\u592a\u5927\u5dee\u5f02\u3002<\/p>\n<p>\u4e3a\u4e86\u5bf9\u4e0a\u5c42\u5c4f\u853d\u6563\u5217\u8868\u5e95\u5c42\u4f7f\u7528\u4e86\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u5b58\u50a8\uff0c\u6240\u4ee5\u62bd\u8c61\u4e86\u4e00\u4e2a hashTypeIterator \u8fed\u4ee3\u5668\u6765\u5b9e\u73b0\u6563\u5217\u8868\u7684\u67e5\u8be2\u3002<\/p>\n<p>Hashes \u6570\u636e\u7c7b\u578b\u4f7f\u7528 listpack \u4f5c\u4e3a\u5b58\u50a8\u6570\u636e\u65f6\u7684\u60c5\u51b5\uff0c\u5982\u56fe 2-19 \u6240\u793a\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230529\/1685336809647432e975cfb.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p>\u56fe 2-19<\/p>\n<p>listpack \u6570\u636e\u7ed3\u6784\u5728\u4e4b\u524d\u7684\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\uff0c \u63a5\u4e0b\u6765\u5e26\u4f60\u63ed\u79d8 dict \u5230\u5e95\u957f\u5565\u6837\u3002<\/p>\n<p>Redis \u6570\u636e\u5e93\u5c31\u662f\u4e00\u4e2a\u5168\u5c40\u6563\u5217\u8868\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u53ea\u4f1a\u4f7f\u7528&nbsp;<code>ht_table[0]<\/code>\u6563\u5217\u8868\uff0c\u56fe 2-20 \u662f\u4e00\u4e2a\u6ca1\u6709\u8fdb\u884c rehash \u72b6\u6001\u4e0b\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230529\/1685336809647432e9e6a9e.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p>\u56fe 2-20<\/p>\n<p>dict \u5b57\u5178\u5728\u6e90\u4ee3\u7801&nbsp;dict.h\u4e2d\u4f7f\u7528 dict \u7ed3\u6784\u4f53\u8868\u793a\u3002<\/p>\n<p><code>struct dict { dictType *type; \/\/ \u771f\u6b63\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\uff0c\u5206\u522b\u5b58\u653e\u4e24\u4e2a\u6307\u9488 dictEntry **ht_table[2]; unsigned long ht_used[2]; long rehashidx; int16_t pauserehash; signed char ht_size_exp[2]; };<\/code><\/p>\n<ul>\n<li>dictType *type\uff0c\u5b58\u653e\u51fd\u6570\u7684\u7ed3\u6784\u4f53\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e9b\u51fd\u6570\u6307\u9488\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5b9e\u73b0 dict \u7684 key \u548c value \u5b58\u653e\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u3002<\/li>\n<li>\u91cd\u70b9\u770b&nbsp;dictEntry **ht_table[2]\uff0c\u5b58\u653e\u4e86\u4e24\u4e2a dictEntry \u7684\u4e8c\u7ea7\u6307\u9488\uff0c\u6307\u9488\u5206\u522b\u6307\u5411\u4e86\u4e00\u4e2a dictEntry \u6307\u9488\u7684\u6570\u7ec4\u3002<\/li>\n<li>ht_used[2]\uff0c\u8bb0\u5f55\u6bcf\u4e2a\u6563\u5217\u8868\u4f7f\u7528\u4e86\u591a\u5c11\u69fd\u4f4d\uff08\u6bd4\u5982\u6570\u7ec4\u957f\u5ea6 32\uff0c\u4f7f\u7528\u4e86 12\uff09\u3002<\/li>\n<li>rehashidx\uff0c\u7528\u4e8e\u6807\u8bb0\u662f\u5426\u6b63\u5728\u6267\u884c rehash \u64cd\u4f5c\uff0c-1 \u8868\u793a\u6ca1\u6709\u8fdb\u884c rehash\u3002\u5982\u679c\u6b63\u5728\u6267\u884c rehash\uff0c\u90a3\u4e48\u5176\u503c\u8868\u793a\u5f53\u524d rehash \u64cd\u4f5c\u6267\u884c\u7684 ht_table[0] \u6563\u5217\u8868 dictEntry \u6570\u7ec4\u7684\u7d22\u5f15\u3002<\/li>\n<li>pauserehash&nbsp;\u8868\u793a rehash \u7684\u72b6\u6001\uff0c\u5927\u4e8e 0 \u65f6\u8868\u793a rehash \u6682\u505c\u4e86\uff0c\u5c0f\u4e8e 0 \u8868\u793a\u51fa\u9519\u4e86\u3002<\/li>\n<\/ul>\n<p>\u7ee7\u7eed\u770b&nbsp;dictEntry\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f dictEntry \u7c7b\u578b\uff0c\u5c31\u662f\u8fd9\u73a9\u610f\u5b58\u653e\u4e86\u952e\u503c\u5bf9\uff0c\u8868\u793a\u5b57\u5178\u7684\u4e00\u4e2a\u8282\u70b9\u3002<\/p>\n<p><code>typedef struct dictEntry { void *key; union { void *val; uint64_t u64; int64_t s64; double d; } v; struct dictEntry *next; } dictEntry;<\/code><\/p>\n<ul>\n<li>*key\u6307\u9488\u6307\u5411\u952e\u503c\u5bf9\u4e2d\u7684\u952e\uff0c\u5b9e\u9645\u4e0a\u6307\u5411\u4e00\u4e2a SDS \u5b9e\u4f8b\u3002<\/li>\n<li>v\u662f\u4e00\u4e2a union \u8054\u5408\u4f53\uff0c\u8868\u793a\u952e\u503c\u5bf9\u4e2d\u7684\u503c\uff0c\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u5b57\u6bb5\u6709\u503c\uff0c\u7528\u8054\u5408\u4f53\u7684\u76ee\u662f\u8282\u7701\u5185\u5b58\u3002<\/li>\n<\/ul>\n<ul>\n<li>*val&nbsp;\u5982\u679c\u503c\u662f\u975e\u6570\u5b57\u7c7b\u578b\uff0c\u90a3\u5c31\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u5b58\u50a8\u3002<\/li>\n<li>uint64_t u64\uff0c\u503c\u662f\u65e0\u7b26\u53f7\u6574\u6570\u7684\u65f6\u5019\u4f7f\u7528\u8fd9\u4e2a\u5b57\u6bb5\u5b58\u50a8\u3002<\/li>\n<li>int64_t s64\uff0c\u503c\u662f\u6709\u7b26\u53f7\u6574\u6570\u65f6\uff0c\u4f7f\u7528\u8be5\u5b57\u6bb5\u5b58\u50a8\u3002<\/li>\n<li>double d\uff0c\u503c\u662f\u6d6e\u70b9\u6570\u662f\uff0c\u4f7f\u7528\u8be5\u5b57\u6bb5\u5b58\u50a8\u3002<\/li>\n<\/ul>\n<ul>\n<li>*next\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u6307\u9488\uff0c\u5f53\u6563\u5217\u8868\u6570\u636e\u589e\u52a0\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e0d\u540c\u7684 key \u5f97\u5230\u7684\u54c8\u5e0c\u503c\u76f8\u7b49\uff0c\u4e5f\u5c31\u662f\u8bf4\u591a\u4e2a key \u5bf9\u5e94\u5728\u4e00\u4e2a\u54c8\u5e0c\u6876\u91cc\u9762\uff0c\u8fd9\u5c31\u662f\u54c8\u5e0c\u51b2\u7a81\u3002Redis \u4f7f\u7528\u62c9\u94fe\u6cd5\uff0c\u4e5f\u5c31\u662f\u7528\u94fe\u8868\u5c06\u6570\u636e\u4e32\u8d77\u6765\u3002<\/li>\n<\/ul>\n<blockquote style=\"margin-top: 5px;margin-bottom: 5px;padding-left: 1em;margin-left: 0px;opacity: 0.6\">\n<p>MySQL\uff1a\u201c\u4e3a\u5565 ht_table[2] \u5b58\u653e\u4e86\u4e24\u4e2a\u6307\u5411\u6563\u5217\u8868\u7684\u6307\u9488\uff1f\u7528\u4e00\u4e2a\u6563\u5217\u8868\u4e0d\u5c31\u591f\u4e86\u4e48\u3002\u201d<\/p>\n<\/blockquote>\n<p>\u9ed8\u8ba4\u4f7f\u7528&nbsp;ht_table [0]&nbsp;\u8fdb\u884c\u8bfb\u5199\u6570\u636e\uff0c\u5f53\u6563\u5217\u8868\u7684\u6570\u636e\u8d8a\u6765\u8d8a\u591a\u7684\u65f6\u5019\uff0c\u54c8\u5e0c\u51b2\u7a81\u4e25\u91cd\u4f1a\u51fa\u73b0\u54c8\u5e0c\u6876\u7684\u94fe\u8868\u6bd4\u8f83\u957f\uff0c\u5bfc\u81f4\u67e5\u8be2\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<p>\u6211\u4e3a\u4e86\u552f\u5feb\u4e0d\u7834\u60f3\u4e86\u4e00\u4e2a\u6cd5\u5b50\uff0c\u5f53\u6563\u5217\u8868\u4fdd\u5b58\u7684\u952e\u503c\u5bf9\u592a\u591a\u6216\u8005\u592a\u5c11\u7684\u65f6\u5019\uff0c\u9700\u8981\u901a\u8fc7 rehash\uff08\u91cd\u65b0\u6563\u5217\uff09\u5bf9\u6563\u5217\u8868\u8fdb\u884c\u6269\u5bb9\u6216\u8005\u7f29\u5bb9\u3002<\/p>\n<h4>\u6269\u5bb9\u548c\u7f29\u5bb9<\/h4>\n<ol>\n<li>\u4e3a\u4e86\u9ad8\u6027\u80fd\uff0c\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\uff0c\u6211\u4f1a\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u7b49\u4e8e&nbsp;ht_used[0] * 2\u7684\u6563\u5217\u8868&nbsp;ht_table[1]\uff0c\u4e5f\u5c31\u662f\u6bcf\u6b21\u6269\u5bb9\u65f6\u6839\u636e\u6563\u5217\u8868&nbsp;ht_table [0]\u5df2\u4f7f\u7528\u7a7a\u95f4\u6269\u5927\u4e00\u500d\u521b\u5efa\u4e00\u4e2a\u65b0\u6563\u5217\u8868ht_table [1]\u3002\u53cd\u4e4b\uff0c\u5982\u679c\u662f\u7f29\u5bb9\u64cd\u4f5c\uff0c\u5c31\u6839\u636eht_table [0]\u5df2\u4f7f\u7528\u7a7a\u95f4\u7f29\u5c0f\u4e00\u500d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6563\u5217\u8868\u3002<\/li>\n<li>\u91cd\u65b0\u8ba1\u7b97\u952e\u503c\u5bf9\u7684\u54c8\u5e0c\u503c\uff0c\u5f97\u5230\u8fd9\u4e2a\u952e\u503c\u5bf9\u5728\u65b0\u6563\u5217\u8868&nbsp;ht_table [1]\u7684\u6876\u4f4d\u7f6e\uff0c\u5c06\u952e\u503c\u5bf9\u8fc1\u79fb\u5230\u65b0\u7684\u6563\u5217\u8868\u4e0a\u3002<\/li>\n<li>\u6240\u6709\u952e\u503c\u5bf9\u8fc1\u79fb\u5b8c\u6210\u540e\uff0c\u4fee\u6539\u6307\u9488\uff0c\u91ca\u653e\u7a7a\u95f4\u3002\u5177\u4f53\u662f\u628a&nbsp;ht_table[0]\u6307\u9488\u6307\u5411\u6269\u5bb9\u540e\u7684\u6563\u5217\u8868\uff0c\u56de\u6536\u539f\u6765\u5c0f\u7684\u6563\u5217\u8868\u5185\u5b58\u7a7a\u95f4\uff0cht_table[1]\u6307\u9488\u6307\u5411NULL\uff0c\u4e3a\u4e0b\u6b21\u6269\u5bb9\u6216\u8005\u7f29\u5bb9\u505a\u51c6\u5907\u3002<\/li>\n<\/ol>\n<blockquote style=\"margin-top: 5px;margin-bottom: 5px;padding-left: 1em;margin-left: 0px;opacity: 0.6\">\n<p>MySQL\uff1a\u201c\u4ec0\u4e48\u65f6\u5019\u4f1a\u89e6\u53d1\u6269\u5bb9\uff1f\u201d<\/p>\n<\/blockquote>\n<ol>\n<li>\u5f53\u524d\u6ca1\u6709\u6267\u884c&nbsp;BGSAVE\u6216\u8005&nbsp;BGREWRITEAOF\u547d\u4ee4\uff0c\u540c\u65f6\u8d1f\u8f7d\u56e0\u5b50\u5927\u4e8e\u7b49\u4e8e 1\u3002\u4e5f\u5c31\u662f\u5f53\u524d\u6ca1\u6709 RDB \u5b50\u8fdb\u7a0b\u548c AOF \u91cd\u5199\u5b50\u8fdb\u7a0b\u5728\u5de5\u4f5c\uff0c\u6bd5\u7adf\u8fd9\u4fe9\u64cd\u4f5c\u8fd8\u662f\u6bd4\u8f83\u5bb9\u6613\u5bf9\u6027\u80fd\u9020\u6210\u5f71\u54cd\u7684\uff0c\u5c31\u4e0d\u6269\u5bb9\u706b\u4e0a\u6d47\u6cb9\u4e86\u3002<\/li>\n<li>\u6b63\u5728\u6267\u884c&nbsp;BGSAVE\u6216\u8005&nbsp;BGREWRITEAOF\u547d\u4ee4\uff0c\u8d1f\u8f7d\u56e0\u5b50\u5927\u4e8e\u7b49\u4e8e 5\u3002\uff08\u8fd9\u65f6\u5019\u54c8\u5e0c\u51b2\u7a81\u592a\u4e25\u91cd\u4e86\uff0c\u518d\u4e0d\u89e6\u53d1\u6269\u5bb9\uff0c\u67e5\u8be2\u6548\u7387\u592a\u6162\u4e86\uff09\u3002<\/li>\n<\/ol>\n<p>\u8d1f\u8f7d\u56e0\u5b50 = \u6563\u5217\u8868\u5b58\u50a8 dictEntry \u8282\u70b9\u6570\u91cf \/ \u6563\u5217\u8868\u6876\u4e2a\u6570\u3002\u5b8c\u7f8e\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u54c8\u5e0c\u6876\u5b58\u50a8\u4e00\u4e2a dictEntry \u8282\u70b9\uff0c\u8fd9\u65f6\u5019\u8d1f\u8f7d\u56e0\u5b50 = 1\u3002<\/p>\n<blockquote style=\"margin-top: 5px;margin-bottom: 5px;padding-left: 1em;margin-left: 0px;opacity: 0.6\">\n<p>MySQL\uff1a\u201c\u9700\u8981\u8fc1\u79fb\u6570\u636e\u91cf\u5f88\u5927\uff0crehash \u64cd\u4f5c\u5c82\u4e0d\u662f\u4f1a\u957f\u65f6\u95f4\u963b\u585e\u4e3b\u7ebf\u7a0b\uff1f\u201d<\/p>\n<\/blockquote>\n<p>\u4e3a\u4e86\u9632\u6b62\u963b\u585e\u4e3b\u7ebf\u7a0b\u9020\u6210\u6027\u80fd\u95ee\u9898\uff0c\u6211\u5e76\u4e0d\u662f\u4e00\u6b21\u6027\u628a\u5168\u90e8\u7684 key \u8fc1\u79fb\uff0c\u800c\u662f\u5206\u591a\u6b21\uff0c\u5c06\u8fc1\u79fb\u64cd\u4f5c\u5206\u6563\u5230\u6bcf\u6b21\u8bf7\u6c42\u4e2d\uff0c\u907f\u514d\u96c6\u4e2d\u5f0f rehash \u9020\u6210\u957f\u65f6\u95f4\u963b\u585e\uff0c\u8fd9\u4e2a\u65b9\u5f0f\u53eb\u6e10\u8fdb\u5f0f rehash\u3002<\/p>\n<p>\u5728\u6267\u884c\u6e10\u8fdb\u5f0f rehash \u671f\u95f4\uff0cdict \u4f1a\u540c\u65f6\u4f7f\u7528&nbsp;ht_table[0]&nbsp;\u548c&nbsp;ht_table[1]\u4e24\u4e2a\u6563\u5217\u8868\uff0crehash \u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\u3002<\/p>\n<ol>\n<li>\u5c06&nbsp;rehashidx\u8bbe\u7f6e\u6210 0\uff0c\u8868\u793a rehash \u5f00\u59cb\u6267\u884c\u3002<\/li>\n<li>\u5728 rehash \u671f\u95f4\uff0c\u670d\u52a1\u7aef\u6bcf\u6b21\u5904\u7406\u5ba2\u6237\u7aef\u5bf9 dict \u6563\u5217\u8868\u6267\u884c\u6dfb\u52a0\u3001\u67e5\u627e\u3001\u5220\u9664\u6216\u8005\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0c\u9664\u4e86\u6267\u884c\u6307\u5b9a\u64cd\u4f5c\u4ee5\u5916\uff0c\u8fd8\u4f1a\u68c0\u67e5\u5f53\u524d dict \u662f\u5426\u5904\u4e8e rehash \u72b6\u6001\uff0c\u662f\u7684\u8bdd\u5c31\u628a\u6563\u5217\u8868ht_table[0]\u4e0a\u7d22\u5f15\u4f4d\u7f6e\u4e3a&nbsp;rehashidx&nbsp;\u7684\u6876\u7684\u94fe\u8868\u7684\u6240\u6709\u952e\u503c\u5bf9 rehash \u5230\u6563\u5217\u8868&nbsp;ht_table[1]\u4e0a\uff0c\u8fd9\u4e2a\u54c8\u5e0c\u6876\u7684\u6570\u636e\u8fc1\u79fb\u5b8c\u6210\uff0c\u5c31\u628a&nbsp;rehashidx&nbsp;\u7684\u503c\u52a0 1\uff0c\u8868\u793a\u4e0b\u4e00\u6b21\u8981\u8fc1\u79fb\u7684\u6876\u6240\u5728\u4f4d\u7f6e\u3002<\/li>\n<li>\u5f53\u6240\u6709\u7684\u952e\u503c\u5bf9\u8fc1\u79fb\u5b8c\u6210\u540e\uff0c\u5c06&nbsp;rehashidx\u8bbe\u7f6e\u6210 -1\uff0c\u8868\u793a rehash \u64cd\u4f5c\u5df2\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<blockquote style=\"margin-top: 5px;margin-bottom: 5px;padding-left: 1em;margin-left: 0px;opacity: 0.6\">\n<p>MySQL\uff1a\u201crehash \u8fc7\u7a0b\u4e2d\uff0c\u5b57\u5178\u7684\u5220\u9664\u3001\u67e5\u627e\u3001\u66f4\u65b0\u548c\u6dfb\u52a0\u64cd\u4f5c\uff0c\u8981\u4ece\u4e24\u4e2a ht_table \u90fd\u641e\u4e00\u904d\u4e48\uff1f\u201d<\/p>\n<\/blockquote>\n<p>\u5220\u9664\u3001\u4fee\u6539\u548c\u67e5\u627e\u53ef\u80fd\u4f1a\u5728\u4e24\u4e2a\u6563\u5217\u8868\u8fdb\u884c\uff0c\u7b2c\u4e00\u4e2a\u6563\u5217\u8868\u6ca1\u627e\u5230\u5c31\u5230\u7b2c\u4e8c\u4e2a\u6563\u5217\u8868\u8fdb\u884c\u67e5\u627e\u3002\u4f46\u662f\u589e\u52a0\u64cd\u4f5c\u53ea\u4f1a\u5728\u65b0\u7684\u6563\u5217\u8868\u4e0a\u8fdb\u884c\u3002<\/p>\n<blockquote style=\"margin-top: 5px;margin-bottom: 5px;padding-left: 1em;margin-left: 0px;opacity: 0.6\">\n<p>MySQL\uff1a\u201c\u5982\u679c\u8bf7\u6c42\u6bd4\u8f83\u5c11\uff0c\u5c82\u4e0d\u662f\u4f1a\u5f88\u957f\u65f6\u95f4\u90fd\u8981\u4f7f\u7528\u4e24\u4e2a\u6563\u5217\u8868\u3002\u201d<\/p>\n<\/blockquote>\n<p>\u597d\u95ee\u9898\uff0c\u5728 Redis Server \u521d\u59cb\u5316\u65f6\uff0c\u4f1a\u6ce8\u518c\u4e00\u4e2a\u65f6\u95f4\u4e8b\u4ef6\uff0c\u5b9a\u65f6\u6267\u884c&nbsp;serverCron&nbsp;\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u542b rehash \u64cd\u4f5c\u7528\u4e8e\u8f85\u52a9\u8fc1\u79fb\uff0c\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>serverCron&nbsp;\u51fd\u6570\u9664\u4e86\u505a rehash \u4ee5\u5916\uff0c\u4e3b\u8981\u5904\u7406\u5982\u4e0b\u5de5\u4f5c\u3002<\/p>\n<ul>\n<li>\u8fc7\u671f key \u5220\u9664\u3002<\/li>\n<li>\u76d1\u63a7\u670d\u52a1\u8fd0\u884c\u72b6\u6001\u3002<\/li>\n<li>\u66f4\u65b0\u7edf\u8ba1\u6570\u636e\u3002<\/li>\n<li>\u6e10\u8fdb\u5f0f rehash\u3002<\/li>\n<li>\u89e6\u53d1 BGSAVE \/ AOF rewrite \u4ee5\u53ca\u505c\u6b62\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u5904\u7406\u5ba2\u6237\u7aef\u8d85\u65f6\u3002<\/li>\n<li>&#8230;&#8230;<\/li>\n<\/ul>\n<p><span style=\"color: #3a3a3a\">\u662f\u4e0d\u662f\u5f88\u8d34\u5fc3\uff0c\u65e2\u80fd\u4fdd\u8bc1\u6027\u80fd\uff0c\u53c8\u80fd\u907f\u514d\u5185\u5b58\u6d6a\u8d39\u3002\u597d\u4e86\uff0c\u4eca\u5929\u6563\u5217\u8868\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u539f\u7406\u5c31\u5230\u8fd9\u91cc\u3002\u540e\u9762\u6211\u5c06\u7ed9\u5927\u5bb6\u5206\u4eab\u5982\u4f55\u4f7f\u7528 Hash \u5b9e\u73b0\u8d2d\u7269\u8f66\u529f\u80fd\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5ea6\u56fe\u89e3 Redis Hash\uff08&#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-201845","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201845","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=201845"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201845\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}