{"id":201289,"date":"2025-05-10T08:56:13","date_gmt":"2025-05-10T00:56:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201289\/"},"modified":"2025-05-10T08:56:13","modified_gmt":"2025-05-10T00:56:13","slug":"redis%e7%9a%84%e5%85%ad%e7%a7%8d%e5%ba%95%e5%b1%82%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201289\/","title":{"rendered":"Redis\u7684\u516d\u79cd\u5e95\u5c42\u6570\u636e\u7ed3\u6784(\u5c0f\u7ed3)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u7684\u516d\u79cd\u5e95\u5c42\u6570\u636e\u7ed3\u6784(\u5c0f\u7ed3)<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis\u7684\u516d\u79cd\u5e95\u5c42\u6570\u636e\u7ed3\u6784(\u5c0f\u7ed3)\u300b\uff0c\u6d89\u53ca\u5230Redis\u5e95\u5c42\u6570\u636e\u7ed3\u6784\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<h2>1\u3001\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32\uff08SDS\uff09<\/h2>\n<p>Redis \u867d\u7136\u662f\u7528 C \u8bed\u8a00\u5199\u7684\uff0c\u4f46Redis\u6ca1\u6709\u76f4\u63a5\u4f7f\u7528C\u8bed\u8a00\u4f20\u7edf\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff08\u4ee5\u7a7a\u5b57\u7b26 \u2018\\0\u2019 \u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\uff09\uff0c\u4e8c\u662f\u81ea\u5df1\u6784\u5efa\u4e86\u4e00\u79cd\u540d\u4e3a\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32\uff08simple dynamic string\uff0cSDS\uff09\u7684\u62bd\u8c61\u7c7b\u578b\uff0c\u5e76\u5c06 SDS \u4f5c\u4e3a Redis\u7684\u9ed8\u8ba4\u5b57\u7b26\u4e32\u8868\u793a\u3002<br \/>\u5728Redis\u91cc\u9762\uff0cC\u5b57\u7b26\u4e32\u53ea\u4f1a\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff08string literal\uff09\u7528\u5728\u4e00\u4e9b\u65e0\u987b\u5bf9\u5b57\u7b26\u4e32\u503c\u8fdb\u884c\u4fee\u6539\u7684\u5730\u65b9\uff0c\u6bd4\u5982\u6253\u5370\u65e5\u5fd7\u3002<\/p>\n<p><strong>SDS \u7684\u5b9a\u4e49\uff1a<\/strong><\/p>\n<pre>struct sdshdr{\n     \/\/\u8bb0\u5f55buf\u6570\u7ec4\u4e2d\u5df2\u4f7f\u7528\u5b57\u8282\u7684\u6570\u91cf\n     \/\/\u7b49\u4e8e SDS \u6240\u4fdd\u5b58\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\n     int len;\n     \n     \/\/\u8bb0\u5f55 buf \u6570\u7ec4\u4e2d\u672a\u4f7f\u7528\u5b57\u8282\u7684\u6570\u91cf\n     int free;\n     \n     \/\/\u5b57\u8282\u6570\u7ec4\uff0c\u7528\u4e8e\u4fdd\u5b58\u5b57\u7b26\u4e32\n     char buf[];\n}\n<\/pre>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256365963b14bcbe59a6.jpg\" class=\"aligncenter\"><\/p>\n<p>\u2460 free \u5c5e\u6027\u7684\u503c\u4e3a 0\uff0c\u8868\u793a\u8fd9\u4e2aSDS\u6ca1\u6709\u5206\u914d\u4efb\u4f55\u672a\u4f7f\u7528\u7684\u7a7a\u95f4\u3002<br \/>\u2461 len \u5c5e\u6027\u7684\u503c\u4e3a 5\uff0c\u8868\u793a\u8fd9\u4e2aSDS\u4fdd\u5b58\u4e86\u4e00\u4e2a\u4e94\u5b57\u8282\u957f\u7684\u5b57\u7b26\u4e32\u3002<br \/>\u2462 buf \u5c5e\u6027\u662f\u4e00\u4e2achar \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u524d\u4e94\u4e2a\u5b57\u8282\u5206\u522b\u4fdd\u5b58\u4e86 \u2018R\u2019\u3001\u2018e\u2019\u3001<br \/>\u2018d\u2019\u3001\u2018i\u2019\u3001\u2018s\u2019 \u4e94\u4e2a\u5b57\u7b26\uff0c\u800c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u5219\u4fdd\u5b58\u4e86\u7a7a\u5b57\u7b26 \u2018\\0\u2019 \u3002<\/p>\n<p>\uff08SDS\u9075\u5faaC\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u60ef\u4f8b\uff0c\u4fdd\u5b58\u7a7a\u5b57\u7b26\u7684 1 \u5b57\u8282\u7a7a\u95f4\u4e0d\u8ba1\u7b97\u5728SDS\u7684 len\u5c5e\u6027\u91cc\u9762\uff0c\u5e76\u4e14\u4e3a\u7a7a\u5b57\u7b26\u5206\u914d\u989d\u5916\u7684 1 \u5b57\u8282\u7a7a\u95f4\uff0c\u4ee5\u53ca\u6dfb\u52a0\u7a7a\u5b57\u7b26\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7b49\u64cd\u4f5c\uff0c\u90fd\u662f\u7531SDS \u51fd\u6570\u81ea\u52a8\u5b8c\u6210\u7684\uff0c\u6240\u6709\u8fd9\u4e2a\u7a7a\u5b57\u7b26\u5bf9\u4e8eSDS\u7684\u4f7f\u7528\u8005\u6765\u8bf4\u662f\u5b8c\u5168\u900f\u660e\u7684\u3002\u9075\u5faa\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u8fd9\u4e00\u60ef\u4f8b\u7684\u597d\u5904\u662f\uff0cSDS\u53ef\u4ee5\u76f4\u63a5\u91cd\u7528C\u5b57\u7b26\u4e32\u51fd\u6570\u5e93\u91cc\u9762\u7684\u51fd\u6570\u3002\uff09<\/p>\n<p>SDS \u4e0e C \u5b57\u6d6e\u4e32\u7684\u533a\u522b\uff1a<\/p>\n<p>\uff081\uff09\u5e38\u6570\u590d\u6742\u5ea6\u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6<br \/>\u56e0\u4e3a C \u5b57\u7b26\u4e32\u5e76\u4e0d\u8bb0\u5f55\u81ea\u8eab\u7684\u957f\u5ea6\u4fe1\u606f\uff0c \u6240\u4ee5\u4e3a\u4e86\u83b7\u53d6\u4e00\u4e2a C \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c \u7a0b\u5e8f\u5fc5\u987b\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c \u5bf9\u9047\u5230\u7684\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u8ba1\u6570\uff0c \u76f4\u5230\u9047\u5230\u4ee3\u8868\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u7a7a\u5b57\u7b26\u4e3a\u6b62\uff0c \u8fd9\u4e2a\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4e3a O(N) \u3002<\/p>\n<p>\u548c C \u5b57\u7b26\u4e32\u4e0d\u540c\uff0c \u56e0\u4e3a SDS \u5728 len \u5c5e\u6027\u4e2d\u8bb0\u5f55\u4e86 SDS \u672c\u8eab\u7684\u957f\u5ea6\uff0c \u6240\u4ee5\u83b7\u53d6\u4e00\u4e2a SDS \u957f\u5ea6\u7684\u590d\u6742\u5ea6\u4ec5\u4e3a O(1) \u3002<\/p>\n<p>\uff082\uff09\u675c\u7edd\u7f13\u51b2\u533a\u6ea2\u51fa<br \/>\u6211\u4eec\u77e5\u9053\u5728 C \u8bed\u8a00\u4e2d\u4f7f\u7528 strcat \u51fd\u6570\u6765\u8fdb\u884c\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\uff0c\u4e00\u65e6\u6ca1\u6709\u5206\u914d\u8db3\u591f\u957f\u5ea6\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5c31\u4f1a\u9020\u6210\u7f13\u51b2\u533a\u6ea2\u51fa\u3002\u800c\u5bf9\u4e8e SDS \u6570\u636e\u7c7b\u578b\uff0c\u5728\u8fdb\u884c\u5b57\u7b26\u4fee\u6539\u7684\u65f6\u5019\uff0c\u4f1a\u9996\u5148\u6839\u636e\u8bb0\u5f55\u7684 len \u5c5e\u6027\u68c0\u67e5\u5185\u5b58\u7a7a\u95f4\u662f\u5426\u6ee1\u8db3\u9700\u6c42\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u4f1a\u8fdb\u884c\u76f8\u5e94\u7684\u7a7a\u95f4\u6269\u5c55\uff0c\u7136\u540e\u5728\u8fdb\u884c\u4fee\u6539\u64cd\u4f5c\uff0c\u6240\u4ee5\u4e0d\u4f1a\u51fa\u73b0\u7f13\u51b2\u533a\u6ea2\u51fa\u3002<\/p>\n<p>\uff083\uff09\u51cf\u5c11\u4fee\u6539\u5b57\u7b26\u4e32\u7684\u5185\u5b58\u91cd\u65b0\u5206\u914d\u6b21\u6570<br \/>C\u8bed\u8a00\u7531\u4e8e\u4e0d\u8bb0\u5f55\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u5982\u679c\u8981\u4fee\u6539\u5b57\u7b26\u4e32\uff0c\u5fc5\u987b\u8981\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff08\u5148\u91ca\u653e\u518d\u7533\u8bf7\uff09\uff0c\u56e0\u4e3a\u5982\u679c\u6ca1\u6709\u91cd\u65b0\u5206\u914d\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u589e\u5927\u65f6\u4f1a\u9020\u6210\u5185\u5b58\u7f13\u51b2\u533a\u6ea2\u51fa\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u51cf\u5c0f\u65f6\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732\u3002<\/p>\n<p>\u800c\u5bf9\u4e8eSDS\uff0c\u7531\u4e8elen\u5c5e\u6027\u548cfree\u5c5e\u6027\u7684\u5b58\u5728\uff0c\u5bf9\u4e8e\u4fee\u6539\u5b57\u7b26\u4e32SDS\u5b9e\u73b0\u4e86\u7a7a\u95f4\u9884\u5206\u914d\u548c\u60f0\u6027\u7a7a\u95f4\u91ca\u653e\u4e24\u79cd\u7b56\u7565\uff1a<\/p>\n<p><strong>1\u3001\u7a7a\u95f4\u9884\u5206\u914d<\/strong>\uff1a\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7a7a\u95f4\u6269\u5c55\u7684\u65f6\u5019\uff0c\u6269\u5c55\u7684\u5185\u5b58\u6bd4\u5b9e\u9645\u9700\u8981\u7684\u591a\uff0c\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u8fde\u7eed\u6267\u884c\u5b57\u7b26\u4e32\u589e\u957f\u64cd\u4f5c\u6240\u9700\u7684\u5185\u5b58\u91cd\u5206\u914d\u6b21\u6570\u3002<\/p>\n<p><strong>2\u3001\u60f0\u6027\u7a7a\u95f4\u91ca\u653e<\/strong>\uff1a\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f29\u77ed\u64cd\u4f5c\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u7acb\u5373\u4f7f\u7528\u5185\u5b58\u91cd\u65b0\u5206\u914d\u6765\u56de\u6536\u7f29\u77ed\u540e\u591a\u4f59\u7684\u5b57\u8282\uff0c\u800c\u662f\u4f7f\u7528 free \u5c5e\u6027\u5c06\u8fd9\u4e9b\u5b57\u8282\u7684\u6570\u91cf\u8bb0\u5f55\u4e0b\u6765\uff0c\u7b49\u5f85\u540e\u7eed\u4f7f\u7528\u3002\uff08\u5f53\u7136SDS\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684API\uff0c\u5f53\u6211\u4eec\u6709\u9700\u8981\u65f6\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u91ca\u653e\u8fd9\u4e9b\u672a\u4f7f\u7528\u7684\u7a7a\u95f4\uff09\u3002<\/p>\n<p>\uff084\uff09\u4e8c\u8fdb\u5236\u5b89\u5168<br \/>\u56e0\u4e3aC\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u4f5c\u4e3a\u5b57\u7b26\u4e32\u7ed3\u675f\u7684\u6807\u8bc6\uff0c\u800c\u5bf9\u4e8e\u4e00\u4e9b\u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u5982\u56fe\u7247\u7b49\uff09\uff0c\u5185\u5bb9\u53ef\u80fd\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64C\u5b57\u7b26\u4e32\u65e0\u6cd5\u6b63\u786e\u5b58\u53d6\uff1b\u800c\u6240\u6709 SDS \u7684API \u90fd\u662f\u4ee5\u5904\u7406\u4e8c\u8fdb\u5236\u7684\u65b9\u5f0f\u6765\u5904\u7406 buf \u91cc\u9762\u7684\u5143\u7d20\uff0c\u5e76\u4e14 SDS \u4e0d\u662f\u4ee5\u7a7a\u5b57\u7b26\u4e32\u6765\u5224\u65ad\u662f\u5426\u7ed3\u675f\uff0c\u800c\u662f\u4ee5 len \u5c5e\u6027\u8868\u793a\u7684\u957f\u5ea6\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u7ed3\u675f\u3002<\/p>\n<p>\uff085\uff09\u517c\u5bb9\u90e8\u5206 C \u5b57\u7b26\u4e32\u51fd\u6570<br \/>\u867d\u7136 SDS \u7684 API \u90fd\u662f\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\uff0c \u4f46\u5b83\u4eec\u4e00\u6837\u9075\u5faa C \u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u60ef\u4f8b\uff1a \u8fd9\u4e9b API \u603b\u4f1a\u5c06 SDS \u4fdd\u5b58\u7684\u6570\u636e\u7684\u672b\u5c3e\u8bbe\u7f6e\u4e3a\u7a7a\u5b57\u7b26\uff0c \u5e76\u4e14\u603b\u4f1a\u5728\u4e3a buf \u6570\u7ec4\u5206\u914d\u7a7a\u95f4\u65f6\u591a\u5206\u914d\u4e00\u4e2a\u5b57\u8282\u6765\u5bb9\u7eb3\u8fd9\u4e2a\u7a7a\u5b57\u7b26\uff0c \u8fd9\u662f\u4e3a\u4e86\u8ba9\u90a3\u4e9b\u4fdd\u5b58\u6587\u672c\u6570\u636e\u7684 SDS \u53ef\u4ee5\u91cd\u7528\u4e00\u90e8\u5206 \u5e93\u5b9a\u4e49\u7684\u51fd\u6570\u3002 <\/p>\n<p>\uff086\uff09\u603b<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366063b14bcc51b8b.jpg\" class=\"aligncenter\"><\/p>\n<h2>2\u3001\u94fe\u8868<\/h2>\n<p>\u4f5c\u4e3a\u4e00\u79cd\u5e38\u7528\u6570\u636e\u7ed3\u6784\uff0c\u94fe\u8868\u5185\u7f6e\u5728\u5f88\u591a\u9ad8\u7ea7\u7684\u7f16\u7a0b\u8bed\u8a00\u91cc\u9762\uff0c\u56e0\u4e3aRedis\u4f7f\u7528\u7684 C \u8bed\u8a00\u5e76\u6ca1\u6709\u5185\u7f6e\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5 Redis \u6784\u5efa\u4e86\u81ea\u5df1\u7684\u94fe\u8868\u7ed3\u6784\u3002<\/p>\n<p>\u6bcf\u4e2a\u94fe\u8868\u8282\u70b9\u4f7f\u7528\u4e00\u4e2a adlist.h\/listNode \u7ed3\u6784\u6765\u8868\u793a\uff1a<\/p>\n<pre>typedef struct listNode {\n\n    \/\/ \u524d\u7f6e\u8282\u70b9\n    struct listNode *prev;\n\n    \/\/ \u540e\u7f6e\u8282\u70b9\n    struct listNode *next;\n\n    \/\/ \u8282\u70b9\u7684\u503c\n    void *value;\n\n} listNode;\n<\/pre>\n<p>\u591a\u4e2a listNode \u53ef\u4ee5\u901a\u8fc7 prev \u548c next \u6307\u9488\u7ec4\u6210\u53cc\u7aef\u94fe\u8868\uff0c \u5982\u56fe 3-1 \u6240\u793a\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366063b14bccb77d5.jpg\" class=\"aligncenter\"><\/p>\n<p>\u867d\u7136\u4ec5\u4ec5\u4f7f\u7528\u591a\u4e2a listNode \u7ed3\u6784\u5c31\u53ef\u4ee5\u7ec4\u6210\u94fe\u8868\uff0c \u4f46\u4f7f\u7528 adlist.h\/list \u6765\u6301\u6709\u94fe\u8868\u7684\u8bdd\uff0c \u64cd\u4f5c\u8d77\u6765\u4f1a\u66f4\u65b9\u4fbf\uff1a<\/p>\n<pre>typedef struct list {\n\n    \/\/ \u8868\u5934\u8282\u70b9\n    listNode *head;\n\n    \/\/ \u8868\u5c3e\u8282\u70b9\n    listNode *tail;\n\n    \/\/ \u94fe\u8868\u6240\u5305\u542b\u7684\u8282\u70b9\u6570\u91cf\n    unsigned long len;\n\n    \/\/ \u8282\u70b9\u503c\u590d\u5236\u51fd\u6570\n    void *(*dup)(void *ptr);\n\n    \/\/ \u8282\u70b9\u503c\u91ca\u653e\u51fd\u6570\n    void (*free)(void *ptr);\n\n    \/\/ \u8282\u70b9\u503c\u5bf9\u6bd4\u51fd\u6570\n    int (*match)(void *ptr, void *key);\n\n} list;\n<\/pre>\n<p>list \u7ed3\u6784\u4e3a\u94fe\u8868\u63d0\u4f9b\u4e86\u8868\u5934\u6307\u9488 head \u3001\u8868\u5c3e\u6307\u9488 tail \uff0c \u4ee5\u53ca\u94fe\u8868\u957f\u5ea6\u8ba1\u6570\u5668 len \uff0c \u800c dup \u3001 free \u548c match \u6210\u5458\u5219\u662f\u7528\u4e8e\u5b9e\u73b0\u591a\u6001\u94fe\u8868\u6240\u9700\u7684\u7c7b\u578b\u7279\u5b9a\u51fd\u6570\uff1a<br \/>\u2460 dup \u51fd\u6570\u7528\u4e8e\u590d\u5236\u94fe\u8868\u8282\u70b9\u6240\u4fdd\u5b58\u7684\u503c\uff1b<br \/>\u2461 free \u51fd\u6570\u7528\u4e8e\u91ca\u653e\u94fe\u8868\u8282\u70b9\u6240\u4fdd\u5b58\u7684\u503c\uff1b<br \/>\u2462 match \u51fd\u6570\u5219\u7528\u4e8e\u5bf9\u6bd4\u94fe\u8868\u8282\u70b9\u6240\u4fdd\u5b58\u7684\u503c\u548c\u53e6\u4e00\u4e2a\u8f93\u5165\u503c\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<p>\u56fe 3-2 \u662f\u7531\u4e00\u4e2a list \u7ed3\u6784\u548c\u4e09\u4e2a listNode \u7ed3\u6784\u7ec4\u6210\u7684\u94fe\u8868\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366163b14bcd1c539.jpg\" class=\"aligncenter\"><\/p>\n<p>Redis \u7684\u94fe\u8868\u5b9e\u73b0\u7684\u7279\u6027\u53ef\u4ee5\u603b\u7ed3\u5982\u4e0b\uff1a<br \/>\u2460 \u53cc\u7aef\uff1a \u94fe\u8868\u8282\u70b9\u5e26\u6709 prev \u548c next \u6307\u9488\uff0c \u83b7\u53d6\u67d0\u4e2a\u8282\u70b9\u7684\u524d\u7f6e\u8282\u70b9\u548c\u540e\u7f6e\u8282\u70b9\u7684\u590d\u6742\u5ea6\u90fd\u662f O(1) \u3002<br \/>\u2461 \u65e0\u73af\uff1a \u8868\u5934\u8282\u70b9\u7684 prev \u6307\u9488\u548c\u8868\u5c3e\u8282\u70b9\u7684 next \u6307\u9488\u90fd\u6307\u5411 NULL \uff0c \u5bf9\u94fe\u8868\u7684\u8bbf\u95ee\u4ee5 NULL \u4e3a\u7ec8\u70b9\u3002<br \/>\u2462 \u5e26\u8868\u5934\u6307\u9488\u548c\u8868\u5c3e\u6307\u9488\uff1a \u901a\u8fc7 list \u7ed3\u6784\u7684 head \u6307\u9488\u548c tail \u6307\u9488\uff0c \u7a0b\u5e8f\u83b7\u53d6\u94fe\u8868\u7684\u8868\u5934\u8282\u70b9\u548c\u8868\u5c3e\u8282\u70b9\u7684\u590d\u6742\u5ea6\u4e3a O(1) \u3002<br \/>\u2463 \u5e26\u94fe\u8868\u957f\u5ea6\u8ba1\u6570\u5668\uff1a \u7a0b\u5e8f\u4f7f\u7528 list \u7ed3\u6784\u7684 len \u5c5e\u6027\u6765\u5bf9 list \u6301\u6709\u7684\u94fe\u8868\u8282\u70b9\u8fdb\u884c\u8ba1\u6570\uff0c \u7a0b\u5e8f\u83b7\u53d6\u94fe\u8868\u4e2d\u8282\u70b9\u6570\u91cf\u7684\u590d\u6742\u5ea6\u4e3a O(1) \u3002<br \/>\u2464 \u591a\u6001\uff1a \u94fe\u8868\u8282\u70b9\u4f7f\u7528 void* \u6307\u9488\u6765\u4fdd\u5b58\u8282\u70b9\u503c\uff0c \u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7 list \u7ed3\u6784\u7684 dup \u3001 free \u3001 match \u4e09\u4e2a\u5c5e\u6027\u4e3a\u8282\u70b9\u503c\u8bbe\u7f6e\u7c7b\u578b\u7279\u5b9a\u51fd\u6570\uff0c \u6240\u4ee5\u94fe\u8868\u53ef\u4ee5\u7528\u4e8e\u4fdd\u5b58\u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<h2>3\u3001\u5b57\u5178<\/h2>\n<p>\u5b57\u5178\u53c8\u79f0\u4e3a\u7b26\u53f7\u8868\u6216\u8005\u5173\u8054\u6570\u7ec4\u3001\u6216\u6620\u5c04\uff08map\uff09\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u4fdd\u5b58\u952e\u503c\u5bf9\u7684\u62bd\u8c61\u6570\u636e\u7ed3\u6784\u3002\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u4e2a\u952e key \u90fd\u662f\u552f\u4e00\u7684\uff0c\u901a\u8fc7 key \u53ef\u4ee5\u5bf9\u503c\u6765\u8fdb\u884c\u67e5\u627e\u6216\u4fee\u6539\u3002C \u8bed\u8a00\u4e2d\u6ca1\u6709\u5185\u7f6e\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u7684\u5b9e\u73b0\uff0c\u6240\u4ee5\u5b57\u5178\u4f9d\u7136\u662f Redis \u81ea\u5df1\u6784\u5efa\u7684\u3002<\/p>\n<p>Redis \u7684\u5b57\u5178\u4f7f\u7528\u54c8\u5e0c\u8868\u4f5c\u4e3a\u5e95\u5c42\u5b9e\u73b0\uff0c \u4e00\u4e2a\u54c8\u5e0c\u8868\u91cc\u9762\u53ef\u4ee5\u6709\u591a\u4e2a\u54c8\u5e0c\u8868\u8282\u70b9\uff0c \u800c\u6bcf\u4e2a\u54c8\u5e0c\u8868\u8282\u70b9\u5c31\u4fdd\u5b58\u4e86\u5b57\u5178\u4e2d\u7684\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<h3>\u54c8\u5e0c\u8868<\/h3>\n<p>Redis \u5b57\u5178\u6240\u4f7f\u7528\u7684<strong>\u54c8\u5e0c\u8868<\/strong>\u7531 dict.h\/dictht \u7ed3\u6784\u5b9a\u4e49\uff1a<\/p>\n<pre>typedef struct dictht {\n\n    \/\/ \u54c8\u5e0c\u8868\u6570\u7ec4\n    dictEntry **table;\n\n    \/\/ \u54c8\u5e0c\u8868\u5927\u5c0f\n    unsigned long size;\n\n    \/\/ \u54c8\u5e0c\u8868\u5927\u5c0f\u63a9\u7801\uff0c\u7528\u4e8e\u8ba1\u7b97\u7d22\u5f15\u503c\n    \/\/ \u603b\u662f\u7b49\u4e8e size - 1\n    unsigned long sizemask;\n\n    \/\/ \u8be5\u54c8\u5e0c\u8868\u5df2\u6709\u8282\u70b9\u7684\u6570\u91cf\n    unsigned long used;\n\n} dictht;\n<\/pre>\n<p>\u56fe 4-1 \u5c55\u793a\u4e86\u4e00\u4e2a\u5927\u5c0f\u4e3a 4 \u7684\u7a7a\u54c8\u5e0c\u8868 \uff08\u6ca1\u6709\u5305\u542b\u4efb\u4f55\u952e\u503c\u5bf9\uff09\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366163b14bcd796ac.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u54c8\u5e0c\u8868\u8282\u70b9<\/h3>\n<p><strong>\u54c8\u5e0c\u8868\u8282\u70b9<\/strong>\u4f7f\u7528 dictEntry \u7ed3\u6784\u8868\u793a\uff0c \u6bcf\u4e2a dictEntry \u7ed3\u6784\u90fd\u4fdd\u5b58\u7740\u4e00\u4e2a\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>typedef struct dictEntry {\n\n    \/\/ \u952e\n    void *key;\n\n    \/\/ \u503c\n    union {\n        void *val;\n        uint64_t u64;\n        int64_t s64;\n    } v;\n\n    \/\/ \u6307\u5411\u4e0b\u4e2a\u54c8\u5e0c\u8868\u8282\u70b9\uff0c\u5f62\u6210\u94fe\u8868\n    struct dictEntry *next;\n\n} dictEntry;\n<\/pre>\n<p>key \u7528\u6765\u4fdd\u5b58\u952e\uff0cval \u5c5e\u6027\u7528\u6765\u4fdd\u5b58\u503c\uff0c\u503c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u4e5f\u53ef\u4ee5\u662fuint64_t \u6574\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f int64_t \u6574\u6570\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u54c8\u5e0c\u8868\u8282\u70b9\u7684\u6307\u9488\uff0c\u6211\u4eec\u77e5\u9053\u54c8\u5e0c\u8868\u6700\u5927\u7684\u95ee\u9898\u662f\u5b58\u5728\u54c8\u5e0c\u51b2\u7a81\uff0c\u5982\u4f55\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u6709\u5f00\u653e\u5730\u5740\u6cd5\u548c\u94fe\u5730\u5740\u6cd5\u3002\u8fd9\u91cc\u91c7\u7528\u7684\u4fbf\u662f\u94fe\u5730\u5740\u6cd5\uff0c\u901a\u8fc7 next \u8fd9\u4e2a\u6307\u9488\u53ef\u4ee5\u5c06\u591a\u4e2a\u54c8\u5e0c\u503c\u76f8\u540c\u7684\u952e\u503c\u5bf9\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u7528\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002<\/p>\n<p>\uff08<strong>\u56e0\u4e3a dictEntry \u8282\u70b9\u7ec4\u6210\u7684\u94fe\u8868\u6ca1\u6709\u6307\u5411\u94fe\u8868\u8868\u5c3e\u7684\u6307\u9488\uff0c \u6240\u4ee5\u4e3a\u4e86\u901f\u5ea6\u8003\u8651\uff0c \u7a0b\u5e8f\u603b\u662f\u5c06\u65b0\u8282\u70b9\u6dfb\u52a0\u5230\u94fe\u8868\u7684\u8868\u5934\u4f4d\u7f6e\uff08\u590d\u6742\u5ea6\u4e3a O(1)\uff09\uff0c \u6392\u5728\u5176\u4ed6\u5df2\u6709\u8282\u70b9\u7684\u524d\u9762\u3002<\/strong>\uff09<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366163b14bcdd4e78.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u5b57\u5178<\/h3>\n<p>Redis \u4e2d\u7684\u5b57\u5178\u7531 dict.h\/dict \u7ed3\u6784\u8868\u793a\uff1a<\/p>\n<pre>typedef struct dict {\n\n    \/\/ \u7c7b\u578b\u7279\u5b9a\u51fd\u6570\n    dictType *type;\n\n    \/\/ \u79c1\u6709\u6570\u636e\n    void *privdata;\n\n    \/\/ \u54c8\u5e0c\u8868\n    dictht ht[2];\n\n    \/\/ rehash \u7d22\u5f15\n    \/\/ \u5f53 rehash \u4e0d\u5728\u8fdb\u884c\u65f6\uff0c\u503c\u4e3a -1\n    int rehashidx; \n    \/* rehashing not in progress if rehashidx == -1 *\/\n\n} dict;\n<\/pre>\n<p>type \u5c5e\u6027\u548c privdata \u5c5e\u6027\u662f\u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u952e\u503c\u5bf9\uff0c \u4e3a\u521b\u5efa\u591a\u6001\u5b57\u5178\u800c\u8bbe\u7f6e\u7684\uff1a<\/p>\n<p>\u25cf type \u5c5e\u6027\u662f\u4e00\u4e2a\u6307\u5411 dictType \u7ed3\u6784\u7684\u6307\u9488\uff0c \u6bcf\u4e2a dictType \u7ed3\u6784\u4fdd\u5b58\u4e86\u4e00\u7c07\u7528\u4e8e\u64cd\u4f5c\u7279\u5b9a\u7c7b\u578b\u952e\u503c\u5bf9\u7684\u51fd\u6570\uff0c Redis \u4f1a\u4e3a\u7528\u9014\u4e0d\u540c\u7684\u5b57\u5178\u8bbe\u7f6e\u4e0d\u540c\u7684\u7c7b\u578b\u7279\u5b9a\u51fd\u6570\u3002<br \/>\u25cf \u800c privdata \u5c5e\u6027\u5219\u4fdd\u5b58\u4e86\u9700\u8981\u4f20\u7ed9\u90a3\u4e9b\u7c7b\u578b\u7279\u5b9a\u51fd\u6570\u7684\u53ef\u9009\u53c2\u6570\u3002<\/p>\n<pre>typedef struct dictType {\n\n    \/\/ \u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u51fd\u6570\n    unsigned int (*hashFunction)(const void *key);\n\n    \/\/ \u590d\u5236\u952e\u7684\u51fd\u6570\n    void *(*keyDup)(void *privdata, const void *key);\n\n    \/\/ \u590d\u5236\u503c\u7684\u51fd\u6570\n    void *(*valDup)(void *privdata, const void *obj);\n\n    \/\/ \u5bf9\u6bd4\u952e\u7684\u51fd\u6570\n    int (*keyCompare)(void *privdata, const void *key1, const void *key2);\n\n    \/\/ \u9500\u6bc1\u952e\u7684\u51fd\u6570\n    void (*keyDestructor)(void *privdata, void *key);\n\n    \/\/ \u9500\u6bc1\u503c\u7684\u51fd\u6570\n    void (*valDestructor)(void *privdata, void *obj);\n\n} dictType;\n<\/pre>\n<p>ht \u5c5e\u6027\u662f\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u9879\u7684\u6570\u7ec4\uff0c \u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u9879\u90fd\u662f\u4e00\u4e2a dictht \u54c8\u5e0c\u8868\uff0c \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c \u5b57\u5178\u53ea\u4f7f\u7528 ht[0] \u54c8\u5e0c\u8868\uff0c ht[1] \u54c8\u5e0c\u8868\u53ea\u4f1a\u5728\u5bf9 ht[0] \u54c8\u5e0c\u8868\u8fdb\u884c rehash \u65f6\u4f7f\u7528\u3002<\/p>\n<p>\u9664\u4e86 ht[1] \u4e4b\u5916\uff0c \u53e6\u4e00\u4e2a\u548c rehash \u6709\u5173\u7684\u5c5e\u6027\u5c31\u662f rehashidx \uff1a \u5b83\u8bb0\u5f55\u4e86 rehash \u76ee\u524d\u7684\u8fdb\u5ea6\uff0c \u5982\u679c\u76ee\u524d\u6ca1\u6709\u5728\u8fdb\u884c rehash \uff0c \u90a3\u4e48\u5b83\u7684\u503c\u4e3a -1 \u3002<\/p>\n<p>\u56fe 4-3 \u5c55\u793a\u4e86\u4e00\u4e2a\u666e\u901a\u72b6\u6001\u4e0b\uff08\u6ca1\u6709\u8fdb\u884c rehash\uff09\u7684\u5b57\u5178\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366263b14bce3e382.jpg\" class=\"aligncenter\"><\/p>\n<p>\u2460 <strong>\u54c8\u5e0c\u7b97\u6cd5<\/strong>\uff1aRedis\u8ba1\u7b97\u54c8\u5e0c\u503c\u548c\u7d22\u5f15\u503c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre># \u4f7f\u7528\u5b57\u5178\u8bbe\u7f6e\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u8ba1\u7b97\u952e key \u7684\u54c8\u5e0c\u503c\nhash = dict-&gt;type-&gt;hashFunction(key);\n\n# \u4f7f\u7528\u54c8\u5e0c\u8868\u7684 sizemask \u5c5e\u6027\u548c\u54c8\u5e0c\u503c\uff0c\u8ba1\u7b97\u51fa\u7d22\u5f15\u503c\n# \u6839\u636e\u60c5\u51b5\u4e0d\u540c\uff0c ht[x] \u53ef\u4ee5\u662f ht[0] \u6216\u8005 ht[1]\nindex = hash &amp; dict-&gt;ht[x].sizemask;\n<\/pre>\n<p>\u2461<strong>\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81<\/strong>\uff1a\u8fd9\u4e2a\u95ee\u9898\u4e0a\u9762\u6211\u4eec\u4ecb\u7ecd\u4e86\uff0c\u65b9\u6cd5\u662f\u94fe\u5730\u5740\u6cd5\u3002\u901a\u8fc7\u5b57\u5178\u91cc\u9762\u7684 *next \u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u5177\u6709\u76f8\u540c\u7d22\u5f15\u503c\u7684\u54c8\u5e0c\u8868\u8282\u70b9\u3002<\/p>\n<p>\u2462<strong>\u6269\u5bb9\u548c\u6536\u7f29(rehash)<\/strong>\uff1a\u5f53\u54c8\u5e0c\u8868\u4fdd\u5b58\u7684\u952e\u503c\u5bf9\u592a\u591a\u6216\u8005\u592a\u5c11\u65f6\uff0c\u5c31\u8981\u901a\u8fc7 rerehash(\u91cd\u65b0\u6563\u5217\uff09\u6765\u5bf9\u54c8\u5e0c\u8868\u8fdb\u884c\u76f8\u5e94\u7684\u6269\u5c55\u6216\u8005\u6536\u7f29\u3002\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<p>1\u3001\u4e3a\u5b57\u5178\u7684 ht[1] \u54c8\u5e0c\u8868\u5206\u914d\u7a7a\u95f4\uff0c \u8fd9\u4e2a\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u5927\u5c0f\u53d6\u51b3\u4e8e\u8981\u6267\u884c\u7684\u64cd\u4f5c\uff0c \u4ee5\u53ca ht[0] \u5f53\u524d\u5305\u542b\u7684\u952e\u503c\u5bf9\u6570\u91cf \uff08\u4e5f\u5373\u662f ht[0].used \u5c5e\u6027\u7684\u503c\uff09<br \/>\u25cf \u5982\u679c\u6267\u884c\u7684\u662f\u6269\u5c55\u64cd\u4f5c\uff0c \u90a3\u4e48 ht[1] \u7684\u5927\u5c0f\u4e3a\u7b2c\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e ht[0].used * 2n\uff1b \uff08\u4e5f\u5c31\u662f\u6bcf\u6b21\u6269\u5c55\u90fd\u662f\u6839\u636e\u539f\u54c8\u5e0c\u8868\u5df2\u4f7f\u7528\u7684\u7a7a\u95f4\u6269\u5927\u4e00\u500d\u521b\u5efa\u53e6\u4e00\u4e2a\u54c8\u5e0c\u8868\uff09<br \/>\u25cf \u5982\u679c\u6267\u884c\u7684\u662f\u6536\u7f29\u64cd\u4f5c\uff0c \u6bcf\u6b21\u6536\u7f29\u662f\u6839\u636e\u5df2\u4f7f\u7528\u7a7a\u95f4\u7f29\u5c0f\u4e00\u500d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u54c8\u5e0c\u8868\u3002<br \/>2\u3001\u5c06\u4fdd\u5b58\u5728 ht[0] \u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9 rehash \u5230 ht[1] \u4e0a\u9762\uff1a rehash \u6307\u7684\u662f\u91cd\u65b0\u8ba1\u7b97\u952e\u7684\u54c8\u5e0c\u503c\u548c\u7d22\u5f15\u503c\uff0c \u7136\u540e\u5c06\u952e\u503c\u5bf9\u653e\u7f6e\u5230 ht[1] \u54c8\u5e0c\u8868\u7684\u6307\u5b9a\u4f4d\u7f6e\u4e0a\u3002<br \/>3\u3001\u5f53 ht[0] \u5305\u542b\u7684\u6240\u6709\u952e\u503c\u5bf9\u90fd\u8fc1\u79fb\u5230\u4e86 ht[1] \u4e4b\u540e \uff08ht[0] \u53d8\u4e3a\u7a7a\u8868\uff09\uff0c \u91ca\u653e ht[0] \uff0c \u5c06 ht[1] \u8bbe\u7f6e\u4e3a ht[0] \uff0c \u5e76\u5728 ht[1] \u65b0\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u54c8\u5e0c\u8868\uff0c \u4e3a\u4e0b\u4e00\u6b21 rehash \u505a\u51c6\u5907\u3002<\/p>\n<p>\u2463<strong>\u89e6\u53d1\u6269\u5bb9\u4e0e\u6536\u7f29\u7684\u6761\u4ef6<\/strong>\uff1a<br \/>1\u3001\u670d\u52a1\u5668\u76ee\u524d\u6ca1\u6709\u6267\u884c BGSAVE \u547d\u4ee4\u6216\u8005 BGREWRITEAOF \u547d\u4ee4\uff0c\u5e76\u4e14\u8d1f\u8f7d\u56e0\u5b50\u5927\u4e8e\u7b49\u4e8e1\u3002<\/p>\n<p>2\u3001\u670d\u52a1\u5668\u76ee\u524d\u6b63\u5728\u6267\u884c BGSAVE \u547d\u4ee4\u6216\u8005 BGREWRITEAOF \u547d\u4ee4\uff0c\u5e76\u4e14\u8d1f\u8f7d\u56e0\u5b50\u5927\u4e8e\u7b49\u4e8e5\u3002<\/p>\n<p>ps\uff1a\u8d1f\u8f7d\u56e0\u5b50 = \u54c8\u5e0c\u8868\u5df2\u4fdd\u5b58\u8282\u70b9\u6570\u91cf \/ \u54c8\u5e0c\u8868\u5927\u5c0f\u3002<\/p>\n<p>3\u3001\u53e6\u4e00\u65b9\u9762\uff0c \u5f53\u54c8\u5e0c\u8868\u7684\u8d1f\u8f7d\u56e0\u5b50\u5c0f\u4e8e 0.1 \u65f6\uff0c \u7a0b\u5e8f\u81ea\u52a8\u5f00\u59cb\u5bf9\u54c8\u5e0c\u8868\u6267\u884c\u6536\u7f29\u64cd\u4f5c\u3002<\/p>\n<p>\u2464<strong>\u6e10\u8fd1\u5f0f rehash<\/strong><br \/>\u4ec0\u4e48\u53eb\u6e10\u8fdb\u5f0f rehash\uff1f\u4e5f\u5c31\u662f\u8bf4\u6269\u5bb9\u548c\u6536\u7f29\u64cd\u4f5c\u4e0d\u662f\u4e00\u6b21\u6027\u3001\u96c6\u4e2d\u5f0f\u5b8c\u6210\u7684\uff0c\u800c\u662f\u5206\u591a\u6b21\u3001\u6e10\u8fdb\u5f0f\u5b8c\u6210\u7684\u3002\u5982\u679c\u4fdd\u5b58\u5728Redis\u4e2d\u7684\u952e\u503c\u5bf9\u53ea\u6709\u51e0\u4e2a\u51e0\u5341\u4e2a\uff0c\u90a3\u4e48 rehash \u64cd\u4f5c\u53ef\u4ee5\u77ac\u95f4\u5b8c\u6210\uff0c\u4f46\u662f\u5982\u679c\u952e\u503c\u5bf9\u6709\u51e0\u767e\u4e07\uff0c\u51e0\u5343\u4e07\u751a\u81f3\u51e0\u4ebf\uff0c\u90a3\u4e48\u8981\u4e00\u6b21\u6027\u7684\u8fdb\u884c rehash\uff0c\u52bf\u5fc5\u4f1a\u9020\u6210Redis\u4e00\u6bb5\u65f6\u95f4\u5185\u4e0d\u80fd\u8fdb\u884c\u522b\u7684\u64cd\u4f5c\u3002\u6240\u4ee5Redis\u91c7\u7528\u6e10\u8fdb\u5f0f rehash\uff0c\u8fd9\u6837\u5728\u8fdb\u884c\u6e10\u8fdb\u5f0frehash\u671f\u95f4\uff0c\u5b57\u5178\u7684\u5220\u9664\u67e5\u627e\u66f4\u65b0\u7b49\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5728\u4e24\u4e2a\u54c8\u5e0c\u8868\u4e0a\u8fdb\u884c\uff0c\u7b2c\u4e00\u4e2a\u54c8\u5e0c\u8868\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u4f1a\u53bb\u7b2c\u4e8c\u4e2a\u54c8\u5e0c\u8868\u4e0a\u8fdb\u884c\u67e5\u627e\u3002\u4f46\u662f\u8fdb\u884c\u589e\u52a0\u64cd\u4f5c\uff0c\u4e00\u5b9a\u662f\u5728\u65b0\u7684\u54c8\u5e0c\u8868\u4e0a\u8fdb\u884c\u7684\u3002<\/p>\n<h2>4\u3001\u8df3\u8dc3\u8868<\/h2>\n<p>\u8df3\u8dc3\u8868\uff08skiplist\uff09\u662f\u4e00\u79cd\u6709\u5e8f\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u901a\u8fc7\u5728\u6bcf\u4e2a\u8282\u70b9\u4e2d\u7ef4\u6301\u591a\u4e2a\u6307\u5411\u5176\u5b83\u8282\u70b9\u7684\u6307\u9488\uff0c\u4ece\u800c\u8fbe\u5230\u5feb\u901f\u8bbf\u95ee\u8282\u70b9\u7684\u76ee\u7684\u3002<\/p>\n<p>\u5177\u6709\u5982\u4e0b\u6027\u8d28\uff1a<br \/>1\u3001\u7531\u5f88\u591a\u5c42\u7ed3\u6784\u7ec4\u6210\uff1b<\/p>\n<p>2\u3001\u6bcf\u4e00\u5c42\u90fd\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u94fe\u8868\uff0c\u6392\u5217\u987a\u5e8f\u4e3a\u7531\u9ad8\u5c42\u5230\u5e95\u5c42\uff0c\u90fd\u81f3\u5c11\u5305\u542b\u4e24\u4e2a\u94fe\u8868\u8282\u70b9\uff0c\u5206\u522b\u662f\u524d\u9762\u7684head\u8282\u70b9\u548c\u540e\u9762\u7684nil\u8282\u70b9\uff1b<\/p>\n<p>3\u3001\u6700\u5e95\u5c42\u7684\u94fe\u8868\u5305\u542b\u4e86\u6240\u6709\u7684\u5143\u7d20\uff1b<\/p>\n<p>4\u3001\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u51fa\u73b0\u5728\u67d0\u4e00\u5c42\u7684\u94fe\u8868\u4e2d\uff0c\u90a3\u4e48\u5728\u8be5\u5c42\u4e4b\u4e0b\u7684\u94fe\u8868\u4e5f\u5168\u90fd\u4f1a\u51fa\u73b0\uff08\u4e0a\u4e00\u5c42\u7684\u5143\u7d20\u662f\u5f53\u524d\u5c42\u7684\u5143\u7d20\u7684\u5b50\u96c6\uff09\uff1b<\/p>\n<p>5\u3001\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u4e2a\u6307\u9488\uff0c\u4e00\u4e2a\u6307\u5411\u540c\u4e00\u5c42\u7684\u4e0b\u4e00\u4e2a\u94fe\u8868\u8282\u70b9\uff0c\u53e6\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u5c42\u7684\u540c\u4e00\u4e2a\u94fe\u8868\u8282\u70b9\uff1b<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366263b14bce9e080.jpg\" class=\"aligncenter\"><\/p>\n<p>\u548c\u94fe\u8868\u3001\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u88ab\u5e7f\u6cdb\u5730\u5e94\u7528\u5728 Redis \u5185\u90e8\u4e0d\u540c\uff0c Redis \u53ea\u5728\u4e24\u4e2a\u5730\u65b9\u7528\u5230\u4e86\u8df3\u8dc3\u8868\uff0c \u4e00\u4e2a\u662f\u5b9e\u73b0\u6709\u5e8f\u96c6\u5408\u952e\uff0c \u53e6\u4e00\u4e2a\u662f\u5728\u96c6\u7fa4\u8282\u70b9\u4e2d\u7528\u4f5c\u5185\u90e8\u6570\u636e\u7ed3\u6784\uff0c \u9664\u6b64\u4e4b\u5916\uff0c \u8df3\u8dc3\u8868\u5728 Redis \u91cc\u9762\u6ca1\u6709\u5176\u4ed6\u7528\u9014\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366363b14bcf0c954.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u8df3\u8dc3\u8868\u8282\u70b9\uff08zskiplistNode\uff09<\/h3>\n<p>\u8be5\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027\uff1a<br \/>\u2460\u5c42\uff08level\uff09\uff1a\u8282\u70b9\u4e2d\u7528 L1 \u3001 L2 \u3001 L3 \u7b49\u5b57\u6837\u6807\u8bb0\u8282\u70b9\u7684\u5404\u4e2a\u5c42\uff0c L1 \u4ee3\u8868\u7b2c\u4e00\u5c42\uff0c L2 \u4ee3\u8868\u7b2c\u4e8c\u5c42\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u6bcf\u4e2a\u5c42\u90fd\u5e26\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a\u524d\u8fdb\u6307\u9488\u548c\u8de8\u5ea6\u3002\u524d\u8fdb\u6307\u9488\u7528\u4e8e\u8bbf\u95ee\u4f4d\u4e8e\u8868\u5c3e\u65b9\u5411\u7684\u5176\u4ed6\u8282\u70b9\uff0c\u800c\u8de8\u5ea6\u5219\u8bb0\u5f55\u4e86\u524d\u8fdb\u6307\u9488\u6240\u6307\u5411\u8282\u70b9\u548c\u5f53\u524d\u8282\u70b9\u7684\u8ddd\u79bb\u3002\u5728\u4e0a\u9762\u7684\u56fe\u7247\u4e2d\uff0c\u8fde\u7ebf\u4e0a\u5e26\u6709\u6570\u5b57\u7684\u7bad\u5934\u5c31\u4ee3\u8868\u524d\u8fdb\u6307\u9488\uff0c\u800c\u90a3\u4e2a\u6570\u5b57\u5c31\u662f\u8de8\u5ea6\u3002\u5f53\u7a0b\u5e8f\u4ece\u8868\u5934\u5411\u8868\u5c3e\u8fdb\u884c\u904d\u5386\u65f6\uff0c\u8bbf\u95ee\u4f1a\u6cbf\u7740\u5c42\u7684\u524d\u8fdb\u6307\u9488\u8fdb\u884c\u3002<br \/>\u6bcf\u6b21\u521b\u5efa\u4e00\u4e2a\u65b0\u8df3\u8dc3\u8868\u8282\u70b9\u7684\u65f6\u5019\uff0c \u7a0b\u5e8f\u90fd\u6839\u636e\u5e42\u6b21\u5b9a\u5f8b \uff08power law\uff0c\u8d8a\u5927\u7684\u6570\u51fa\u73b0\u7684\u6982\u7387\u8d8a\u5c0f\uff09 \u968f\u673a\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e 1 \u548c 32 \u4e4b\u95f4\u7684\u503c\u4f5c\u4e3a level \u6570\u7ec4\u7684\u5927\u5c0f\uff0c \u8fd9\u4e2a\u5927\u5c0f\u5c31\u662f\u5c42\u7684\u201c\u9ad8\u5ea6\u201d\u3002\uff08<strong>\u6240\u4ee5\u8bf4\u8868\u5934\u8282\u70b9\u7684\u9ad8\u5ea6\u5c31\u662f32<\/strong>\uff09<\/p>\n<p>\u2461\u540e\u9000\uff08backward\uff09\u6307\u9488\uff1a\u8282\u70b9\u4e2d\u7528 BW \u5b57\u6837\u6807\u8bb0\u8282\u70b9\u7684\u540e\u9000\u6307\u9488\uff0c\u5b83\u6307\u5411\u4f4d\u4e8e\u5f53\u524d\u8282\u70b9\u7684\u524d\u4e00\u4e2a\u8282\u70b9\u3002\u540e\u9000\u6307\u9488\u5728\u7a0b\u5e8f\u4ece\u8868\u5c3e\u5411\u8868\u5934\u904d\u5386\u65f6\u4f7f\u7528\u3002<\/p>\n<p>\u2462\u5206\u503c\uff08score\uff09\uff1a\u5404\u4e2a\u8282\u70b9\u4e2d\u7684 1.0 \u3001 2.0 \u548c 3.0 \u662f\u8282\u70b9\u6240\u4fdd\u5b58\u7684\u5206\u503c\u3002\u5728\u8df3\u8dc3\u8868\u4e2d\uff0c\u8282\u70b9\u6309\u5404\u81ea\u6240\u4fdd\u5b58\u7684\u5206\u503c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u3002<\/p>\n<p>\u2463\u6210\u5458\u5bf9\u8c61\uff08obj\uff09\uff1a\u5404\u4e2a\u8282\u70b9\u4e2d\u7684 o1 \u3001 o2 \u548c o3 \u662f\u8282\u70b9\u6240\u4fdd\u5b58\u7684\u6210\u5458\u5bf9\u8c61\u3002<\/p>\n<p>\u6ce8\u610f\u8868\u5934\u8282\u70b9\u548c\u5176\u4ed6\u8282\u70b9\u7684\u6784\u9020\u662f\u4e00\u6837\u7684\uff1a \u8868\u5934\u8282\u70b9\u4e5f\u6709\u540e\u9000\u6307\u9488\u3001\u5206\u503c\u548c\u6210\u5458\u5bf9\u8c61\uff0c \u4e0d\u8fc7\u8868\u5934\u8282\u70b9\u7684\u8fd9\u4e9b\u5c5e\u6027\u90fd\u4e0d\u4f1a\u88ab\u7528\u5230\uff0c \u6240\u4ee5\u56fe\u4e2d\u7701\u7565\u4e86\u8fd9\u4e9b\u90e8\u5206\uff0c \u53ea\u663e\u793a\u4e86\u8868\u5934\u8282\u70b9\u7684\u5404\u4e2a\u5c42\u3002<\/p>\n<h3>\u8df3\u8dc3\u8868\uff08zskiplist\uff09<\/h3>\n<p>\u2460 header \uff1a\u6307\u5411\u8df3\u8dc3\u8868\u7684\u8868\u5934\u8282\u70b9\u3002<br \/>\u2461 tail \uff1a\u6307\u5411\u8df3\u8dc3\u8868\u7684\u8868\u5c3e\u8282\u70b9\u3002<br \/>\u2462 level \uff1a\u8bb0\u5f55\u76ee\u524d\u8df3\u8dc3\u8868\u5185\uff0c\u5c42\u6570\u6700\u5927\u7684\u90a3\u4e2a\u8282\u70b9\u7684\u5c42\u6570\uff08\u8868\u5934\u8282\u70b9\u7684\u5c42\u6570\u4e0d\u8ba1\u7b97\u5728\u5185\uff09\u3002<br \/>\u2463 length \uff1a\u8bb0\u5f55\u8df3\u8dc3\u8868\u7684\u957f\u5ea6\uff0c\u4e5f\u5373\u662f\uff0c\u8df3\u8dc3\u8868\u76ee\u524d\u5305\u542b\u8282\u70b9\u7684\u6570\u91cf\uff08\u8868\u5934\u8282\u70b9\u4e0d\u8ba1\u7b97\u5728\u5185\uff09\u3002<\/p>\n<h2>5\u3001\u6574\u6570\u96c6\u5408<\/h2>\n<p>\u6574\u6570\u96c6\u5408\uff08intset\uff09\u662f\u96c6\u5408\u952e\u7684\u5e95\u5c42\u5b9e\u73b0\u4e4b\u4e00\uff0c\u5f53\u4e00\u4e2a\u96c6\u5408\u53ea\u5305\u542b\u6574\u6570\u503c\u5143\u7d20\uff0c\u5e76\u4e14\u8fd9\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u6570\u91cf\u4e0d\u591a\u65f6\uff0cRedis\u5c31\u4f1a\u4f7f\u7528\u96c6\u5408\u4f5c\u4e3a\u96c6\u5408\u952e\u7684\u5e95\u5c42\u5b9e\u73b0\u3002<\/p>\n<p>\u6574\u6570\u96c6\u5408\uff08intset\uff09\u662fRedis\u7528\u4e8e\u4fdd\u5b58\u6574\u6570\u503c\u7684\u96c6\u5408\u62bd\u8c61\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u4fdd\u5b58\u7c7b\u578b\u4e3aint16_t\u3001int32_t \u6216\u8005int64_t \u7684\u6574\u6570\u503c\uff0c\u5e76\u4e14\u4fdd\u8bc1\u96c6\u5408\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p>\u6bcf\u4e2a intset.h\/intset \u7ed3\u6784\u8868\u793a\u4e00\u4e2a\u6574\u6570\u96c6\u5408\uff1a<\/p>\n<pre>typedef struct intset {\n\n    \/\/ \u7f16\u7801\u65b9\u5f0f\n    uint32_t encoding;\n\n    \/\/ \u96c6\u5408\u5305\u542b\u7684\u5143\u7d20\u6570\u91cf\n    uint32_t length;\n\n    \/\/ \u4fdd\u5b58\u5143\u7d20\u7684\u6570\u7ec4\n    int8_t contents[];\n\n} intset;\n<\/pre>\n<p>\u6574\u6570\u96c6\u5408\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f contents \u6570\u7ec4\u7684\u4e00\u4e2a\u6570\u636e\u9879\uff0c\u5b83\u4eec\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u4e14\u4e0d\u5305\u542b\u4efb\u4f55\u91cd\u590d\u9879\u3002<\/p>\n<p>length \u5c5e\u6027\u8bb0\u5f55\u4e86 contents \u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u867d\u7136 contents \u6570\u7ec4\u58f0\u660e\u4e3a int8_t \u7c7b\u578b\uff0c\u4f46\u662f\u5b9e\u9645\u4e0acontents \u6570\u7ec4\u5e76\u4e0d\u4fdd\u5b58\u4efb\u4f55 int8_t \u7c7b\u578b\u7684\u503c\uff0c\u5176\u771f\u6b63\u7c7b\u578b\u6709 encoding \u6765\u51b3\u5b9a\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366363b14bcf6379d.jpg\" class=\"aligncenter\"><\/p>\n<p>\u2460 \u5347\u7ea7\uff08encoding int16_t -&gt; int32_t -&gt; int64_t\uff09<br \/>\u5f53\u6211\u4eec\u65b0\u589e\u7684\u5143\u7d20\u7c7b\u578b\u6bd4\u539f\u96c6\u5408\u5143\u7d20\u7c7b\u578b\u7684\u957f\u5ea6\u8981\u5927\u65f6\uff0c\u9700\u8981\u5bf9\u6574\u6570\u96c6\u5408\u8fdb\u884c\u5347\u7ea7\uff0c\u624d\u80fd\u5c06\u65b0\u5143\u7d20\u653e\u5165\u6574\u6570\u96c6\u5408\u4e2d\u3002\u5177\u4f53\u6b65\u9aa4\uff1a<br \/>1\u3001\u6839\u636e\u65b0\u5143\u7d20\u7c7b\u578b\uff0c\u6269\u5c55\u6574\u6570\u96c6\u5408\u5e95\u5c42\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u5e76\u4e3a\u65b0\u5143\u7d20\u5206\u914d\u7a7a\u95f4\u3002<br \/>2\u3001\u5c06\u5e95\u5c42\u6570\u7ec4\u73b0\u6709\u7684\u6240\u6709\u5143\u7d20\u90fd\u8f6c\u6210\u4e0e\u65b0\u5143\u7d20\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u8f6c\u6362\u540e\u7684\u5143\u7d20\u653e\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\uff0c\u653e\u7f6e\u8fc7\u7a0b\u4e2d\uff0c\u7ef4\u6301\u6574\u4e2a\u5143\u7d20\u987a\u5e8f\u90fd\u662f\u6709\u5e8f\u7684\u3002<br \/>3\u3001\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u6574\u6570\u96c6\u5408\u4e2d\uff08\u4fdd\u8bc1\u6709\u5e8f\uff09\u3002<br \/>\u5347\u7ea7\u80fd\u6781\u5927\u5730\u8282\u7701\u5185\u5b58\u3002<\/p>\n<p>\u2461 \u964d\u7ea7<br \/>\u6574\u6570\u96c6\u5408\u4e0d\u652f\u6301\u964d\u7ea7\u64cd\u4f5c\uff0c\u4e00\u65e6\u5bf9\u6570\u7ec4\u8fdb\u884c\u4e86\u5347\u7ea7\uff0c\u7f16\u7801\u5c31\u4f1a\u4e00\u76f4\u4fdd\u6301\u5347\u7ea7\u540e\u7684\u72b6\u6001\u3002<\/p>\n<h2>6\u3001\u538b\u7f29\u5217\u8868<\/h2>\n<p>\u538b\u7f29\u5217\u8868\uff08ziplist\uff09\u662f\u5217\u8868\u952e\u548c\u54c8\u5e0c\u952e\u7684\u5e95\u5c42\u5b9e\u73b0\u4e4b\u4e00\u3002<br \/>\u5f53\u4e00\u4e2a\u5217\u8868\u952e\u53ea\u5305\u542b\u5c11\u91cf\u5217\u8868\u9879\uff0c \u5e76\u4e14\u6bcf\u4e2a\u5217\u8868\u9879\u8981\u4e48\u5c31\u662f\u5c0f\u6574\u6570\u503c\uff0c \u8981\u4e48\u5c31\u662f\u957f\u5ea6\u6bd4\u8f83\u77ed\u7684\u5b57\u7b26\u4e32\uff0c \u90a3\u4e48 Redis \u5c31\u4f1a\u4f7f\u7528\u538b\u7f29\u5217\u8868\u6765\u505a\u5217\u8868\u952e\u7684\u5e95\u5c42\u5b9e\u73b0\u3002<\/p>\n<p>\u56e0\u4e3a\u54c8\u5e0c\u952e\u91cc\u9762\u5305\u542b\u7684\u6240\u6709\u952e\u548c\u503c\u90fd\u662f\u5c0f\u6574\u6570\u503c\u6216\u8005\u77ed\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u538b\u7f29\u5217\u8868\u662f Redis \u4e3a\u4e86\u8282\u7ea6\u5185\u5b58\u800c\u5f00\u53d1\u7684\uff0c \u7531\u4e00\u7cfb\u5217\u7279\u6b8a\u7f16\u7801\u7684\u8fde\u7eed\u5185\u5b58\u5757\u7ec4\u6210\u7684\u987a\u5e8f\u578b\uff08sequential\uff09\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u4e00\u4e2a\u538b\u7f29\u5217\u8868\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u591a\u4e2a\u8282\u70b9\uff08entry\uff09\uff0c \u6bcf\u4e2a\u8282\u70b9\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u6216\u8005\u4e00\u4e2a\u6574\u6570\u503c\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366363b14bcfca31b.jpg\" class=\"aligncenter\"><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366463b14bd037843.jpg\" class=\"aligncenter\"><\/p>\n<p>\u6bcf\u4e2a\u538b\u7f29\u5217\u8868\u8282\u70b9\u90fd\u7531 previous_entry_length \u3001 encoding \u3001 content \u4e09\u4e2a\u90e8\u5206\u7ec4\u6210<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366463b14bd09763b.jpg\" class=\"aligncenter\"><\/p>\n<p>\u2460 previous_entry_ength\uff1a\u8bb0\u5f55\u538b\u7f29\u5217\u8868\u524d\u4e00\u4e2a\u5b57\u8282\u7684\u957f\u5ea6\u3002previous_entry_ength \u7684\u957f\u5ea6\u53ef\u80fd\u662f1\u4e2a\u5b57\u8282\u6216\u8005\u662f5\u4e2a\u5b57\u8282\u3002\u5982\u679c\u4e0a\u4e00\u4e2a\u8282\u70b9\u7684\u957f\u5ea6\u5c0f\u4e8e254\uff0c\u5219\u8be5\u8282\u70b9\u53ea\u9700\u8981\u4e00\u4e2a\u5b57\u8282\u5c31\u53ef\u4ee5\u8868\u793a\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u957f\u5ea6\u4e86\u3002\u5982\u679c\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e254\uff0c\u5219\u5c5e\u6027\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u4e3a254\uff0c\u540e\u9762\u7528\u56db\u4e2a\u5b57\u8282\u8868\u793a\u5f53\u524d\u8282\u70b9\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u957f\u5ea6\u3002\u5229\u7528\u6b64\u539f\u7406\u5373\u5f53\u524d\u8282\u70b9\u4f4d\u7f6e\u51cf\u53bb\u4e0a\u4e00\u4e2a\u8282\u70b9\u7684\u957f\u5ea6\u5373\u5f97\u5230\u4e0a\u4e00\u4e2a\u8282\u70b9\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u538b\u7f29\u5217\u8868\u53ef\u4ee5\u4ece\u5c3e\u90e8\u5411\u5934\u90e8\u904d\u5386\u3002\u8fd9\u4e48\u505a\u5f88\u6709\u6548\u5730\u51cf\u5c11\u4e86\u5185\u5b58\u7684\u6d6a\u8d39\u3002<\/p>\n<p>\u2461 encoding\uff1a\u8282\u70b9\u7684encoding\u4fdd\u5b58\u7684\u662f\u8282\u70b9\u7684content\u7684\u5185\u5bb9\u7c7b\u578b\u4ee5\u53ca\u957f\u5ea6\uff0cencoding\u7c7b\u578b\u4e00\u5171\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u5b57\u8282\u6570\u7ec4\u4e00\u79cd\u662f\u6574\u6570\uff0cencoding\u533a\u57df\u957f\u5ea6\u4e3a1\u5b57\u8282\u30012\u5b57\u8282\u6216\u80055\u5b57\u8282\u957f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366463b14bd0f164d.jpg\" class=\"aligncenter\"><\/p>\n<p>\u2462 content\uff1acontent\u533a\u57df\u7528\u4e8e\u4fdd\u5b58\u8282\u70b9\u7684\u5185\u5bb9\uff0c\u8282\u70b9\u5185\u5bb9\u7c7b\u578b\u548c\u957f\u5ea6\u7531encoding\u51b3\u5b9a\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167256366563b14bd15acfa.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fde\u9501\u66f4\u65b0\u95ee\u9898<br \/>\u8fde\u9501\u66f4\u65b0\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u9700\u8981\u5bf9\u538b\u7f29\u5217\u8868\u6267\u884c N \u6b21\u7a7a\u95f4\u91cd\u5206\u914d\u64cd\u4f5c\uff0c \u800c\u6bcf\u6b21\u7a7a\u95f4\u91cd\u5206\u914d\u7684\u6700\u574f\u590d\u6742\u5ea6\u4e3a O(N) \uff0c \u6240\u4ee5\u8fde\u9501\u66f4\u65b0\u7684\u6700\u574f\u590d\u6742\u5ea6\u4e3a O(N^2) \u3002<\/p>\n<p><strong>\u8981\u6ce8\u610f\u7684\u662f\uff0c \u5c3d\u7ba1\u8fde\u9501\u66f4\u65b0\u7684\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c \u4f46\u5b83\u771f\u6b63\u9020\u6210\u6027\u80fd\u95ee\u9898\u7684\u51e0\u7387\u662f\u5f88\u4f4e\u7684<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u7684\u516d\u79cd\u5e95\u5c42\u6570\u636e\u7ed3\u6784(\u5c0f&#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-201289","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201289","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=201289"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201289\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}