{"id":207,"date":"2022-06-19T23:23:18","date_gmt":"2022-06-19T15:23:18","guid":{"rendered":"https:\/\/cococat.top\/?p=207"},"modified":"2022-06-19T23:23:18","modified_gmt":"2022-06-19T15:23:18","slug":"redisnote","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2022\/06\/19\/redisnote\/","title":{"rendered":"\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5b66\u4e60\u7b14\u8bb0-Redis"},"content":{"rendered":"<h1>1. NOSQL\u7684\u7279\u70b9<\/h1>\n<h2>1.1 \u5206\u7c7b<\/h2>\n<p>\u952e\u503c\u5bf9\/\u534a\u7ed3\u6784\u5316\u6587\u6863(json)\/\u56fe\u6570\u636e\u5e93<\/p>\n<h2>1.2 \u4f18\u70b9<\/h2>\n<ul>\n<li>\u5185\u5b58\u7ea7\u6570\u636e\u5e93\uff0c\u8bfb\u5199\u6548\u7387\u9ad8<\/li>\n<li>\u652f\u6301\u9ad8\u5e76\u53d1\uff08\u5173\u7cfb\u578b\u6570\u636e\u5e93\u6709\u78c1\u76d8IO\u74f6\u9888\uff09<\/li>\n<li>\u652f\u6301\u6d77\u91cf\u6570\u636e\uff08\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5355\u8868\u8fc7\u5927\u5bfc\u81f4SQL\u67e5\u8be2\u6027\u80fd\u4f4e\u4e0b\uff09<\/li>\n<li>\u952e\u503c\u5bf9\/\u534a\u7ed3\u6784\u5316\u6587\u6863(json)\/\u56fe\u6570\u636e\u5e93\u2026\u2026\u7684\u5f62\u5f0f\uff0c\u6570\u636e\u65e0\u8026\u5408\uff0c\u6269\u5c55\u6027\u5f3a<\/li>\n<\/ul>\n<h2>1.3 \u7f3a\u70b9<\/h2>\n<ul>\n<li>\u4e0d\u652f\u6301sql<\/li>\n<li>\u4e0d\u652f\u6301\u4e8b\u52a1<\/li>\n<li>\u65e0\u6570\u636e\u5b8c\u6574\u6027\u7ea6\u675f<\/li>\n<li>\u529f\u80fd\u4e0d\u5982\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5b8c\u5584\uff08\u4f8b\u5982\u7f3a\u5c11\u591a\u8868\u5173\u8054\u67e5\u8be2\uff09<\/li>\n<\/ul>\n<h1>2. Redis\u76845\u79cd\u57fa\u7840<strong>\u6570\u636e\u7c7b\u578b<\/strong> \/ <strong>\u5bf9\u8c61<\/strong>\u7c7b\u578b<\/h1>\n<h2>2.0 redis \u5bf9\u8c61\u7cfb\u7edf<\/h2>\n<p>redis\u57fa\u4e8e\u5176\u5e95\u5c42\u57fa\u7840\u6570\u636e\u7ed3\u6784(\u53c2\u8003\u7b2c3\u8282)\u5efa\u7acb\u4e86\u4e00\u4e2a\u5bf9\u8c61\u7cfb\u7edf\u3002\u4f5c\u4e3a\u952e\u503c\u5bf9\u6570\u636e\u5e93\uff0credis\u4e2d\u7684\u6bcf\u4e00\u4e2a\u952e\u503c\u5bf9\u90fd\u5305\u542b\u4e00\u4e2a\u952e\u5bf9\u8c61\u548c\u4e00\u4e2a\u503c\u5bf9\u8c61\u3002\u5bf9\u8c61\u7684\u7ed3\u6784\u4f53<code>redisObject<\/code>\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct redisObject {\n    unsigned type:4;        \/\/ \u7c7b\u578b\n    unsigned encoding:4;    \/\/ \u7f16\u7801\n    int refcount;           \/\/ \u5f15\u7528\u8ba1\u6570\n    \/\/ ...\n    void *ptr;              \/\/ \u6307\u5411\u5e95\u5c42\u5b9e\u73b0\u6570\u636e\u7ed3\u6784\u7684\u6307\u9488\n    \/\/ ...\n    unsigned lru:22;        \/\/ \u6700\u540e\u4e00\u6b21\u88ab\u547d\u4ee4\u7a0b\u5e8f\u8bbf\u95ee\u7684\u65f6\u95f4\n} robj;<\/code><\/pre>\n<p>\u5176\u4e2d\u7684<code>type<\/code>\u7c7b\u578b\u5c5e\u6027\u4e00\u5171\u6709\u4e94\u79cd\u7c7b\u578b\uff0c\u5206\u522b\u662f\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u3002\u5bf9\u4e8e\u4e00\u4e2a\u952e\u5bf9\u8c61\u800c\u8a00\uff0c\u5176\u7c7b\u578b\u6c38\u8fdc\u662f\u5b57\u7b26\u4e32\uff1b\u5bf9\u4e8e\u503c\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7<code>TYPE<\/code>\u547d\u4ee4\u67e5\u770b\u5176\u7c7b\u578b\u3002<br \/>\n<code>encoding<\/code>\u7f16\u7801\u5c5e\u6027\u8bb0\u5f55\u4e86\u672c\u5bf9\u8c61\u91c7\u7528\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u7c7b\u578b\uff0c\u6bcf\u79cdredis\u5bf9\u8c61\u81f3\u5c11\u90fd\u6709\u4e24\u79cd\u7f16\u7801\uff0c\u4f1a\u6839\u636e\u6570\u636e\u7684\u7c7b\u578b\u548c\u89c4\u6a21\u81ea\u52a8\u5207\u6362\uff0c\u63d0\u9ad8\u4e86\u6548\u7387\u3002<br \/>\n<code>refcount<\/code>\u5f15\u7528\u8ba1\u6570\u53ef\u4ee5\u7528\u4e8e\u5185\u5b58\u56de\u6536\u673a\u5236\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u521b\u5efa\u6216\u88ab\u5f15\u7528\u65f6\uff0c\u8ba1\u6570\u5668\u589e\u52a01\uff1b\u4e0d\u518d\u88ab\u5f15\u7528\u65f6\u5219\u8ba1\u6570\u5668\u51cf1\uff0c\u5f53\u5f15\u7528\u8ba1\u6570\u53d8\u4e3a0\u65f6\u5185\u5b58\u91ca\u653e\u3002\u8bbe\u8ba1\u601d\u60f3\u4e0a\u7c7b\u4f3c\u4e8eC++\u7684\u667a\u80fd\u6307\u9488\uff0c\u4f46\u7531\u4e8e<code>redisObject<\/code>\u4e0d\u5b58\u5728\u5d4c\u5957\u4f7f\u7528\uff0c\u6545\u4e0d\u4f1a\u51fa\u73b0C++\u667a\u80fd\u6307\u9488\u4e2d\u7684\u5faa\u73af\u5f15\u7528\u95ee\u9898\u3002<br \/>\n\u6b64\u5916\uff0credis\u51fa\u4e8e\u8282\u7701\u5185\u5b58\u7684\u8003\u8651\uff0c\u652f\u6301\u5bf9\u8c61\u7684\u5171\u4eab\u673a\u5236\uff0c\u5373\u4e0d\u540c\u7684\u952e\u5bf9\u8c61\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a<code>redisObject<\/code>\u4f5c\u4e3a\u503c\u5bf9\u8c61\uff0c\u6b64\u673a\u5236\u4e5f\u4f9d\u8d56\u4e8e\u5f15\u7528\u8ba1\u6570\u3002\u4f8b\u5982\uff0credis\u5728\u521d\u59cb\u5316\u65f6\uff0c\u4f1a\u76f4\u63a5\u521b\u5efa0~9999\u7684\u6574\u6570\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u4ee5\u5907\u5c06\u6765\u901a\u8fc7\u5bf9\u8c61\u5171\u4eab\u673a\u5236\u4f7f\u7528\u3002<br \/>\n<code>lru<\/code>\u8bb0\u5f55\u4e00\u4e2a\u5bf9\u8c61\u6700\u540e\u4e00\u6b21\u88ab\u547d\u4ee4\u7a0b\u5e8f\u8bbf\u95ee\u7684\u65f6\u95f4\uff0c\u5982\u679c\u670d\u52a1\u5668\u8bbe\u7f6e\u4e86\u76f8\u5e94\u7684\u5185\u5b58\u56de\u6536\u673a\u5236\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6b64\u6210\u5458\u53d8\u91cf\u627e\u5230\u7a7a\u8f6c\u65f6\u95f4\u8f83\u9ad8\u7684\u952e\uff0c\u5e76\u5c06\u4ed6\u4eec\u91ca\u653e\u3002<\/p>\n<h2>2.1 \u5b57\u7b26\u4e32 String<\/h2>\n<p>\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u6570\u5b57\uff0c\u6570\u7ec4\u4e43\u81f3\u56fe\u50cf\u3002<\/p>\n<h3>2.1.1 \u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>int\u6216\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32(SDS)\u3002<br \/>\n\u82e5\u5b57\u7b26\u4e32\u4fdd\u5b58\u7684\u662f\u53ef\u4ee5\u7528long\u7c7b\u578b\u8868\u793a\u7684\u6574\u6570\u503c\uff0c\u90a3\u4e48\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u7f16\u7801<code>encoding<\/code>\u4f1a\u88ab\u7f6e\u4e3a<code>REDIS_ENCODING_INT<\/code>\uff0c<code>ptr<\/code>\u4f1a\u7531void * \u7c7b\u578b\u6539\u4e3along\u7c7b\u578b\uff0c\u5e76\u76f4\u63a5\u5c06\u8fd9\u4e2a\u6574\u6570\u503c\u4fdd\u5b58\u5728<code>ptr<\/code>\u4e2d\u3002\uff08\u6ce8\u610f\uff1a\u6d6e\u70b9\u6570\u4e0d\u80fd\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u9700\u8981\u5f53\u505a\u5b57\u7b26\u4e32\u5b58\uff09<br \/>\n\u4fdd\u5b58\u5b57\u7b26\u4e32\u65f6\uff0c\u867d\u7136\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u90fd\u91c7\u7528SDS\uff0c\u4f46\u662f\u4ecd\u7136\u5206\u4e3a\u4e24\u79cd\u4e0d\u540c\u7684\u7f16\u7801\u3002\u5f53\u5b57\u7b26\u4e32\u957f\u5ea6\u5c0f\u4e8e39\u5b57\u8282\u65f6\uff0c\u7f16\u7801\u7f6e\u4e3a<code>REDIS_ENCODING_EMBSTR<\/code>\uff0c\u6b64\u65f6\u4f1a\u5728\u5185\u5b58\u4e0a\u7528\u4e00\u5757\u8fde\u7eed\u7684\u533a\u57df\u5b58\u653eredis\u5bf9\u8c61\u7ed3\u6784\u4f53<code>redisObject<\/code>\u548cSDS\u7ed3\u6784\u4f53<code>sdshdr<\/code>\uff0c\u6b64\u65b9\u6cd5\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5206\u914d\u4e0e\u91ca\u653e\u7684\u6b21\u6570\uff0c\u4e14\u53ef\u4ee5\u5145\u5206\u5229\u7528\u7f13\u5b58\u673a\u5236\uff0c\u4e00\u6b21\u6027\u52a0\u8f7d\u5bf9\u8c61\u548c\u6570\u636e\uff1b\u5f53\u5b57\u7b26\u4e32\u957f\u5ea6\u5927\u4e8e39\u5b57\u8282\u65f6\uff0c\u7f16\u7801\u7f6e\u4e3a<code>REDIS_ENCODING_RAW<\/code>\uff0c\u6b64\u65f6redis\u5bf9\u8c61\u7ed3\u6784\u4f53\u548cSDS\u7ed3\u6784\u4f53\u4f1a\u5206\u522b\u521b\u5efa\u3002<br \/>\n\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0credis\u5bf9\u4e8eembstr\u7f16\u7801\u6ca1\u6709\u5199\u4efb\u4f55\u7684\u4fee\u6539\u7a0b\u5e8f\uff0c\u6240\u4ee5\u9700\u8981\u4fee\u6539\u65f6\u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3araw\u7f16\u7801\u3002<\/p>\n<h3>2.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u7531\u4e8eredis\u7684\u5355\u7ebf\u7a0b\u6a21\u578b\u6027\u8d28\uff0c\u53ef\u4ee5\u5b58\u6570\u5b57\u7528\u505a\u8ba1\u6570\uff0c\u4f8b\u5982\u7c89\u4e1d\u6570\u7b49\u3002<\/li>\n<\/ul>\n<h2>2.2 \u54c8\u5e0c\u8868 hash<\/h2>\n<p>\u952e\u503c\u5bf9\u54c8\u5e0c\u8868\uff0c\u5176\u503c\u672c\u8eab\u4e5f\u662f\u952e\u503c\u5bf9\u7ed3\u6784\uff0c\u5373\u6574\u4f53\u7ed3\u6784\u662f\uff1a\u952e-\u5b57\u6bb5-\u503c(key-field-value)\uff0c\u4e00\u4e2akey\u4e0b\u53ef\u4ee5\u5b58\u50a8\u591a2^31\u6761field-value\u5b57\u6bb5\/\u503c\u3002<\/p>\n<h3>2.2.1 \u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>\u6709\u538b\u7f29\u5217\u8868\uff08ziplist\uff09\u548c\u5b57\u5178\uff08dict\uff09\u4e24\u79cd\u5b9e\u73b0\u3002<br \/>\n\u9ed8\u8ba4\u7684\u7a7a\u767d\u54c8\u5e0c\u8868\u91c7\u7528ziplist\uff0c\u5f53\u67d0\u4e2a\u952e\u6216\u503c\u7684\u957f\u5ea6\u8fbe\u5230\u9608\u503c(\u9ed8\u8ba464\u5b57\u8282)\uff0c\u6216\u8005ziplist\u7684\u8282\u70b9\u4e2a\u6570\u8fbe\u5230\u9608\u503c(\u9ed8\u8ba4512\u4e2a)\uff0c\u7ed3\u6784\u4f1a\u8f6c\u6362\u4e3adict\u3002<br \/>\n\u5f53\u91c7\u7528ziplist\u5b9e\u73b0\uff0c\u63d2\u5165\u65b0\u7684\u952e\u503c\u5bf9\u65f6\uff0c\u603b\u662f\u5728\u538b\u7f29\u5217\u8868\u7684\u5c3e\u90e8\u5148\u63a8\u5165\u4e00\u4e2a\u4fdd\u5b58\u4e86\u952e(field)\u7684\u538b\u7f29\u5217\u8868\u8282\u70b9\uff0c\u518d\u63a8\u5165\u4e00\u4e2a\u4fdd\u5b58\u503c(value)\u7684\u538b\u7f29\u5217\u8868\u8282\u70b9\u3002\u5f53\u91c7\u7528dict\u5b9e\u73b0\uff0c\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e(field)\u548c\u503c(value)\u90fd\u662fSDS\u3002<\/p>\n<h3>2.2.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u5b58\u7ed3\u6784\u5316\u5bf9\u8c61\u6570\u636e\uff0c\u5373\u201c\u6210\u5458\u53d8\u91cf-\u503c\u201d\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\u8d2d\u7269\u8f66\u573a\u666f\uff0ckey\u662f\u7528\u6237id\uff0cfield\u662f\u5546\u54c1id\uff0cvalue\u662f\u5546\u54c1\u6570\u91cf<\/li>\n<\/ul>\n<h2>2.3 \u5217\u8868 list<\/h2>\n<p>\u5143\u7d20\u5217\u8868\uff0c\u53ef\u4ee5\u5728\u5de6\u53f3\u4e24\u7aef\u8fdb\u884c\u63d2\u5165\u548c\u5f39\u51fa\u64cd\u4f5c\uff1b\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u4e0b\u6807\u83b7\u53d6\u67d0\u4e2a\u5143\u7d20(\u4f8b\u5982\uff0cLINDEX mylist 0\u83b7\u53d6mylist\u6b63\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20)\u6216\u67d0\u4e2a\u8303\u56f4\u5185\u7684\u5143\u7d20\u5217\u8868<\/p>\n<h3>2.3.1 \u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>\u6709\u538b\u7f29\u5217\u8868(ziplist)\u548c\u53cc\u7aef\u94fe\u8868\uff08list\uff09\u4e24\u79cd\u5b9e\u73b0\u3002<br \/>\n\u9ed8\u8ba4\u7684\u7a7a\u767d\u5217\u8868\u91c7\u7528ziplist\uff0c\u5f53\u5c1d\u8bd5\u65b0\u589e\u7684string\u5927\u5c0f\u8fbe\u5230\u9608\u503c(\u9ed8\u8ba464\u5b57\u8282)\uff0c\u6216ziplist\u8282\u70b9\u6570\u91cf\u8fbe\u5230\u9608\u503c(\u9ed8\u8ba4512\u4e2a)\u65f6\uff0c\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3alist\u3002<strong>\u5728\u4f7f\u7528list\u4f5c\u4e3a\u5e95\u5c42\u5b9e\u73b0\u65f6\uff0c\u5176\u6bcf\u4e00\u4e2a\u94fe\u8868\u8282\u70b9\u7684\u6570\u636e\u5bf9\u8c61\u90fd\u662fSDS\u3002<\/strong><\/p>\n<h3>2.3.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u505a\u65f6\u95f4\u7ebf\u3001\u6d88\u606f\u961f\u5217\uff1a\u5de6\u8fdb\u53f3\u51fa\u3002<\/li>\n<li>\u505a\u6570\u636e\u5206\u9875\uff1a\u6bd4\u5982\u8bc4\u8bba\u533a\u5206\u9875\uff0clrange\u547d\u4ee4\uff0c\u83b7\u53d6\u67d0\u4e2a\u8303\u56f4\u5185\u8bc4\u8bba<\/li>\n<\/ul>\n<h2>2.4 \u96c6\u5408 set<\/h2>\n<p>\u81ea\u52a8\u53bb\u91cd\u7684\u65e0\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u4ea4\u96c6\u5e76\u96c6\u5dee\u96c6\u7b49\u96c6\u5408\u8fd0\u7b97\u3002<br \/>\n\u7531\u4e8e\u65e0\u5e8f\u6027\uff0c\u4e0d\u80fd\u901a\u8fc7\u7d22\u5f15\u4e0b\u6807\u83b7\u53d6\u5143\u7d20\u3002<\/p>\n<h3>2.4.1 \u5e95\u5c42\u5b9e\u73b0<\/h3>\n<p>\u6709\u6574\u6570\u96c6\u5408\uff08intset\uff09\u548c\u5b57\u5178\uff08dict\uff09\u4e24\u79cd\u5b9e\u73b0\u3002<br \/>\n\u7531\u63d2\u5165\u96c6\u5408\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u51b3\u5b9a\u5176\u521d\u59cb\u5b9e\u73b0\u7c7b\u578b\uff0c\u82e5\u6b64\u5143\u7d20\u53ef\u4ee5\u88ab\u8868\u793a\u4e3aLong long\uff08\u5373\u662f\u4e00\u4e2a\u6574\u6570\uff09\uff0c\u5219\u4e3aintset\uff1b\u5426\u5219\u662fdict\u3002\u5bf9\u4e8e\u4e00\u4e2aintset\u7c7b\u578b\u7684set\uff0c\u5f53\u96c6\u5408\u5143\u7d20\u6570\u91cf\u8fbe\u5230\u9608\u503c(\u9ed8\u8ba4512\u4e2a)\uff0c\u6216\u8005\u5c1d\u8bd5\u63d2\u5165\u4e00\u4e2a\u4e0d\u80fd\u88ab\u8868\u793a\u4e3along long\u7684\u6570\u636e\u65f6\uff0c\u8f6c\u6362\u4e3adict\u3002<strong>\u5728\u4f7f\u7528dict\u4f5c\u4e3a\u5e95\u5c42\u5b9e\u73b0\u65f6\uff0c\u5176\u6bcf\u4e00\u4e2a\u952e\u7684\u6570\u636e\u5bf9\u8c61\u90fd\u662fSDS\uff0c\u503c\u90fd\u662fNULL\u3002<\/strong><\/p>\n<h3>2.4.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u6807\u7b7e\u529f\u80fd<\/li>\n<li>\u5229\u7528\u5176\u53bb\u91cd\u7279\u6027\u548c\u96c6\u5408\u8fd0\u7b97\u7279\u6027\uff0c\u5b58\u653e\u597d\u53cb\u5217\u8868\uff0c\u5b9e\u73b0\u5171\u540c\u597d\u53cb\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2>2.5 \u6709\u5e8f\u96c6 zset<\/h2>\n<p>\u6709\u5e8f\u3001\u81ea\u52a8\u53bb\u91cd\u7684\u96c6\u5408\uff0c\u6bcf\u4e2a\u96c6\u5408\u5143\u7d20\u9700\u8981\u4fdd\u5b58\u4e00\u4e2a\u5206\u503c(score)\u4f5c\u4e3a\u6392\u5e8f\u7684\u4f9d\u636e\uff0c\u6309\u7167score\u5347\u5e8f\u6392\u5217\u3002<br \/>\n\u5f62\u5f0f\u4e0a\u6709\u4e00\u70b9\u7c7b\u4f3c\u4e8ehash\uff0ckey-field-value\u5bf9\u5e94\u6709\u5e8f\u96c6\u5408\u7684key-member-score<\/p>\n<h3>2.5.1 \u5e95\u5c42\u539f\u7406<\/h3>\n<p>\u6709\u538b\u7f29\u5217\u8868\uff08ziplist\uff09\u548c\u8df3\u8868+\u5b57\u5178\uff08skiplist + dict\uff09\u4e24\u79cd\u5b9e\u73b0\u3002<br \/>\n\u7531\u63d2\u5165\u6709\u5e8f\u96c6\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u51b3\u5b9a\u5176\u521d\u59cb\u5b9e\u73b0\u7c7b\u578b\uff0c\u5f53member\u957f\u5ea6\u5c0f\u4e8e\u9608\u503c(\u9ed8\u8ba4128\u4e2a)\uff0c\u5219\u8bbe\u7f6e\u4e3aziplist\uff0c\u5426\u5219\u4e3askiplist\u3002\u5bf9\u4e8e\u4e00\u4e2aziplist\u7c7b\u578b\u7684zset\uff0c\u5f53\u8282\u70b9\u6570\u91cf\u8fbe\u5230\u9608\u503c\uff0c\u6216\u8005\u5c1d\u8bd5\u65b0\u589e\u4e00\u4e2amember\u5927\u5c0f\u8fbe\u5230\u9608\u503c(64\u5b57\u8282)\u7684\u6570\u636e\u65f6\uff0c\u7ed3\u6784\u8f6c\u6362\u4e3askiplist\u3002<br \/>\n\u5f53\u91c7\u7528ziplist\u5b9e\u73b0\uff0c\u63d2\u5165\u65b0\u7684\u96c6\u5408\u5143\u7d20\u65f6\uff0c\u603b\u662f\u5728\u6392\u5e8f\u7684\u4f4d\u7f6e\u5148\u63a8\u5165\u4e00\u4e2a\u4fdd\u5b58\u6210\u5458(member)\u7684\u538b\u7f29\u5217\u8868\u8282\u70b9\uff0c\u518d\u63a8\u5165\u4e00\u4e2a\u4fdd\u5b58\u5206\u503c(score)\u7684\u538b\u7f29\u5217\u8868\u8282\u70b9\uff0c\u4e24\u4e2a\u8282\u70b9\u5728\u5185\u5b58\u4e0a\u8fde\u7eed\uff1b<br \/>\n\u5f53\u91c7\u7528skiplist+dict\u5b9e\u73b0\uff0cskiplist\u6bcf\u4e2a\u8282\u70b9\u7684<code>object<\/code>\u5c5e\u6027\u4fdd\u5b58\u6210\u5458\uff0c<code>score<\/code>\u5c5e\u6027\u4fdd\u5b58\u5206\u503c\uff0c\u6309\u5206\u503c\u5347\u5e8f\u6392\u5e8f\uff1bdict\u5219\u4f5c\u4e3a\u6240\u6709\u6210\u5458\u5230\u5206\u503c\u7684\u6620\u5c04\uff0c\u6bcf\u4e2a\u5b57\u5178\u6210\u5458\u7684field\u4fdd\u5b58\u6210\u5458\uff0cvalue\u4fdd\u5b58\u5206\u503c\u3002\u901a\u8fc7\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u67e5\u627e\u7ed9\u5b9a\u6210\u5458\u7684\u5206\u503c\u7684\u590d\u6742\u5ea6\u53d8\u4e3aO(1)\u3002skip\u548cdict\u901a\u8fc7\u6307\u9488\u7684\u5f62\u5f0f\u5171\u4eab\u6210\u5458\u548c\u5206\u503c\u7684\u5185\u5b58\u3002<br \/>\n<strong>\u5728zset\u4e2d\uff0c\u6210\u5458\u603b\u662fSDS\uff0cscore\u603b\u662fdouble\u7c7b\u578b\u7684\u6d6e\u70b9\u6570\u3002<\/strong><\/p>\n<h3>2.5.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u5b58\u653e\u76f4\u64ad\u95f4\u7528\u6237\u5217\u8868\uff0c\u4f7f\u7528score\u6392\u5e8f\u505a\u6392\u884c\u699c\u3002<\/li>\n<\/ul>\n<h1>3. Redis\u7684\u5185\u90e8\u6570\u636e\u7ed3\u6784<\/h1>\n<p>\u6574\u4f53\u6765\u770b\uff0c\u5206\u4e3a4\u79cd\u666e\u901a\u5185\u90e8\u6570\u636e\u7ed3\u6784(SDS, list, dict, skiplist)\u548c2\u79cd\u5185\u5b58\u6620\u5c04\u6570\u636e\u7ed3\u6784(intset, ziplist)\u3002\u5728\u6570\u636e\u89c4\u6a21\u8f83\u5c0f\u65f6\u7528\u540e\u4e24\u8005\u8282\u7701\u5185\u5b58\u3002<\/p>\n<h2>3.1 \u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32 Simple Dynamic String, SDS<\/h2>\n<p>\u52a8\u6001\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u7ed3\u6784\u4f53\u7ef4\u62a4\u4e86\u5df2\u4f7f\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u548c\u5df2\u5206\u914d\u4f46\u672a\u4f7f\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\uff1b\u6570\u7ec4\u7c7b\u578b\u662fchar\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e9b\u7a7a\u95f4\u5927\u5c0f\u8ba4\u4e3a\u662f\u5b57\u7b26\u957f\u5ea6\u3002<\/p>\n<pre><code class=\"language-c++\">struct sdshdr {\n    int len;\n    int free;\n    char buf[];\n}<\/code><\/pre>\n<p>\u4e0eC\u5b57\u7b26\u4e32\u7c7b\u4f3c\uff0c\u5b57\u8282\u6570\u636e<code>buf<\/code>\u4e2d\u7684\u5b57\u7b26\u4e32\u4f1a\u4ee5\u7a7a\u5b57\u7b26'\\0'\u7ed3\u5c3e\uff0c\u6b64\u7a7a\u5b57\u7b26\u4e0d\u4f1a\u8ba1\u5165\u5df2\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f<code>len<\/code>\uff0c\u4e5f\u4e0d\u4f1a\u88ab\u8ba1\u5165<code>free<\/code>\u3002<\/p>\n<h3>3.1.1 SDS\u76f8\u8f83\u4e8eC\u5b57\u7b26\u4e32\u7684\u4f18\u52bf<\/h3>\n<ol>\n<li>\u7531\u4e8e\u7ef4\u62a4\u4e86\u957f\u5ea6<code>len<\/code>\uff0c\u76f8\u8f83\u4e8eC\u5b57\u7b26\u4e32\uff0cSDS\u53ef\u4ee5\u5728O(1)\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8ba1\u7b97strlen\uff1b<\/li>\n<li>\u4fee\u6539\u5b57\u7b26\u4e32\u65f6\u80fd\u591f\u9884\u5148\u68c0\u67e5\u5df2\u5206\u914d\u7a7a\u95f4\u662f\u5426\u5145\u8db3\u5e76\u8fdb\u884c\u989d\u5916\u7a7a\u95f4\u5206\u914d\uff0c\u4ece\u800c\u89e3\u51b3\u7f13\u51b2\u533a\u6ea2\u51fa\u7684\u95ee\u9898\u3002<\/li>\n<li>\u4fee\u6539\u5b57\u7b26\u4e32\u957f\u5ea6\u65f6\u9700\u8981\u7684\u5185\u5b58\u5206\u914d\u6b21\u6570\u66f4\u5c11\u3002C\u5b57\u7b26\u4e32\u6bcf\u6b21\u4fee\u6539\u957f\u5ea6\u65f6\u90fd\u9700\u8981\u91cd\u65b0\u8c03\u6574\u5185\u5b58\u4ee5\u9002\u5e94\u65b0\u7684\u957f\u5ea6\uff0c\u800cSDS\u5b9e\u73b0\u4e86\u7a7a\u95f4\u9884\u5206\u914d\u548c\u60f0\u6027\u7a7a\u95f4\u56de\u6536\u673a\u5236\u6765\u964d\u4f4e\u5185\u5b58\u5206\u914d\u7684\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u7a7a\u95f4\u9884\u5206\u914d\u6307\u5728\u5bf9SDS\u8fdb\u884c\u6269\u5bb9\u65f6\uff0c\u4f1a\u591a\u5206\u914d\u4e00\u90e8\u5206\u7a7a\u95f4\u4f5c\u4e3a\u4f59\u91cf\uff1b\u60f0\u6027\u7a7a\u95f4\u56de\u6536\u673a\u662f\u6307\u5728\u7f29\u77edSDS\u65f6\uff0c\u4e0d\u7acb\u523b\u56de\u6536\u7a7a\u95f4\uff0c\u800c\u662f\u5c06\u7a7a\u51fa\u6765\u7684\u90e8\u5206\u4f5c\u4e3a\u672a\u4f7f\u7528\u7a7a\u95f4\u8ba1\u5165<code>free<\/code>\u4e2d\u3002<\/li>\n<li>\u4e8c\u8fdb\u5236\u5b89\u5168(\u5305\u62ecSDS\u7684\u6240\u6709API\u5728\u5185)\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u6570\u5b57\uff0c\u6570\u7ec4\u4e43\u81f3\u56fe\u50cf\u3002\u800cC\u5b57\u7b26\u4e32\u53ea\u80fd\u5b58\u653e\u67d0\u79cd\u7f16\u7801\u4e0b\u7684\u6587\u672c\uff0c\u4e14\u4f1a\u51fa\u73b0\u4ee5\u7b2c\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4f5c\u4e3a\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u95ee\u9898\u3002<\/li>\n<\/ol>\n<h3>3.1.2 SDS\u5728Redis\u5185\u90e8\u529f\u80fd\u7684\u5e94\u7528<\/h3>\n<ul>\n<li>redis\u4e2d\u6240\u6709\u952e\u503c\u5bf9\u7684\u952e<\/li>\n<li>\u505a\u7f13\u5b58\uff0cAOF\u7f13\u51b2\u533a\u3001\u5ba2\u6237\u7aef\u7684\u8f93\u5165\u7f13\u51b2\u533a<\/li>\n<\/ul>\n<h2>3.2 \u94fe\u8868 list<\/h2>\n<p>redis\u7684\u94fe\u8868\u662f\u53cc\u7aef\u3001\u53cc\u5411\u3001\u65e0\u73af\u94fe\u8868\uff0c\u5185\u90e8\u6570\u636e\u53ef\u4ee5\u91cd\u590d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u4e0b\u6807\u83b7\u53d6\u5143\u7d20\u3002<br \/>\n\u8282\u70b9\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct listNode {\n    struct listNode *prev;              \/\/ \u524d\u9a71\u6307\u9488\n    struct listNode *next;              \/\/ \u540e\u7ee7\u6307\u9488\n    void *value;                        \/\/ \u503c\n} listNode;<\/code><\/pre>\n<p>\u5934\u7ed3\u70b9\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct list {\n    listNode *head;                     \/\/ \u8868\u5934\u6307\u9488 \n    listNode *tail;                     \/\/ \u8868\u5c3e\u6307\u9488\n    unsigned long len;                  \/\/ \u8282\u70b9\u6570\u91cf\n    void *(*dup)(void *ptr);            \/\/ \u590d\u5236\u51fd\u6570\n    void (*free)(void *ptr);            \/\/ \u91ca\u653e\u51fd\u6570\n    int (*match)(void *ptr, void *key); \/\/ \u6bd4\u5bf9\u51fd\u6570\n} list;<\/code><\/pre>\n<p>\u5934\u7ed3\u70b9\u5305\u542b\u6307\u5411\u94fe\u8868\u5934\u5c3e\u8282\u70b9\u7684\u6307\u9488\uff0c\u5934\u5c3e\u7684\u64cd\u4f5c\u90fd\u662fO(1)\u590d\u6742\u5ea6\uff1b\u4fdd\u5b58\u4e86\u5f53\u524d\u94fe\u8868\u957f\u5ea6\uff0c\u6545\u6709\u7740O(1)\u7684\u957f\u5ea6\u8ba1\u7b97\u590d\u6742\u5ea6\uff1b\u7531\u4e8e\u662f\u94fe\u8868\uff0c\u6709\u7740O(n)\u7684\u67e5\u627e\u590d\u6742\u5ea6\u3002<br \/>\n\u7531\u4e8e\u503c\u7c7b\u578b\u662fvoid\u6307\u9488\uff0c\u4e14\u53ef\u4ee5\u81ea\u5b9a\u4e49\u590d\u5236\u3001\u91ca\u653e\u548c\u6bd4\u5bf9\u51fd\u6570\uff0c\u6545redis\u7684\u94fe\u8868\u662f<strong>\u591a\u6001<\/strong>\u7684\uff0c\u4e00\u4e2a\u94fe\u8868\u53ef\u4ee5\u4fdd\u5b58\u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<h3>3.2.1 \u94fe\u8868\u5728redis\u5185\u90e8\u529f\u80fd\u7684\u5e94\u7528<\/h3>\n<ul>\n<li>\u4f5c\u4e3a\u5217\u8868\u7684\u5e95\u5c42\u7ed3\u6784\u4e4b\u4e00<\/li>\n<li>\u4fdd\u5b58\u8f93\u5165\u7684\u547d\u4ee4<\/li>\n<li>\u5b9e\u73b0\u53d1\u5e03\u4e0e\u8ba2\u9605\u529f\u80fd<\/li>\n<\/ul>\n<h2>3.3 \u5b57\u5178 dict<\/h2>\n<p>\u7528\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u5b57\u5178\u7ed3\u6784\u4f53dict\u5305\u542b\u4e24\u4e2a\u54c8\u5e0c\u8868\u6307\u9488\uff0c0\u53f7\u662f\u4e3b\u8981\u4f7f\u7528\u7684\u54c8\u5e0c\u8868\uff0c1\u53f7\u53ea\u6709\u5728rehash\u65f6\u624d\u542f\u7528\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct dict {\n    dictType *type;     \/\/ \u4fdd\u5b58\u4e86\u4e00\u7ec4\u64cd\u4f5c\u7279\u5b9a\u7c7b\u578b\u952e\u503c\u5bf9\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u54c8\u5e0c\u7b97\u6cd5\n    void *privdata;     \/\/ \u4fdd\u5b58\u4f20\u9012\u7ed9\u4e0a\u8ff0\u64cd\u4f5c\u51fd\u6570\u7684\u53ef\u9009\u53c2\u6570\n    dictht ht[2];       \/\/ \u54c8\u5e0c\u8868\uff082 \u4e2a\uff09\n    int rehashidx;      \/\/ rehash\u7d22\u5f15\uff0c\u503c\u4e3a-1\u8868\u793arehash\u672a\u8fdb\u884c\n} dict;<\/code><\/pre>\n<p>\u54c8\u5e0c\u8868\u8868\u5934dictht\u4fdd\u5b58\u4e86\u5f53\u524d\u54c8\u5e0c\u8868\u5b58\u653e\u7684\u8282\u70b9\u4e2a\u6570\u548c\u54c8\u5e0c\u8868\u7684\u5927\u5c0f(\u54c8\u5e0c\u6876\u5927\u5c0f)\uff0c\u7528\u4e8e\u8ba1\u7b97\u88c5\u8f7d\u56e0\u5b50\u3002\u7ed3\u6784\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct dictht {\n    dictEntry **table;          \/\/ \u54c8\u5e0c\u8868\u6570\u7ec4\n    unsigned long size;         \/\/ \u54c8\u5e0c\u8868\u5927\u5c0f(\u6876\u4e2a\u6570)\n    unsigned long sizemask;     \/\/ \u54c8\u5e0c\u8868\u5927\u5c0f\u63a9\u7801\uff0c\u7528\u4e8e\u8ba1\u7b97\u7d22\u5f15\uff0c\u603b\u662f\u7b49\u4e8esize - 1\n    unsigned long used;         \/\/ \u5df2\u6709\u8282\u70b9\u6570\u91cf\n} dictht;<\/code><\/pre>\n<p>redis\u54c8\u5e0c\u8868\u5185\u90e8\u91c7\u7528\u62c9\u94fe\u6cd5\u89e3\u51b3\u51b2\u7a81\uff0c\u5176\u54c8\u5e0c\u8868\u8282\u70b9dictEntry\u7ed3\u6784\u4f53\u4e2d\u4fdd\u5b58\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u3002\u4e3a\u4e86\u901f\u5ea6\u8003\u8651\uff0c\u65b0\u7684\u8282\u70b9\u603b\u662f\u88ab\u6dfb\u52a0\u5230\u94fe\u8868\u5934\uff0c\u4ece\u800c\u5f97\u5230O(1)\u590d\u6742\u5ea6\u3002<\/p>\n<pre><code class=\"language-c\">typedef struct dictEntry{\n    void *key;                  \/\/ \u952e\n    union {\n        void *val;\n        uint64_t u64;\n        int64_t s64;\n    } v;                        \/\/ \u503c\n    struct dictEntry *next;     \/\/ \u62c9\u94fe\u6cd5\u7684\u4e0b\u4e00\u8282\u70b9\n} dictEntry;<\/code><\/pre>\n<h3>3.3.1 rehash\u6761\u4ef6<\/h3>\n<p>\u8d1f\u8f7d\u56e0\u5b50\u5373\u4fdd\u5b58\u8282\u70b9\u6570\u91cf\/\u54c8\u5e0c\u6876\u6570\u91cf\u3002\u5f53\u5f00\u542f\u4e86\u81ea\u7136rehash\u65f6\uff0c\u8d1f\u8f7d\u56e0\u5b50 &gt;= 1\u5373\u4f1a\u89e6\u53d1\u81ea\u7136rehash\uff1b\u65e0\u8bba\u662f\u5426\u8bbe\u7f6e\uff0c\u5f53\u4fdd\u8d1f\u8f7d\u56e0\u5b50\u8fbe\u5230\u9608\u503c\uff08\u9ed8\u8ba45\uff09\u65f6\u5f3a\u5236\u89e6\u53d1\u3002<br \/>\n\u5f53redis\u4f7f\u7528\u5b50\u8fdb\u7a0b\u8fdb\u884c\u540e\u53f0\u6301\u4e45\u5316(\u6267\u884cBGSAVE\u6216\u8005BGREWRITEAOF\u547d\u4ee4)\u65f6\uff0c\u81ea\u7136rehash\u4f1a\u6682\u65f6\u5173\u95ed\u3002<br \/>\n\u5f53\u8d1f\u8f7d\u56e0\u5b50\u5c0f\u4e8e0.1\u65f6\uff0c\u89e6\u53d1\u54c8\u5e0c\u8868\u6536\u7f29\u3002<\/p>\n<h3>3.3.2 \u6e10\u8fdb\u5f0frehash<\/h3>\n<p>rehash\u7684\u8fc7\u7a0b\u5373\u4e3a\u5c06\u5f53\u524d\u76841\u53f7\u54c8\u5e0c\u8868\u5206\u914d\u4e00\u4e2a\u66f4\u5927\u7684\u7a7a\u95f4\uff0c\u7136\u540e\u5c060\u53f7\u8868\u7684\u5185\u5bb9\u9010\u6b65\u62f7\u8d1d\u52301\u53f7\u8868\uff0c\u5b8c\u6210\u540e\u6e05\u74060\u53f7\u8868\uff0c\u518d\u4e92\u6362\u4e24\u4e2a\u54c8\u5e0c\u8868\u6307\u9488\u3002<br \/>\n\u62f7\u8d1d\u8fc7\u7a0b\u662f\u6e10\u8fdb\u5f0f\u7684\uff0c\u5f53rehash\u5f00\u59cb\u540e\uff0c\u5c06<code>rehashindex<\/code>\u8bbe\u7f6e\u4e3a0\uff0c\u6b64\u540e\u6bcf\u6267\u884c\u4e00\u6b21\u6dfb\u52a0\/\u67e5\u627e\/\u5220\u9664\/\u66f4\u65b0\u65f6\uff0c\u90fd\u4f1a\u5c060\u53f7\u8868\u4e0a<code>rehashindex<\/code>\u7d22\u5f15\u4e0a\u7684\u6240\u6709\u5185\u5bb9\u62f7\u8d1d\u52301\u53f7\u8868\uff0c\u6700\u540e\u5c06<code>rehashindex<\/code>\u9012\u589e\u3002rehash\u7ed3\u675f\u540e\uff0c\u5c06<code>rehashindex<\/code>\u91cd\u65b0\u7f6e0.<br \/>\n\u6b64\u5916\uff0credis\u670d\u52a1\u5668\u8fd8\u6709\u4e00\u4e2a\u5e38\u89c4\u4efb\u52a1\u6267\u884crehash\uff0c\u5728\u56fa\u5b9a\u7684\u6beb\u79d2\u6570\u5185\u5c3d\u53ef\u80fd\u5730\u62f7\u8d1d\u6570\u636e\u3002<br \/>\n\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0crehash\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6240\u6709\u7684\u67e5\u627e\u3001\u5220\u9664\u64cd\u4f5c\u90fd\u8981\u8bbf\u95ee0\u30011\u4e24\u4e2a\u8868\uff0c\u53730\u53f7\u8868\u6ca1\u6709\u627e\u5230\u6570\u636e\u5219\u53bb1\u53f7\u8868\u91cc\u67e5\u627e\uff1b\u800c\u6dfb\u52a0\u64cd\u4f5c\u5219\u76f4\u63a5\u63d2\u51651\u53f7\u8868\u3002<\/p>\n<h3>3.3.3 \u5b57\u5178\u5728redis\u5185\u90e8\u529f\u80fd\u7684\u5e94\u7528<\/h3>\n<ul>\n<li>redis\u4f5c\u4e3aKV\u6570\u636e\u5e93\uff0c\u5176\u57fa\u7840\u7684KV\u529f\u80fd\u5c31\u662f\u7528\u5b57\u5178\u5b9e\u73b0\u7684\uff0c\u7531\u4e00\u4e2a\u88ab\u79f0\u4e3a\u952e\u7a7a\u95f4(key space)\u7684\u5b57\u5178\u5b58\u653e\u7740\u6240\u6709\u7684\u952e\u5bf9\u8c61\u548c\u503c\u5bf9\u8c61\uff0c\u952e\u7a7a\u95f4\u7684\u952e\u5373redis\u6570\u636e\u5e93\u7684\u952e\uff0c\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\uff1b\u952e\u7a7a\u95f4\u7684\u503c\u5373\u6570\u636e\u5e93\u7684\u503c\uff0c\u662f\u4e94\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff08\u5bf9\u8c61\uff09\u4e2d\u7684\u4efb\u4e00\u79cd\u3002<\/li>\n<li>\u4f5c\u4e3a\u54c8\u5e0c\u7684\u5e95\u5c42\u7ed3\u6784\u4e4b\u4e00<\/li>\n<\/ul>\n<h2>3.4 \u8df3\u8868 skiplist<\/h2>\n<p>\u8df3\u8868\u662f\u4e00\u79cd\u8bbe\u7f6e\u4e86\u591a\u7ea7\u5206\u5c42\u7d22\u5f15\u7684\u94fe\u8868\uff0c\u4ece\u4e0a\u5230\u4e0b\u8bb0\u5f55\u7684\u8282\u70b9\u8d8a\u6765\u8d8a\u591a\uff0c\u4e14\u4e0b\u4e00\u5c42\u6c38\u8fdc\u5305\u542b\u4e0a\u4e00\u5c42\u7684\u8282\u70b9\uff0c\u6700\u4e0b\u5c42\u662f\u5305\u542b\u6240\u6709\u8282\u70b9\u7684\u539f\u59cb\u94fe\u8868\u3002<br \/>\n\u67e5\u627e\u65f6\u4ece\u6700\u4e0a\u5c42\u5f00\u59cb\uff0c\u5b9a\u4f4d\u5de6\u53f3\u533a\u95f4\u7684\u4e24\u4e2a\u8282\u70b9\uff0c\u7136\u540e\u8fdb\u5165\u4e0b\u4e00\u5c42\uff0c\u5982\u6b64\u8fed\u4ee3\u3002\u67e5\u627e\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(logN)\uff0c\u6700\u574f\u590d\u6742\u5ea6O(N)\uff0c\u6027\u80fd\u4e0eAVL\u3001\u7ea2\u9ed1\u6811\u7c7b\u4f3c\uff0c\u4e14\u5177\u6709\u5b9e\u73b0\u7b80\u5355\u7684\u4f18\u52bf\u3002<br \/>\n\u8df3\u8868\u8282\u70b9\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct zskiplistNode {\n    struct zskiplistNode *backward;         \/\/ \u540e\u9000\u6307\u9488\n    double score;                           \/\/ zset\u6392\u5e8f\u4f9d\u636e\u7684\u5206\u503c\n    robj *obj;                              \/\/ \u6210\u5458\u5bf9\u8c61\n    struct zskiplistLevel {                 \/\/ \u5c42\n        struct zskiplistNode *forward;      \/\/ \u524d\u8fdb\u6307\u9488\n        unsigned int span;                  \/\/ \u8de8\u5ea6\n    } level[];\n} zskiplistNode;<\/code><\/pre>\n<p>\u6bcf\u4e2a\u201c\u5c42\u201d\u5305\u542b\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u548c\u8de8\u5ea6\uff0c\u8de8\u5ea6\u8bb0\u5f55\u4e86\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5c42\u6570\u8d8a\u9ad8\u8de8\u5ea6\u8d8a\u5927\u3002\u6b64\u5916\uff0c\u5728\u67e5\u627e\u4e00\u4e2a\u8282\u70b9\u4e2d\u7d2f\u52a0\u7ecf\u8fc7\u7684\u6240\u6709\u8282\u70b9\u7684\u8de8\u5ea6\u5c31\u53ef\u4ee5\u5f97\u5230\u8282\u70b9\u5728\u8df3\u8868\u4e2d\u7684\u6392\u4f4d\u3002<br \/>\n\u6bcf\u6b21\u6784\u9020\u65b0\u7684\u8df3\u8868\u8282\u70b9\u65f6\uff0c\u4f1a\u7531\u5e42\u6b21\u5b9a\u5f8b(power law\uff0c\u8d8a\u5927\u7684\u6570\u51fa\u73b0\u6982\u7387\u8d8a\u5c0f)\u751f\u6210\u4e00\u4e2a1~32\u95f4\u7684\u503c\u4f5c\u4e3a\u65b0\u8282\u70b9\u7684\u5c42\u6570\u3002redis\u4e2d\u5e42\u6b21\u53c2\u6570<code>ZSKIPLIST_P <\/code>\u8bbe\u7f6e\u4e3a0.25\uff0c\u8fd9\u6837\u4e0a\u4e00\u5c42\u7684\u8282\u70b9\u6570\u5927\u7ea6\u662f\u5176\u4e0b\u4e00\u5c42\u76841\/4\uff0c\u4ece\u6982\u7387\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c32\u5c42\u7684\u8df3\u8868\u5df2\u7ecf\u80fd\u591f\u5bb9\u7eb32^64\u4e2a\u8282\u70b9\uff0c\u5df2\u7ecf\u80fd\u6ee1\u8db3\u5927\u90e8\u5206\u573a\u666f\u3002<br \/>\n\u5bf9\u8c61\u6307\u9488\u90fd\u662f\u6307\u5411SDS\uff0c\u5bf9\u4e8e\u8df3\u8868\u8282\u70b9\u6765\u8bf4\uff0c\u6bcf\u4e2a\u8282\u70b9\u6307\u5411\u7684\u6210\u5458\u5bf9\u8c61\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u800c\u5206\u503c\u53ef\u4ee5\u76f8\u540c\u3002\u5206\u503c\u76f8\u540c\u65f6\uff0c\u987a\u5e8f\u7531\u6210\u5458\u5bf9\u8c61\u7684\u5b57\u5178\u5e8f\u51b3\u5b9a\u3002<\/p>\n<p>\u8df3\u8868\u8868\u5934\u4fdd\u5b58\u4e86\u5934\u5c3e\u6307\u9488\u3001\u8282\u70b9\u6570\u91cf\u548c\u6700\u5927\u5c42\u6570\u7b49\u4fe1\u606f\uff0c\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct zskiplist {\n    struct zskiplistNode *header, *tail;    \/\/ \u5934\u5c3e\u6307\u9488\n    unsigned long length;                   \/\/ \u8282\u70b9\u6570\u91cf\n    int level;                              \/\/ \u6700\u5927\u5c42\u6570\n}\n<\/code><\/pre>\n<h3>3.4.1 \u8df3\u8868\u5728redis\u5185\u90e8\u529f\u80fd\u7684\u5e94\u7528<\/h3>\n<ul>\n<li>\u4f5c\u4e3a\u6709\u5e8f\u96c6\u7684zset\u7684\u5e95\u5c42\u7ed3\u6784\u4e4b\u4e00<\/li>\n<li>\u5728\u96c6\u7fa4\u8282\u70b9\u4e2d\u7528\u4f5c\u5185\u90e8\u6570\u636e\u7ed3\u6784<\/li>\n<\/ul>\n<h3>3.4.2 \u8df3\u8868\u6bd4\u8d77\u7ea2\u9ed1\u6811\u7b49\u5e73\u8861\u4e8c\u53c9\u6811\u7684\u4f18\u52bf<\/h3>\n<ul>\n<li>\u5728\u67e5\u8be2\u6027\u80fd\u7c7b\u4f3c\u7684\u524d\u63d0\u4e0b\uff0c\u5b9e\u73b0\u7b80\u5355<\/li>\n<li>\u8303\u56f4\u67e5\u8be2\u6027\u80fd\u66f4\u9ad8\uff0cAVL\u5fc5\u987b\u4e2d\u5e8f\u904d\u5386\uff0c\u8df3\u8868\u5b9a\u4f4d\u5230\u7b26\u5408\u8303\u56f4\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u540e\u53ef\u4ee5\u76f4\u63a5\u6cbf\u7740\u6700\u4e0b\u5c42\u987a\u5e8f\u8bbf\u95ee<\/li>\n<li>\u63d2\u5165\/\u5220\u9664\u6027\u80fd\u66f4\u9ad8\uff0cAVL\u63d2\u5165\u5220\u9664\u540e\u5fc5\u987b\u8c03\u8282\u5b50\u6811\uff0c\u800c\u8df3\u8868\u53ea\u9700\u8981\u8c03\u8282\u524d\u540e\u8282\u70b9<\/li>\n<\/ul>\n<h2>3.5 \u6574\u6570\u96c6\u5408 intset<\/h2>\n<p>\u5e95\u5c42\u5b9e\u73b0\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5185\u90e8\u5143\u7d20<strong>\u4e0d\u91cd\u590d<\/strong>\u4e14<strong>\u6709\u5e8f<\/strong>\uff0c\u7531\u4e8e\u5176\u6709\u5e8f\u6027\uff0c\u63d2\u5165\u590d\u6742\u5ea6\u662fO(N)\uff0c\u67e5\u627e\u590d\u6742\u5ea6O(logN)\u3002\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct intset {\n    uint32_t encoding;  \/\/ \u7f16\u7801\u65b9\u5f0f\n    uint32_t length;    \/\/ \u5305\u542b\u7684\u5143\u7d20\u6570\u91cf\n    int8_t contents[];  \/\/ \u4fdd\u5b58\u5143\u7d20\u7684\u6570\u7ec4\n}<\/code><\/pre>\n<p>\u6839\u636e<code>encoding<\/code>\u7684\u53d6\u503c\uff0cintset\u7684\u7f16\u7801\u53ef\u4ee5\u8bbe\u7f6e\u4e3aint8_t\u3001int16_t\u548cint32_t\u4e09\u79cd\uff0c\u4f46\u662f<code>contents<\/code>\u8fd8\u662f\u58f0\u660e\u4e3aint8_t\uff0c\u53d6\u51fa\u6570\u636e\u9700\u8981\u6839\u636e\u5b9e\u9645\u7684\u7f16\u7801\u786e\u5b9a\u4f4d\u5bbd\u3002<br \/>\n\u5f53\u63d2\u5165\u65b0\u5143\u7d20\u7684\u4f4d\u5bbd\u5927\u4e8e\u5f53\u524d\u4f4d\u5bbd\u65f6\uff08\u4f8b\u5982\u5f53\u524d\u662fint8,\u65b0\u63d2\u5165\u4e86\u4e00\u4e2aint16\uff09\uff0c\u6570\u7ec4\u4f1a\u8fdb\u884c\u5347\u7ea7\uff0c\u5373\u5185\u5b58\u91cd\u5206\u914d\uff0c\u5e76\u5bf9\u73b0\u6709\u5143\u7d20\u8fdb\u884c\u4f4d\u5bbd\u62d3\u5c55\u548c\u62f7\u8d1d\uff0c\u5b8c\u6210\u5347\u7ea7\u540e\u518d\u8fdb\u884c\u65b0\u6570\u636e\u7684\u63d2\u5165\u3002\u5347\u7ea7\u673a\u5236\u4f7f\u5f97intset\u66f4\u7075\u6d3b\uff0c\u4e5f\u66f4\u80fd\u8282\u7701\u5185\u5b58\u3002\u6b64\u5916\uff0cintset\u4e0d\u652f\u6301\u964d\u7ea7\u64cd\u4f5c\u3002<\/p>\n<h2>3.6 \u538b\u7f29\u5217\u8868 ziplist<\/h2>\n<p>\u662f\u7531\u8fde\u7eed\u5185\u5b58\u5757\u7ec4\u6210\u7684\u987a\u5e8f\u578b\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u79cd\u5b58\u50a8\u65b9\u5f0f\u53ef\u4ee5\u5c3d\u53ef\u80fd\u5229\u7528\u7a7a\u95f4\uff0c\u5728\u4e00\u5757\u5185\u5b58\u4e0a\u7d27\u51d1\u5730\u5b58\u653e\u4e0d\u540c\u957f\u5ea6\u7684\u6570\u636e\u3002<br \/>\n\u4e00\u4e2a\u538b\u7f29\u5217\u8868\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u8282\u70b9(Entry)\uff0c\u6bcf\u4e2aEntry\u53ef\u4ee5\u4fdd\u5b58\u4e00\u4e2a\u6574\u6570\u6216\u662f\u957f\u5ea6\u6709\u9650\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u8282\u70b9\u7531\u5982\u4e0b\u51e0\u4e2a\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li><code>previous_entry_length<\/code> \u8bb0\u5f55<strong>\u524d\u4e00\u4e2a<\/strong>\u8282\u70b9\u7684\u957f\u5ea6\uff0c\u5982\u679c\u524d\u4e00\u4e2a\u8282\u70b9\u5c0f\u4e8e254\u5b57\u8282\uff0c\u5219\u672c\u9879\u957f\u5ea6\u4e3a1\u5b57\u8282\uff0c\u8db3\u4ee5\u5b58\u653e\u957f\u5ea6\uff1b\u5426\u5219\u672c\u9879\u957f\u5ea6\u4e3a5\u5b57\u8282\uff0c\u4e14\u7b2c\u4e00\u4e2a\u5b57\u8282\u4f1a\u88ab\u8bbe\u7f6e\u4e3a0xFE\u4f5c\u4e3a\u6807\u5fd7\uff0c\u5269\u4e0b4\u4e2a\u5b57\u8282\u7528\u6765\u4fdd\u5b58\u5b9e\u9645\u957f\u5ea6<\/li>\n<li><code>encoding<\/code> \u8bb0\u5f55\u672c\u8282\u70b9\u4fdd\u5b58\u5185\u5bb9\u7684\u7f16\u7801\u548c\u957f\u5ea6\u3002<\/li>\n<li><code>content<\/code> \u4fdd\u5b58\u8282\u70b9\u7684\u6570\u636e\u503c\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6574\u6570\u6216\u8005\u5b57\u8282\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p>\u7531\u4e8e\u6bcf\u4e2a\u8282\u70b9\u8bb0\u5f55\u4e86\u81ea\u5df1\u7684\u957f\u5ea6\u548c\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u957f\u5ea6\uff0c\u6545\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u4e0a\u4e00\u8282\u70b9\uff0c\u652f\u6301\u4ece\u540e\u5411\u524d\u7684\u904d\u5386\u3002<\/p>\n<h3>3.6.1 \u8fde\u9501\u66f4\u65b0\u95ee\u9898<\/h3>\n<p>\u7531\u4e8e<code>previous_entry_length<\/code>\u6839\u636e\u524d\u9a71\u8282\u70b9\u957f\u5ea6\u6765\u53d8\u6362\u957f\u5ea6\u7684\u6027\u8d28\uff0c\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u65f6\u53ef\u80fd\u5bfc\u81f4\u8fde\u9501\u66f4\u65b0\u95ee\u9898\u3002\u6bd4\u5982\u5728\u4e00\u4e32\u5927\u5c0f\u4e3a250-253\u5b57\u8282\u7684\u6570\u636e\u524d\u63d2\u5165\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e254\u5b57\u8282\u7684\u6570\u636e\uff0c\u4f1a\u5bfc\u81f4\u8fd9\u4e00\u4e32\u6570\u636e\u90fd\u8981\u8fdb\u884c<code>previous_entry_length<\/code>\u7684\u62d3\u5c55\uff1b\u6216\u8005\u5728\u4e00\u4e32\u5927\u5c0f\u4e3a250-253\u5b57\u8282\u7684\u6570\u636e\u524d\u5220\u9664\u4e86\u4e00\u4e2a\u6570\u636e\uff0c\u800c\u65b0\u7684\u524d\u4e00\u8282\u70b9\u5927\u5c0f\u5927\u4e8e\u7b49\u4e8e254\u5b57\u8282\uff0c\u540c\u6837\u4e5f\u4f1a\u5bfc\u81f4\u8fde\u9501\u66f4\u65b0\u3002\u82e5\u8981\u8fdb\u884cN\u4e2a\u8282\u70b9\u7684\u8fde\u9501\u66f4\u65b0\uff0c\u6bcf\u6b21\u7a7a\u95f4\u5206\u914d\u7684\u6700\u574f\u590d\u6742\u5ea6\u4e3aO(N)\uff0c\u8fde\u9501\u66f4\u65b0\u7684\u590d\u6742\u5ea6\u662fO(N^2)\u3002<br \/>\n\u5b9e\u9645\u4e0a\uff0c\u8fde\u9501\u66f4\u65b0\u51fa\u73b0\u7684\u6982\u7387\u5e76\u4e0d\u662f\u5f88\u9ad8\uff0c\u5728ziplist\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u8fd8\u662fO(N)\uff0c\u6700\u574fO(N^2)\u3002<\/p>\n<h1>4. Redis\u76f8\u8f83\u4e8e\u5176\u4ed6\u5de5\u5177\u7684\u4f18\u52bf<\/h1>\n<h2>4.1 \u5bf9\u6bd4MySQL<\/h2>\n<p>\u4f5c\u4e3a\u5185\u5b58\u6570\u636e\u5e93\uff0credis\u907f\u514d\u4e86\u6781\u5176\u8017\u65f6\u7684\u78c1\u76d8IO\uff0c\u5929\u751f\u9ad8\u6027\u80fd\u3001\u9ad8\u5e76\u53d1<\/p>\n<h2>4.2 \u5bf9\u6bd4C++ map<\/h2>\n<p>\u5404\u4e2a\u9ad8\u7ea7\u8bed\u8a00\u6240\u652f\u6301\u7684\u952e\u503c\u5bf9\u6570\u636e\u7ed3\u6784\u53ea\u80fd\u7528\u6765\u505a\u672c\u5730\u7f13\u5b58\uff0c\u5728\u591a\u5b9e\u4f8b\u7684\u573a\u666f\u4e0b\uff0c\u5404\u4e2a\u8282\u70b9\u5b58\u5728\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\uff1b\u800credis\u652f\u6301\u5206\u5e03\u5f0f\u7f13\u5b58\uff0c\u5404\u4e2a\u5b9e\u4f8b\u53ef\u4ee5\u5171\u6709\u4e00\u4efd\u7f13\u5b58\u6570\u636e\uff0c\u5177\u6709\u4e00\u81f4\u6027\u3002<\/p>\n<h2>4.3 \u5bf9\u6bd4memcached<\/h2>\n<ol>\n<li>memcached\u6ca1\u6709\u6301\u4e45\u5316\u529f\u80fd\uff0c\u65ad\u7535\u6570\u636e\u4e22\u5931\uff0credis\u6709\u6301\u4e45\u5316\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u78c1\u76d8\u3002<\/li>\n<li>memcached\u53ea\u6709string\u7c7b\u578b\uff0credis\u652f\u6301\u66f4\u4e30\u5bcc\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>memcached\u6ca1\u6709\u539f\u751f\u7684\u5206\u5e03\u5f0f\u652f\u6301\uff0c\u82e5\u8981\u5b9e\u73b0\u5fc5\u987b\u5728\u5ba2\u6237\u7aef\u4f7f\u7528\u4e00\u81f4\u6027\u54c8\u5e0c\u7b49\u5206\u5e03\u5f0f\u7b97\u6cd5\uff1b\u800credis\u6709\u539f\u751f\u7684\u4e3b\u4ece\u590d\u5236\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8f83\u5bb9\u6613\u5730\u5b9e\u73b0\u8bfb\u5199\u5206\u79bb\u3002<\/li>\n<li>memcached\u7684value\u6700\u59271MB\uff1bredis\u53ef\u4ee5512MB<\/li>\n<li>memcached\u662f\u591a\u7ebf\u7a0b\u3001\u975e\u963b\u585eIO\uff1bredis\u662f\u5355\u7ebf\u7a0b\uff0c\u901a\u8fc7\u975e\u963b\u585eIO\u591a\u8def\u590d\u7528\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\uff08\u6700\u540e\u8fd9\u6761\u4e3b\u8981\u4f5c\u4e3a\u533a\u522b\uff0c\u4e0d\u4e00\u5b9a\u662f\u4f18\u52bf\uff09\u5bf9\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\uff0credis\u7684\u5355\u7ebf\u7a0b\u6a21\u5f0f\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u5207\u6362\u7684\u5f00\u9500\uff0c\u6027\u80fd\u66f4\u9ad8\u3002<\/li>\n<\/ol>\n<h1>5. Reactor\u6a21\u5f0f\u4e0eRedis\u4e3a\u4ec0\u4e48\u5feb\uff1f<\/h1>\n<h2>5.1 Reactor\u6a21\u5f0f<\/h2>\n<p>\u4e00\u79cd\u5e38\u89c1\u7684\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u5f00\u53d1\u6a21\u5f0f\uff0c\u89c1\u4e8eredis\u3001netty\u7b49\u8f6f\u4ef6\u3002<br \/>\nreactor\u6a21\u5f0f\u662f\u4e00\u79cd<strong>\u4e8b\u4ef6\u9a71\u52a8<\/strong>\u673a\u5236\uff0c\u4e3b\u8981\u7531\u4e24\u4e2a\u90e8\u5206\u7ec4\u6210\uff1aReactor\u548cHandler\uff0cHandler\u4ee5<strong>\u975e\u963b\u585e\u65b9\u5f0f<\/strong>\u5904\u7406\u4e0eIO\u6709\u5173\u7684\u5b9e\u9645\u5de5\u4f5c\uff1b\u800cReactor\u8d1f\u8d23\u76d1\u542c\u548c\u5206\u53d1\u4e8b\u4ef6\uff0c\u4ee5<strong>IO\u591a\u8def\u590d\u7528\u673a\u5236<\/strong>\u76d1\u542c\u591a\u4e2asocket\uff0c\u54cd\u5e94IO\u4e8b\u4ef6\uff0c\u5e76\u628aIO\u5206\u53d1\u7ed9\u4e0d\u540cHandler\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h3>5.1.1 \u5355reactor\u5355\u7ebf\u7a0b\u6a21\u5f0f<\/h3>\n<p><strong>\u6574\u4e2a\u8fdb\u7a0b\u90fd\u662f\u5355\u7ebf\u7a0b<\/strong>\u3002\u82e5reactor\u6536\u5230\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\u5efa\u7acbIO\u4e8b\u4ef6\uff0c\u5219\u521b\u5efa\u4e00\u4e2ahandler\u5bf9\u8c61\u5904\u7406\u8be5\u4e8b\u4ef6\uff1b\u82e5reactor\u6536\u5230\u7684IO\u4e8b\u4ef6\u4e0d\u662f\u8fde\u63a5\u5efa\u7acb\uff0c\u5219\u4f7f\u7528\u6b64\u524d\u5df2\u521b\u5efa\u7684\u5bf9\u5e94handler\u8fdb\u884c\u5904\u7406\u3002<br \/>\n\u8fd9\u79cd\u7ed3\u6784\u4f18\u70b9\u662f\u7b80\u5355\uff0c\u6ca1\u6709\u591a\u7ebf\u7a0b\u7b49\u95ee\u9898\uff1b\u7f3a\u70b9\u662fhandler\u5904\u7406\u671f\u95f4\u6574\u4e2a\u8fdb\u7a0b\u65e0\u6cd5\u54cd\u5e94\u5176\u4ed6\u4e8b\u4ef6\uff0c\u6027\u80fd\u4f4e\uff1b\u4e14\u82e5handler\u5d29\u6e83\u6216\u8005\u6b7b\u5faa\u73af\u5219\u95ee\u9898\u66f4\u4e25\u91cd\u3002<br \/>\n\u4f7f\u7528\u672c\u6a21\u5f0f\u7684\u4f8b\u5b50\u6709\uff1aredis\u4e3b\u7ebf\u7a0b(6.0\u4e4b\u524d)<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic3.zhimg.com\/v2-e8baaf6a534a0f31e2108aab37103842_r.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic3.zhimg.com\/v2-e8baaf6a534a0f31e2108aab37103842_r.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u5355reactor\u5355\u7ebf\u7a0b\" \/><\/div><\/p>\n<h3>5.1.2 \u5355reactor\u591a\u7ebf\u7a0b\u6a21\u5f0f<\/h3>\n<p>reactor\u4ecd\u7136\u662f\u5355\u7ebf\u7a0b\uff0c\u4f46\u662fhandler\u5e76\u4e0d\u5305\u542b\u5904\u7406\u4e1a\u52a1\u7684\u4ee3\u7801\uff0c\u53ea\u662f\u5728\u8c03\u7528<code>read<\/code>\u8bfb\u53d6\u6570\u636e\u540e\uff0c<strong>\u4ea4\u7ed9\u7ebf\u7a0b\u6c60worker\u8fdb\u884c\u4e1a\u52a1\u5904\u7406<\/strong>\uff0c\u5de5\u4f5c\u7ebf\u7a0b\u5c06\u54cd\u5e94\u7ed3\u679c\u8fd4\u8fd8\u7ed9handler\uff0chandler\u518d\u8c03\u7528<code>send<\/code>\u5c06\u54cd\u5e94\u7ed3\u679c\u4ea4\u7ed9client\u3002<br \/>\n\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838cpu\u7684\u591a\u7ebf\u7a0b\u80fd\u529b\uff0c\u4f46\u662freactor\u4ecd\u65e7\u662f\u5355\u7ebf\u7a0b\uff0c\u5355\u4e00\u7ebf\u7a0b\u8d1f\u62c5\u4e86\u6240\u6709\u4e8b\u4ef6\u7684\u76d1\u542c\u548c\u54cd\u5e94\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5bb9\u6613\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002<br \/>\n\u4f7f\u7528\u672c\u6a21\u5f0f\u7684\u4f8b\u5b50\u6709\uff1aredis\u4e3b\u7ebf\u7a0b(\u5f00\u542f6.0\u5f15\u5165\u7684\u591a\u7ebf\u7a0bIO\u540e\uff0c\u4f46\u4e0d\u6807\u51c6\uff0c\u8be6\u89c15.2\u30016.4\u8282)<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic3.zhimg.com\/80\/v2-5754d9ebf6b9b621a1ff89545974b6ea_720w.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic3.zhimg.com\/80\/v2-5754d9ebf6b9b621a1ff89545974b6ea_720w.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u5355reactor\u591a\u7ebf\u7a0b\" \/><\/div><\/p>\n<h3>5.1.3 \u4e3b\u4ecereactor\u591a\u7ebf\u7a0b\u6a21\u5f0f<\/h3>\n<p>\u8fd9\u79cd\u7ed3\u6784\u8bbe\u7f6e\u4e86\u4e24\u4e2aReactor\u7ebf\u7a0b\uff0cmainReactor\u7ebf\u7a0b\u53ea\u8d1f\u8d23\u5904\u7406\u5efa\u7acb\u65b0\u8fde\u63a5\u7684IO\u4e8b\u4ef6\uff0c\u800c\u540e\u5c06\u5b8c\u6210\u5efa\u7acb\u7684\u8fde\u63a5\u4ea4\u7ed9SubReactor\u7ebf\u7a0b\uff0cSubReactor\u8d1f\u8d23\u76d1\u542c\u8fd9\u4e9b\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\uff0c\u5c06\u8fd9\u4e9b\u8fde\u63a5\u4e0a\u7684IO\u4e8b\u4ef6\u5206\u53d1\u7ed9Handler\u8fdb\u884c\u5904\u7406\u3002Handler\u4f9d\u7136\u5c06\u4e1a\u52a1\u5904\u7406\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u3002<br \/>\n\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u89e3\u51b3\u5355reactor\u6a21\u5f0f\u7531\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406\u8fde\u63a5\u4e8b\u4ef6\u548c\u8bfb\u5199\u4e8b\u4ef6\u7684\u7f3a\u70b9\uff0c\u5c06\u8017\u65f6\u7684IO\u8bfb\u5199\u4e8b\u4ef6\u5206\u7ed9SubReactor\u7ebf\u7a0b\uff0c\u4ece\u800c\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u54cd\u5e94\u6027\u80fd<br \/>\n\u4f7f\u7528\u672c\u6a21\u5f0f\u7684\u4f8b\u5b50\u6709\uff1aNGINX\uff0cnetty<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic2.zhimg.com\/80\/v2-ce188fec5a25fe51ea7a2151bdeff135_720w.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic2.zhimg.com\/80\/v2-ce188fec5a25fe51ea7a2151bdeff135_720w.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u4e3b\u4ecereator\u6a21\u5f0f\" \/><\/div><\/p>\n<h2>5.2 redis\u4e3a\u4ec0\u4e48\u5feb\uff1f<\/h2>\n<ol>\n<li>\u7edd\u5927\u591a\u6570\u90fd\u662f\u5185\u5b58\u64cd\u4f5c\uff0c\u4e14\u4f7f\u7528\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff1b<\/li>\n<li>\u4e3b\u7ebf\u7a0b\u91c7\u7528\u5355\u7ebf\u7a0b\u6a21\u578b\uff0c\u6ca1\u6709\u7ebf\u7a0b\u5207\u6362\u7684\u5f00\u9500\uff1b<\/li>\n<li>\u91c7\u7528\u4e86Reactor\u6a21\u5f0f\u5904\u7406IO\u4e8b\u4ef6\uff0c\u5373\u975e\u963b\u585eIO+IO\u591a\u8def\u590d\u7528\u3002\u57286.0\u7248\u672c\u4ee5\u524d\uff0credis\u662f\u5355reactor\u5355\u7ebf\u7a0b\u6a21\u5f0f\uff1b6.0\u7248\u672c\u4ee5\u540e\u5f15\u5165\u4e86\u591a\u7ebf\u7a0bIO\uff0c\u5728\u8be5\u6a21\u5f0f\u4e0b\u5de5\u4f5c\u5219\u662f\u4e00\u4e2a<strong>\u4e0d\u6807\u51c6<\/strong>\u7684\u5355reactor\u591a\u7ebf\u7a0b\u6a21\u5f0f\uff0c\u5176\u4e0d\u6807\u51c6\u7684\u5730\u65b9\u5728\u4e8e\u5176&quot;handler&quot;\u53ea\u4ee5\u591a\u7ebf\u7a0b\u7684\u65b9\u5f0f\u8fdb\u884cIO\u3001\u8bfb\u53d6\u5ba2\u6237\u7aef\u7684\u547d\u4ee4\u5e76\u89e3\u6790\uff0c\u800c\u547d\u4ee4\u7684\u6267\u884c\u5de5\u4f5c\u8fd8\u662f\u7531\u4e3b\u7ebf\u7a0b\u6267\u884c\u3002<\/li>\n<\/ol>\n<p>redis\u7684\u5355\u7ebf\u7a0breactor\u6a21\u578b\uff1a<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.taohuawu.club\/gallery\/single-threaded-redis.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.taohuawu.club\/gallery\/single-threaded-redis.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"redis\u7684\u5355\u7ebf\u7a0b\u6a21\u578b\" \/><\/div><\/p>\n<p>redis 6.0\u5f00\u542f\u591a\u7ebf\u7a0bIO\u540e\u7684\u4e0d\u6807\u51c6\u591a\u7ebf\u7a0b\u6a21\u578b(\u8be6\u89c16.4)\uff1a<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.taohuawu.club\/gallery\/multiple-threaded-redis.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.taohuawu.club\/gallery\/multiple-threaded-redis.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"redis\u7684\u591a\u7ebf\u7a0b\u6a21\u578b\" \/><\/div><\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/jishuin.proginn.com\/p\/763bfbd58a63\">https:\/\/jishuin.proginn.com\/p\/763bfbd58a63<\/a><br \/>\n<a href=\"https:\/\/strikefreedom.top\/archives\/multiple-threaded-network-model-in-redis\">https:\/\/strikefreedom.top\/archives\/multiple-threaded-network-model-in-redis<\/a><\/p>\n<h1>6. Redis\u7684\u7ebf\u7a0b\u6a21\u578b<\/h1>\n<h2>6.1 Redis\u7684\u5355\u7ebf\u7a0b\u6307\u7684\u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>\u4ece\u6574\u4e2a\u7cfb\u7edf\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5373\u4f7f\u662f\u8fdc\u53e4\u7248\u672c\u7684redis\u4e5f\u662f\u591a\u7ebf\u7a0b\u7684\uff0c\u5176\u6301\u4e45\u5316\u3001\u96c6\u7fa4\u540c\u6b65\u7b49\u529f\u80fd\u90fd\u7531\u72ec\u7acb\u4e8e\u4e3b\u7ebf\u7a0b\u5916\u7684\u53e6\u5916\u7ebf\u7a0b\u6267\u884c\uff1b4.0\u7248\u672c\u540eredis\u8fd8\u52a0\u5165\u4e86\u60f0\u6027\u5220\u9664(Lazy Free)\u673a\u5236\uff08\u57283.x\u7248\u672c\u4e2d\uff0c\u5982\u679c\u5220\u9664\u4e00\u4e2a\u503c\u5bf9\u8c61\u5de8\u5927\u7684\u952e\u2014\u2014\u4f8b\u5982\u5305\u542b\u4e86\u6570\u5341\u4e07\u6761field-value\u7684\u54c8\u5e0c\u952e\u2014\u2014\u5219del\u64cd\u4f5c\u7684\u6267\u884c\u5f80\u5f80\u4f1a\u5bfc\u81f4\u4e3b\u7ebf\u7a0b\u5361\u987f\uff0c\u800c\u60f0\u6027\u5220\u9664\u673a\u5236\u901a\u8fc7\u5f02\u6b65\u6267\u884c\u8017\u65f6\u8fc7\u957f\u7684\u5220\u9664\u547d\u4ee4\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff09\uff0c\u8fd9\u4e5f\u662f\u4e00\u4e2a\u57fa\u4e8e\u591a\u7ebf\u7a0b\u7684\u529f\u80fd\u3002<br \/>\n\u4e00\u822c\u6765\u8bf4\uff0c\u8ba8\u8bbaredis\u7684\u7ebf\u7a0b\u6a21\u578b\u90fd\u662f\u5173\u6ce8\u5176<strong>\u4e3b\u7ebf\u7a0b<\/strong>\u63d0\u4f9b\u7684\u670d\u52a1\uff0c<strong>\u5305\u62ec\u7f51\u7edcIO\u4e8b\u4ef6\u5904\u7406\u548c\u547d\u4ee4\u7684\u6267\u884c<\/strong>\u3002\u53ea\u8003\u8651\u4e3b\u7ebf\u7a0b\u65f6\uff0c\u57286.0\u7248\u672c\u4e4b\u524dredis\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u5355\u7ebf\u7a0b\u6a21\u578b\uff0c\u5176\u7f51\u7edcIO\u548c\u547d\u4ee4\u6267\u884c\u90fd\u7531\u540c\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u5b8c\u6210\uff1b6.0\u7248\u672c\u540e\uff0credis\u5f15\u5165\u4e86\u5168\u65b0\u7684\u591a\u7ebf\u7a0bIO\u673a\u5236\uff0c\u652f\u6301\u591a\u4e2aIO\u7684\u5e76\u884c\u5316\uff08\u4f46\u662f\u9ed8\u8ba4\u5173\u95ed\uff09\uff0c\u800c\u547d\u4ee4\u6267\u884c\u7b49\u5176\u4ed6\u4e3b\u7ebf\u7a0b\u529f\u80fd\u5219\u4f9d\u65e7\u4fdd\u6301\u5355\u4e00\u7ebf\u7a0b\u3002<\/p>\n<h2>6.2 Redis\u4e3a\u4ec0\u4e48\u91c7\u7528\u5355\u7ebf\u7a0b\u6a21\u578b<\/h2>\n<ul>\n<li>\u591a\u7ebf\u7a0b\u6709\u7ebf\u7a0b\u5207\u6362\u6210\u672c\uff1b<\/li>\n<li>\u591a\u7ebf\u7a0b\u6a21\u578b\u5fc5\u7136\u6d89\u53ca\u5230\u5e76\u53d1\u8bfb\u5199\uff0c\u5fc5\u987b\u5f15\u5165\u9501\u7b49\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u652f\u6301\u7ebf\u7a0b\u5b89\u5168\uff0c\u8fd9\u90fd\u589e\u52a0\u4e86\u7cfb\u7edf\u590d\u6742\u5ea6\u4e14\u5bfc\u81f4\u6027\u80fd\u635f\u8017\uff1b<\/li>\n<li>\u5355\u7ebf\u7a0b\u5b9e\u73b0\u7b80\u5355\uff0c\u53ef\u7ef4\u62a4\u6027\u9ad8<\/li>\n<li>\u6570\u636e\u5e93\u7684\u5927\u591a\u6570\u8bf7\u6c42\u90fd\u662fIO\u5bc6\u96c6\u578b\u800c\u975eCPU\u5bc6\u96c6\u578b\u3002\u5177\u4f53\u5230redis\u4e0a\u6765\u8bf4\uff0c\u4f5c\u4e3a\u5185\u5b58\u6570\u636e\u5e93\uff0credis\u6267\u884c\u6570\u636e\u64cd\u4f5c\u7684\u901f\u5ea6\u76f8\u5f53\u5feb\uff0c\u6027\u80fd\u74f6\u9888\u5e76\u4e0d\u5728CPU\u5904\u7406\u901f\u5ea6\u4e0a\uff0c\u800c\u662f\u5728\u7f51\u7edcIO\u4e0a\uff0c\u6545\u6ca1\u6709\u5145\u5206\u7684\u5fc5\u8981\u6027\u5c06\u6574\u4e2a\u7cfb\u7edf\u505a\u6210\u591a\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<h2>6.3 Redis 6.0\u5f15\u5165\u591a\u7ebf\u7a0bIO\u7684\u539f\u56e0<\/h2>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u53d1\u5c55\uff0cQPS\u8981\u6c42\u8d8a\u6765\u8d8a\u9ad8\uff0cRedis\u7684\u5355\u7ebf\u7a0b\u7f51\u7edcIO\u74f6\u9888\u8d8a\u6765\u8d8a\u660e\u663e\u3002\u5e38\u89c1\u89e3\u51b3\u65b9\u6848\u662f\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u533a\u5e76\u91c7\u7528\u5206\u5e03\u5f0f\u67b6\u6784\u7684\u591a\u4e2aredis\u670d\u52a1\u5668\uff0c\u6216\u8005\u6570\u636e\u5206\u533a+\u5355\u673a\u8fd0\u884c\u591a\u4e2aredis\u5b9e\u4f8b\u3002\u4f46\u662f\u8fd9\u6837\u7684\u65b9\u6848\u663e\u7136\u5b58\u5728\u7f3a\u9677\uff0c\u6bd4\u5982\u5206\u533a\u540e\u70ed\u70b9\u6570\u636e\u7684\u8bfb\u5199\u4f9d\u7136\u662f\u95ee\u9898\uff0c\u6bd4\u5982\u6570\u636e\u503e\u659c\uff08\u67d0\u4e9b\u8282\u70b9\/\u5b9e\u4f8b\u83b7\u5f97\u7684\u6570\u636e\u91cf\u8fdc\u5927\u4e8e\u5176\u4ed6\uff0c\u5bfc\u81f4\u9700\u8981\u91cd\u65b0\u5206\u914d\uff09\u3002<\/p>\n<h2>6.4 Redis 6.0\u5f00\u542f\u591a\u7ebf\u7a0bIO\u540e\u7684\u6267\u884c\u903b\u8f91<\/h2>\n<p>6.0\u5f15\u5165\u7684\u5e76\u884cIO\u673a\u5236\u82e5\u5f00\u542f\uff08\u4e14\u8bbe\u7f6e\u7684\u7ebf\u7a0b\u6570\u5927\u4e8e1\uff09\uff0c\u5219\u4f1a\u6309\u7167\u5982\u4e0b\u903b\u8f91\u8fd0\u884c\uff1a<br \/>\n\u4e3b\u7ebf\u7a0b\u8d1f\u8d23\u63a5\u6536\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5\u7684\u8bf7\u6c42\uff0c\u4e14\u5728\u6bcf\u8f6e\u5faa\u73af\u6700\u5f00\u59cb\u65f6\uff0c\u901a\u8fc7\u591a\u8def\u590d\u7528\u83b7\u53d6\u53ef\u8bfb\u7684socket\uff0c\u653e\u5165\u5168\u5c40\u7b49\u5f85\u961f\u5217\uff1b<br \/>\n\u961f\u5217\u6ee1\u540e\uff0c\u4e3b\u7ebf\u7a0b\u5c06\u7b49\u5f85\u961f\u5217\u4e2d\u7684socket\u901a\u8fc7Round Robin\uff08RR\uff0c\u5373\u8f6e\u8be2\u7684naive\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5\uff09\u7b97\u6cd5\u5206\u914d\u7ed9\u4e00\u7ec4IO\u7ebf\u7a0b\uff0cIO\u7ebf\u7a0b\u4e0e\u88ab\u5206\u914d\u7684socket\u7ed1\u5b9a\uff1b<br \/>\nIO\u7ebf\u7a0b\u5e76\u884c\u5904\u7406socket\u7684\u8bfb\u53d6\u3001\u547d\u4ee4\u89e3\u6790\u4efb\u52a1\uff0c\u6b64\u671f\u95f4\u4e3b\u7ebf\u7a0b\u963b\u585e\u7b49\u5f85\uff1b<br \/>\n\u4e3b\u7ebf\u7a0b\u6309\u7167\u961f\u5217\u987a\u5e8f\uff0c\u5355\u7ebf\u7a0b\u987a\u5e8f\u6267\u884c\u89e3\u6790\u51fa\u6765\u7684\u547d\u4ee4\uff1b<br \/>\nIO\u7ebf\u7a0b\u5e76\u884c\u5904\u7406socket\u7684\u5199\u56de\u4efb\u52a1\uff0c\u6b64\u671f\u95f4\u4e3b\u7ebf\u7a0b\u963b\u585e\u7b49\u5f85\uff1b<br \/>\n\u4e3b\u7ebf\u7a0b\u6e05\u7a7a\u5168\u5c40\u7b49\u5f85\u961f\u5217\uff0c\u89e3\u9664IO\u7ebf\u7a0b\u548csocket\u7684\u7ed1\u5b9a\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u8bf7\u6c42\uff0c\u56de\u5230\u5faa\u73af\u6700\u5f00\u59cb\uff1b<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/p3-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/f109a45fe4904726b11fd36422f4abac~tplv-k3u1fbpfcp-zoom-in-crop-mark:3024:0:0:0.awebp'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/p3-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/f109a45fe4904726b11fd36422f4abac~tplv-k3u1fbpfcp-zoom-in-crop-mark:3024:0:0:0.awebp\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u6267\u884c\u903b\u8f91\" \/><\/div><\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/juejin.cn\/post\/6989109527886954527\">https:\/\/juejin.cn\/post\/6989109527886954527<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/weixin_43767015\/article\/details\/120450983\">https:\/\/blog.csdn.net\/weixin_43767015\/article\/details\/120450983<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/m0_55389447\/article\/details\/122963912\">https:\/\/blog.csdn.net\/m0_55389447\/article\/details\/122963912<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/adminpd\/article\/details\/122934938\">https:\/\/blog.csdn.net\/adminpd\/article\/details\/122934938<\/a><\/p>\n<h1>7. Redis\u7684\u6570\u636e\u8fc7\u671f\u7b56\u7565<\/h1>\n<h2>7.1 \u952e\u7a7a\u95f4\u4e0e\u8fc7\u671f\u5b57\u5178<\/h2>\n<p>redis\u6570\u636e\u5e93\u7ed3\u6784\u4f53<code>redisDb<\/code>\u5305\u542b\u4e24\u4e2a\u5b57\u5178\u7c7b\u578b\u6210\u5458\uff1a<\/p>\n<pre><code class=\"language-c\">typedef struct redisDb {\n    \/\/ ...\n    dict *dict;         \/\/ \u952e\u7a7a\u95f4\n    dict *expires;      \/\/ \u8fc7\u671f\u5b57\u5178\n    \/\/ ...\n} redisDb;<\/code><\/pre>\n<p>\u5b57\u5178<code>dict<\/code>\u88ab\u79f0\u4e3a\u952e\u7a7a\u95f4(keyspace)\uff0c\u5b58\u653e\u7740redis\u6570\u636e\u5e93\u4e2d\u6240\u6709\u7684\u952e\u5bf9\u8c61\u548c\u503c\u5bf9\u8c61\uff0c\u952e\u7a7a\u95f4\u7684\u952e\u5373redis\u6570\u636e\u5e93\u7684\u952e\uff0c\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\uff1b\u952e\u7a7a\u95f4\u7684\u503c\u5373\u6570\u636e\u5e93\u7684\u503c\uff0c\u662f\u4e94\u79cd\u57fa\u7840\u6570\u636e\u7c7b\u578b\uff08\u5bf9\u8c61\uff09\u4e2d\u7684\u4efb\u4e00\u79cd\u3002<br \/>\n\u5b57\u5178<code>expires<\/code>\u88ab\u79f0\u4e3a\u8fc7\u671f\u5b57\u5178\uff0c\u5b58\u653e\u7740\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u548c\u5b83\u4eec\u7684\u8fc7\u671f\u4fe1\u606f\uff0c\u8fc7\u671f\u5b57\u5178\u7684\u952e\u662f\u952e\u7a7a\u95f4\u4e2d\u7684\u952e\uff0c\u503c\u662f\u4e00\u4e2along long\u7c7b\u578b\u7684\u6574\u6570\uff0c\u4ee5\u6beb\u79d2\u7cbe\u5ea6\u8bb0\u5f55\u8be5\u952e\u7684\u8fc7\u671f\u65f6\u95f4\u6233\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b58\u653e\u5728\u8fc7\u671f\u5b57\u5178\u4e2d\u7684\u952e\u7684<code>redisObject<\/code>\u4e2d\u6307\u5411\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u7684\u6307\u9488<code>ptr<\/code>\u4f1a\u4e0e\u952e\u7a7a\u95f4\u4e2d\u7684\u5bf9\u5e94\u952e\u5171\u7528\uff08\u4e5f\u5c31\u662f\u5171\u7528\u5e95\u5c42\u6570\u636e\u7ed3\u6784SDS\uff09\uff0c\u7528\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7EXPIRE\/PEXPIRE\u547d\u4ee4\u8bbe\u7f6e\u952e\u7684\u751f\u5b58\u65f6\u95f4\uff0c\u6216\u662f\u901a\u8fc7EXPIREAT\/PEXPIREAT\u547d\u4ee4\u8bbe\u7f6e\u952e\u7684\u8fc7\u671f\u65f6\u95f4\u6233\uff0c\u6b64\u65f6\u5bf9\u5e94\u7684\u8fc7\u671f\u4fe1\u606f\u952e\u503c\u5bf9\u4f1a\u88ab\u8bb0\u5f55\u5230\u8fc7\u671f\u5b57\u5178<code>expires<\/code>\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7PERSIST\u547d\u4ee4\u79fb\u9664\u952e\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u6b64\u65f6\u5bf9\u5e94\u7684\u8fc7\u671f\u4fe1\u606f\u952e\u503c\u5bf9\u4f1a\u88ab\u4ece<code>expires<\/code>\u4e2d\u79fb\u9664\u3002<\/p>\n<h2>7.2 \u8fc7\u671f\u952e\u7684\u5220\u9664\u7b56\u7565<\/h2>\n<p>redis\u4e3b\u8981\u91c7\u7528\u5b9a\u671f\u5220\u9664\u548c\u60f0\u6027\u5220\u9664\u4e24\u79cd\u65b9\u5f0f\u3002<br \/>\n\u5b9a\u671f\u5220\u9664\uff1a\u6bcf\u9694\u4e00\u5b9a\u65f6\u95f4(\u9ed8\u8ba4100ms)\uff0c\u968f\u673a\u62bd\u6837\u8fc7\u671f\u5b57\u5178\u4e2d\u768420\u4e2a\u952e\uff0c\u5c06\u5176\u4e2d\u8fbe\u5230\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u8fdb\u884c\u5220\u9664\uff1b\u82e5\u8fc7\u671f\u7684\u8d85\u8fc7\u4e8625%\uff0c\u5219\u518d\u62bd\u683720\u4e2a\uff0c\u5faa\u73af\u64cd\u4f5c\u3002<br \/>\n\u60f0\u6027\u5220\u9664\uff1a\u8bbf\u95ee\u6570\u636e\u65f6\u68c0\u67e5\u8fc7\u671f\u65f6\u95f4\uff0c\u82e5\u5df2\u8fc7\u671f\uff0c\u5220\u9664\u6570\u636e\u5e76\u8fd4\u56denull\u3002<\/p>\n<h2>7.3 \u5185\u5b58\u6dd8\u6c70\u673a\u5236<\/h2>\n<p>\u4e0a\u8ff0\u4e24\u4e2a\u8fc7\u671f\u952e\u5220\u9664\u673a\u5236\u4e5f\u4e0d\u4e00\u5b9a\u80fd\u4fdd\u8bc1\u8fc7\u671f\u6570\u636e\u7684\u5185\u5b58\u5168\u90fd\u91ca\u653e\uff0c\u5f53\u51fa\u73b0\u5185\u5b58\u4e0d\u8db3\u65f6\u9700\u8981\u7528\u5230\u5185\u5b58\u6dd8\u6c70\u673a\u5236\uff0c\u4e0d\u8fc7\u9ed8\u8ba4\u8bbe\u7f6e\u4e0b\u5185\u5b58\u6dd8\u6c70\u662f\u5173\u95ed\u7684\uff0c\u4e0d\u4f1a\u6dd8\u6c70\u4efb\u4f55\u6570\u636e\uff0c\u53ea\u4f1a\u5728\u5269\u4f59\u5185\u5b58\u65e0\u6cd5\u6267\u884c\u65b0\u7684\u5199\u5165\u64cd\u4f5c\u65f6\u62a5\u9519\u3002<br \/>\nredis\u652f\u6301\u7684\u5185\u5b58\u6dd8\u6c70\u8303\u56f4\u5206\u4e3avolatile\uff08\u53ea\u6dd8\u6c70\u8fc7\u671f\u5b57\u5178\u4e2d\u7684\u952e\uff09\u548callkeys\uff08\u53ef\u4ee5\u6dd8\u6c70\u6240\u6709\u952e\uff09\u3002\u5728volatile\u7b56\u7565\u4e0b\uff0c\u6dd8\u6c70\u5bf9\u8c61\u7684\u9009\u62e9\u7b97\u6cd5\u5206\u4e3aLRU\u3001LFU\uff08least frequently used\uff0c\u4f7f\u7528\u9891\u7387\u6700\u4f4e\uff09\u3001\u968f\u673a\u548c\u6311\u9009\u5373\u5c06\u8fc7\u671f\u7684\u952e\uff1b\u5728allkeys\u7b56\u7565\u4e0b\uff0c\u6dd8\u6c70\u5bf9\u8c61\u7684\u9009\u62e9\u7b97\u6cd5\u5206\u4e3aLRU\u3001LFU\u548c\u968f\u673a\u3002<\/p>\n<h2>TODO: 7.4 AOF\u3001RDB\u548c\u590d\u5236\u529f\u80fd\u5bf9\u8fc7\u671f\u952e\u7684\u5904\u7406<\/h2>\n<h1>8. \u7f13\u5b58\u4e09\u5927\u95ee\u9898<\/h1>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u95ee\u9898<\/th>\n<th style=\"text-align: center;\">\u60c5\u5f62<\/th>\n<th style=\"text-align: center;\">\u89e3\u51b3\u65b9\u6848<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">\u7f13\u5b58\u7a7f\u900f<\/td>\n<td style=\"text-align: center;\">\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684key\uff0c\u5bfc\u81f4\u5927\u91cf\u8bf7\u6c42\u7a7f\u900f\u5230\u6570\u636e\u5e93<\/td>\n<td style=\"text-align: center;\">\u7f13\u5b58\u7a7a\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u8f83\u77ed\u7684\u8fc7\u671f\u65f6\u95f4\uff1b\u5e03\u9686\u8fc7\u6ee4\u5668\uff08\u5c06\u6570\u636e\u54c8\u5e0c\u5230bitmap\u4e2d\u7684k\u4e2a\u70b9\uff0c\u5982\u679c\u4e0d\u90fd\u662f1\u5219\u8ba4\u4e3a\u4e0d\u5b58\u5728\u3002\u6709\u4f1a\u8bef\u5224\u7684\u7f3a\u70b9\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u7f13\u5b58\u51fb\u7a7f<\/td>\n<td style=\"text-align: center;\">\u70ed\u70b9\u6570\u636e\u7684\u7f13\u5b58\u5230\u671f\u5931\u6548<\/td>\n<td style=\"text-align: center;\">\u70ed\u70b9\u6570\u636e\u7f13\u5b58\u4e0d\u8fc7\u671f\uff1b\u5206\u5e03\u5f0f\u4e92\u65a5\u9501\uff0c\u7f13\u5b58\u5931\u6548\u65f6\u53ea\u6709\u62ff\u5230\u9501\u624d\u53ef\u4ee5\u8bbf\u95ee\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u7f13\u5b58\u96ea\u5d29<\/td>\n<td style=\"text-align: center;\">\u5927\u91cf\u7f13\u5b58\u96c6\u4e2d\u5931\u6548<\/td>\n<td style=\"text-align: center;\">\u8fc7\u671f\u65f6\u95f4\u968f\u673a\u6d6e\u52a8\uff1b\u591a\u7ea7\u7f13\u5b58\uff1b\u7194\u65ad\u3001\u9650\u6d41\u4fdd\u62a4\u6570\u636e\u5e93\uff0c\u6d41\u91cf\u8fc7\u5927\u65f6\u8fd4\u56de\u201c\u7cfb\u7edf\u62e5\u6324\u201d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>9. Redis\u6301\u4e45\u5316<\/h1>\n<p>redis\u4f5c\u4e3a\u5185\u5b58\u6570\u636e\u5e93\uff0c\u9700\u8981\u6709\u6301\u4e45\u5316\u673a\u5236\u5c06\u6570\u636e\u5199\u5165\u786c\u76d8\uff0c\u7528\u6765\u9632\u6b62\u5f02\u5e38\u6389\u7535\u6216\u8005\u91cd\u542f\u540e\u7684\u6570\u636e\u4e22\u5931\u3002Redis\u6709RDB\u6301\u4e45\u5316\u548cAOF\u6301\u4e45\u5316\u4e24\u79cd\u65b9\u6848\uff0c\u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u5171\u6027\u662f\uff1a\u90fd\u5b58\u5728fork\u51fa\u5b50\u8fdb\u7a0b\u7684\u65b9\u6848\uff0c\u5b50\u8fdb\u7a0b\u5c06\u6570\u636e\u5199\u5165\u5230\u4e34\u65f6\u6587\u4ef6\uff0c\u5b8c\u6210\u5199\u5165\u540e\uff0c\u7528\u4e34\u65f6\u6587\u4ef6\u66ff\u6362\u4e0a\u4e00\u6b21\u7684\u5feb\u7167\u6587\u4ef6\u3002<\/p>\n<h2>9.1 RDB\u6301\u4e45\u5316<\/h2>\n<h3>9.1.1 \u5173\u4e8eRDB\u6301\u4e45\u5316<\/h3>\n<p>Redis DataBase\u6301\u4e45\u5316\uff0c\u53c8\u79f0\u5feb\u7167\u6301\u4e45\u5316\u3002RDB\u6301\u4e45\u5316\u662f\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u90e8\u5206\u5199\u5165\u78c1\u76d8\u4e0a\u7684\u4e8c\u8fdb\u5236\u5feb\u7167\u6587\u4ef6\uff0c\u8fd9\u4e00\u6027\u8d28\u4f7f\u5f97\u5feb\u7167\u6587\u4ef6\u9664\u4e86\u7528\u4e8e\u6570\u636e\u6062\u590d\u4ee5\u5916\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6709\u76f8\u540c\u6570\u636e\u7684Redis\u526f\u672c\u3002RDB\u6587\u4ef6\u652f\u6301\u538b\u7f29\uff0c\u4ee5LZF\u7b97\u6cd5\u538b\u7f29\u6240\u6709RAW\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u5728\u589e\u5927\u6301\u4e45\u5316\u4e0e\u6570\u636e\u6062\u590d\u6240\u9700\u65f6\u95f4\u7684\u524d\u63d0\u4e0b\u51cf\u5c11\u6587\u4ef6\u5927\u5c0f\u3002<br \/>\n\u7531\u4e8eRDB\u6301\u4e45\u5316\u4e00\u6b21\u6027\u4fdd\u5b58\u6240\u6709\u6570\u636e\u7684\u6027\u8d28\uff0c\u5176\u5feb\u7167\u6587\u4ef6\u66f4\u65b0\u9891\u7387\u901a\u5e38\u4f4e\u4e8eAOF\u6301\u4e45\u5316\uff0c\u6545\u5f53Redis\u542f\u52a8\u670d\u52a1\u8fdb\u884c\u6570\u636e\u5e93\u72b6\u6001\u8fd8\u539f\u65f6\uff0c\u53ea\u8981AOF\u6301\u4e45\u5316\u529f\u80fd\u672a\u5173\u95ed\uff0c\u5219\u4f18\u5148\u4f7f\u7528AOF\u6301\u4e45\u5316\u6587\u4ef6\u3002<\/p>\n<h3>9.1.2 RDB\u6301\u4e45\u5316\u7684\u89e6\u53d1<\/h3>\n<p>RDB\u6301\u4e45\u5316\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u6216\u8005\u81ea\u52a8\u89e6\u53d1\u3002<br \/>\n\u624b\u52a8\u89e6\u53d1\u5305\u62ec\u8f93\u5165save\uff08\u5728\u4e3b\u7ebf\u7a0b\u7684\u5355\u7ebf\u7a0b\u6267\u884c\u547d\u4ee4\u9636\u6bb5\u8fdb\u884cRDB\u6301\u4e45\u5316\uff0c\u4f1a\u5bfc\u81f4\u963b\u585e\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u4e00\u822c\u4e0d\u63a8\u8350\uff09\u3001bgsave\uff08fork\u51fa\u5b50\u8fdb\u7a0b\uff0c\u5e76\u884c\u6267\u884c\u6301\u4e45\u5316\uff09\uff1b<br \/>\n\u81ea\u52a8\u89e6\u53d1\u9700\u8981\u8f93\u5165\u5e26\u53c2\u7684save\u547d\u4ee4\uff0csave m n\u8868\u793a\u5728m\u79d2\u5185\u6570\u636e\u5e93\u5b58\u5728n\u6b21\u4fee\u6539\u65f6\uff0c\u81ea\u52a8\u89e6\u53d1bgsave\u3002\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0credis\u5185\u90e8\u5305\u542b\u4e00\u7ec4save m n\u7684\u914d\u7f6e\u3002<\/p>\n<h3>9.1.3 RDB\u6301\u4e45\u5316\u7684\u4f18\u7f3a\u70b9<\/h3>\n<p>\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>RDB\u5feb\u7167\u6587\u4ef6\u662f\u4e8c\u8fdb\u5236\u65b9\u5f0f\u50a8\u5b58\uff0c\u5927\u5c0f\u7d27\u51d1\u3001\u6062\u590d\u901f\u5ea6\u5feb\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>BGSAVE\u5bf9\u65f6\u95f4\u3001\u5185\u5b58\u7684\u6d88\u8017\u90fd\u8f83\u5927\uff0c\u65e0\u6cd5\u505a\u5230\u5b9e\u65f6\u6301\u4e45\u5316\uff1b<\/li>\n<li>\u82e5\u6267\u884cRDB\u6301\u4e45\u5316\u65f6\u5d29\u6e83\uff0c\u4f1a\u4e22\u5931\u4e0a\u4e00\u6b21\u6210\u529fRDB\u4ee5\u540e\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<h2>9.2 AOF\u6301\u4e45\u5316<\/h2>\n<h3>9.2.1 \u5173\u4e8eAOF\u6301\u4e45\u5316<\/h3>\n<p>AOF(append only file)\uff0c\u662f\u76ee\u524d\u7684\u4e3b\u6d41\u6301\u4e45\u5316\u65b9\u5f0f\uff0c\u5728AOF\u6587\u4ef6\u4e2d\u4fdd\u5b58redis\u5199\u547d\u4ee4\u6765\u8bb0\u5f55\u72b6\u6001\uff0c\u9700\u8981\u6062\u590d\u6570\u636e\u65f6\u5219\u91cd\u65b0\u987a\u5e8f\u6267\u884cAOF\u6587\u4ef6\u4e2d\u8bb0\u5f55\u7684\u547d\u4ee4\u3002\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff0c\u4fee\u6539redis.conf\u6587\u4ef6\u5f00\u542f\u3002\u76f8\u6bd4\u8d77RDB\uff0cAOF\u6587\u4ef6\u662f\u6587\u672c\u683c\u5f0f\u800c\u975e\u4e8c\u8fdb\u5236\u683c\u5f0f\uff1b\u4e14\u7531\u4e8e\u5927\u5355\u6b21\u5199\u5165\u6210\u672c\u4f4e\uff0cAOF\u6301\u4e45\u5316\u7684\u5b9e\u65f6\u6027\u66f4\u597d\u3002<\/p>\n<h3>9.2.1 AOF\u6301\u4e45\u5316\u7684\u5de5\u4f5c\u6d41\u7a0b<\/h3>\n<p>\u5728\u6267\u884c\u5b8c\u4e00\u4e2a\u5199\u547d\u4ee4\u540e\uff0credis\u4f1a\u5c06\u8be5\u547d\u4ee4\u8ffd\u52a0(append)\u5199\u5165AOF\u7f13\u51b2\u533a\u4e2d\uff1b\u6bcf\u6b21redis\u4e3b\u8fdb\u7a0b\u7684\u4e8b\u4ef6\u5faa\u73af\u7ed3\u675f\u524d\uff0c\u4f9d\u636e\u8bbe\u7f6e\u7684\u6301\u4e45\u5316\u7b56\u7565\u5224\u65ad\u662f\u5426\u5c06AOF\u7f13\u51b2\u533a\u4e2d\u7684\u5185\u5bb9\u5199\u5165(write)\u548c\u540c\u6b65(fsync)\u5230\u78c1\u76d8\u4e0a\u7684AOF\u6587\u4ef6\u91cc\u3002\u6301\u4e45\u5316\u7b56\u7565\u6709\u4e09\u4e2a\u7c7b\u578b\uff0c\u5bf9\u5e94\u670d\u52a1\u5668\u914d\u7f6e<code>appendfsync<\/code>\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u503c<\/th>\n<th style=\"text-align: center;\">\u884c\u4e3a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><code>always<\/code><\/td>\n<td style=\"text-align: center;\">\u603b\u662f\u5c06\u7f13\u51b2\u533a\u4e2d\u7684\u5185\u5bb9\u5199\u5165AOF\u6587\u4ef6\u5e76<code>fsync<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><code>everysec<\/code><\/td>\n<td style=\"text-align: center;\">\u603b\u662f\u5c06\u7f13\u51b2\u533a\u4e2d\u7684\u5185\u5bb9\u5199\u5165AOF\u6587\u4ef6\uff0c\u5982\u679c\u8ddd\u79bb\u4e0a\u6b21<code>fsync<\/code>\u65f6\u95f4\u8d85\u8fc7\u4e00\u79d2\u5219\u6267\u884c<code>fsync<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><code>no<\/code><\/td>\n<td style=\"text-align: center;\">\u603b\u662f\u5c06\u7f13\u51b2\u533a\u4e2d\u7684\u5185\u5bb9\u5199\u5165AOF\u6587\u4ef6\uff0c\u4e0d\u4e3b\u52a8\u8c03\u7528<code>fsync<\/code>\uff0c\u5b8c\u5168\u4ea4\u7531\u64cd\u4f5c\u7cfb\u7edf\u81ea\u884c\u51b3\u5b9a\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e09\u79cd\u65b9\u5f0f\u4e0eMySQL redo log file\u843d\u76d8\u7684\u8bbe\u7f6e\u975e\u5e38\u7c7b\u4f3c\u3002<\/p>\n<h3>9.2.2 AOF\u6587\u4ef6\u7684\u8f7d\u5165<\/h3>\n<p>\u7531\u4e8eredis\u7684\u547d\u4ee4\u53ea\u80fd\u5728\u5ba2\u6237\u7aef\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\uff0c\u6240\u4ee5\u5728\u4f7f\u7528AOF\u6301\u4e45\u5316\u8fdb\u884c\u6570\u636e\u6062\u590d\u65f6\uff0credis\u4f1a\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7f51\u7edc\u8fde\u63a5\u7684\u4f2a\u5ba2\u6237\u7aef(fake client)\uff0c\u9010\u6761\u5206\u6790\u51faAOF\u6587\u4ef6\u7684\u5199\u547d\u4ee4\u5e76\u4ea4\u7531\u8be5\u4f2a\u5ba2\u6237\u7aef\u6267\u884c\u3002<\/p>\n<h3>9.2.3 AOF\u91cd\u5199<\/h3>\n<p>AOF\u6587\u4ef6\u7684\u5927\u5c0f\u4f1a\u968f\u7740\u8fd0\u884c\u6301\u7eed\u53d8\u5927\uff0c\u4e0d\u4ec5\u5360\u7528\u7a7a\u95f4\uff0c\u8fd8\u4f1a\u4f7f\u5f97AOF\u6301\u4e45\u5316\u7684\u6570\u636e\u6062\u590d\u65f6\u95f4\u65e0\u610f\u4e49\u5730\u589e\u5927\u3002redis\u56e0\u6b64\u8bbe\u8ba1\u4e86AOF\u91cd\u5199\u673a\u5236\uff0c\u5f53AOF\u6587\u4ef6\u7684\u5927\u5c0f\u8d85\u8fc7\u6240\u8bbe\u5b9a\u7684\u91cd\u5199\u9608\u503c\u65f6\u5bf9AOF\u6587\u4ef6\u8fdb\u884c\u91cd\u5199\u3002\u5b9e\u9645\u4e0aAOF\u91cd\u5199\u7684\u8fc7\u7a0b\u5e76\u4e0d\u662f\u5206\u6790\u548c\u5316\u7b80\u73b0\u6709AOF\u6587\u4ef6\uff0c\u800c\u662f\u76f4\u63a5\u8bfb\u53d6\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u5bf9\u6bcf\u6761\u6570\u636e\u8bb0\u5f55\u4e00\u6761set\u547d\u4ee4\uff08\u5176\u4ed6\u5bf9\u8c61\u4e5f\u662f\u7c7b\u4f3c\uff0c\u4f8b\u5982\u5bf9\u5217\u8868\u5bf9\u8c61\u5219\u76f4\u63a5\u4f7f\u7528RPUSH\uff09\u3002<br \/>\n\u8fdb\u884c\u91cd\u5199\u65f6\uff0c\u7236\u8fdb\u7a0b\u4f1a<code>fork<\/code>\u4e00\u4e2a\u5e26\u6709\u7236\u8fdb\u7a0b\u7684\u6570\u636e\u526f\u672c\u7684\u5b50\u8fdb\u7a0b\uff0c\u7531\u5b50\u8fdb\u7a0b\u5b8c\u6210\u91cd\u5199\u8fc7\u7a0b\uff0c\u5728\u6b64\u671f\u95f4\u7236\u8fdb\u7a0b\u4ecd\u7136\u53ef\u4ee5\u5904\u7406\u5176\u4ed6\u547d\u4ee4\u3002\u5728\u5b50\u8fdb\u7a0b\u8fdb\u884c\u91cd\u5199\u671f\u95f4\uff0c\u7236\u8fdb\u7a0b\u9664\u4e86\u5c06\u65b0\u7684AOF\u65e5\u5fd7\u5199\u5165AOF\u7f13\u51b2\u533a\uff0c\u8fd8\u8981\u5199\u5165\u4e00\u4e2a\u72ec\u7acb\u7684AOF\u91cd\u5199\u7f13\u51b2\u533a\u3002\u5f53\u5b50\u8fdb\u7a0b\u5b8c\u6210\u91cd\u5199\u540e\u4f1a\u4ee5\u4fe1\u53f7\u65b9\u5f0f\u901a\u77e5\u7236\u8fdb\u7a0b\uff0c\u7236\u8fdb\u7a0b\u5728\u5904\u7406\u8be5\u4fe1\u53f7\u7684\u51fd\u6570\uff08\u4f1a\u5bfc\u81f4\u7236\u8fdb\u7a0b\u963b\u585e\uff09\u4e2d\u4f1a\u5c06AOF\u91cd\u5199\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5199\u5165\u5230\u65b0\u7684AOF\u6587\u4ef6\u4e2d\uff0c\u6700\u540e\u5c06\u65b0AOF\u6587\u4ef6\u91cd\u547d\u540d\uff0c\u5b8c\u6210\u5bf9\u65e7AOF\u6587\u4ef6\u7684\u8986\u76d6\u3002<\/p>\n<h1>10. \u7f13\u5b58\u548c\u6570\u636e\u5e93\u4e00\u81f4\u6027\u95ee\u9898<\/h1>\n<h2>10.1 \u7ecf\u5178\u7684cache-aside\u65b9\u6848<\/h2>\n<ul>\n<li>\u8bfb\uff1a\u8bfb\u6570\u636e\u65f6\uff0c\u5148\u8bfb\u7f13\u5b58\uff0c\u82e5\u7f13\u5b58\u672a\u547d\u4e2d\u5219\u8bfb\u6570\u636e\u5e93\uff0c\u6210\u529f\u540e\u5c06\u83b7\u5f97\u7684\u6570\u636e\u653e\u5165\u7f13\u5b58<\/li>\n<li>\u5199\uff1a\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c<strong>\u6210\u529f\u540e<\/strong>\u518d\u5220\u9664\u7f13\u5b58\u3002<\/li>\n<\/ul>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic3.zhimg.com\/80\/v2-d775e26a40e369ac362a0b6d1c56e68a_720w.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic3.zhimg.com\/80\/v2-d775e26a40e369ac362a0b6d1c56e68a_720w.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"cache-aside\" \/><\/div><\/p>\n<h2>10.2 \u4e3a\u4ec0\u4e48cache-aside\u65b9\u6848\u8981\u5728\u5199\u7684\u65f6\u5019\u5220\u9664\u7f13\u5b58\uff1f<\/h2>\n<p>\u5b9e\u9645\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u9009\u62e9\u5728\u5199\u6570\u636e\u65f6\u5148\u66f4\u65b0\u7f13\u5b58\u800c\u540e\u518d\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u6216\u662f\u5148\u66f4\u65b0\u6570\u636e\u5e93\u800c\u540e\u518d\u66f4\u65b0\u7f13\u5b58\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u6848\u5728\u5e76\u53d1\u573a\u666f\u4e0b\u5b58\u5728\u663e\u8457\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u91c7\u7528\u5148\u66f4\u65b0\u6570\u636e\u540e\u66f4\u65b0\u7f13\u5b58\u7684\u65b9\u5f0f\uff0c\u6b64\u65f6\u6709\u4e24\u4e2a\u7ebf\u7a0b\u4fee\u6539\u540c\u4e00\u6570\u636e\uff0c\u7531\u4e8e\u7f51\u7edc\u7b49\u539f\u56e0\uff0c\u5b9e\u9645\u7ebf\u7a0b\u6267\u884c\u987a\u5e8f\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u6b65\u9aa4<\/th>\n<th style=\"text-align: center;\">\u6267\u884c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">\u7ebf\u7a0b1\u5199\u5e93<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\">\u7ebf\u7a0b2\u5199\u5e93<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">3<\/td>\n<td style=\"text-align: center;\">\u7ebf\u7a0b2\u5199\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">4<\/td>\n<td style=\"text-align: center;\">\u7ebf\u7a0b1\u5199\u7f13\u5b58<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u6837\uff0c\u7f13\u5b58\u4e0e\u6570\u636e\u5e93\u5c31\u4e0d\u4e00\u81f4\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ea\u80fd\u5f15\u5165\u52a0\u9501\u673a\u5236\uff0c\u8fd9\u4f1a\u4f7f\u5f97\u7cfb\u7edf\u53d8\u5f97\u590d\u6742\uff0c\u4e14\u8fd0\u884c\u6548\u7387\u964d\u4f4e\u3002\u56e0\u6b64\uff0c\u5728\u5199\u6570\u636e\u65f6\u76f4\u63a5\u5220\u9664\u7f13\u5b58\u662f\u7a33\u59a5\u7684\u65b9\u6848\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5220\u9664\u7f13\u5b58\u5e26\u6765\u7684\u526f\u4f5c\u7528\u4ec5\u9650\u4e8e\u4e00\u6b21cache miss\uff0c\u66f4\u65b0\u6210\u672c\u4e0d\u7b97\u9ad8\u3002<\/p>\n<h2>10.3 cache-aside\u65b9\u6848\u7684\u95ee\u9898<\/h2>\n<p>\u5728\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u5220\u9664\u7f13\u5b58\u7684\u7b56\u7565\u4f9d\u7136\u4f1a\u5b58\u5728\u7f13\u5b58\u4e00\u81f4\u6027\u7684\u95ee\u9898\uff0c\u8fd9\u91cc\u5bf9\u8fdb\u884c\u5206\u7c7b\u8ba8\u8bba\u3002<br \/>\n\u6807\u51c6\u7684cache-aside\u7b56\u7565\u5728\u66f4\u65b0\u6570\u636e\u65f6\u662f\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u6210\u529f\u540e\u518d\u5220\u9664\u7f13\u5b58\uff1b\u800c\u5018\u82e5\u4e0d\u91c7\u7528\u6807\u51c6\u65b9\u6848\uff0c\u800c\u662f\u5148\u5220\u9664\u7f13\u5b58\u518d\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u5f53\u540c\u65f6\u6709\u8bfb\u7ebf\u7a0b\u548c\u5199\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u6570\u636e\u65f6\uff0c\u5c31\u4f1a\u5b58\u5728\u8fd9\u6837\u7684\u95ee\u9898\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u6b65\u9aa4<\/th>\n<th style=\"text-align: center;\">\u6267\u884c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">\u5199\u7ebf\u7a0b\u5220\u9664\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\">\u8bfb\u7ebf\u7a0b\u8bbf\u95ee\u7f13\u5b58\uff0ccache miss<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">3<\/td>\n<td style=\"text-align: center;\">\u8bfb\u7ebf\u7a0b\u8bbf\u95ee\u6570\u636e\u5e93\u83b7\u5f97\u65e7\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">4<\/td>\n<td style=\"text-align: center;\">\u8bfb\u7ebf\u7a0b\u5c06\u65e7\u503c\u5199\u5165\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">5<\/td>\n<td style=\"text-align: center;\">\u5199\u7ebf\u7a0b\u5c06\u65b0\u503c\u5199\u5165\u6570\u636e\u5e93<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5373\u4f7f\u662f\u91c7\u7528\u6807\u51c6\u7684cache-aside\u7b56\u7565\uff0c\u4e5f\u4f1a\u5b58\u5728\u53e6\u4e00\u79cd\u95ee\u9898\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u6b65\u9aa4<\/th>\n<th style=\"text-align: center;\">\u6267\u884c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">\u7f13\u5b58\u6070\u597d\u5931\u6548<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\">\u8bfb\u7ebf\u7a0b\u8bbf\u95ee\u7f13\u5b58\uff0ccache miss<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">3<\/td>\n<td style=\"text-align: center;\">\u8bfb\u7ebf\u7a0b\u8bbf\u95ee\u6570\u636e\u5e93\u83b7\u5f97\u65e7\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">4<\/td>\n<td style=\"text-align: center;\">\u5199\u7ebf\u7a0b\u5c06\u65b0\u503c\u5199\u5165\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">5<\/td>\n<td style=\"text-align: center;\">\u5199\u7ebf\u7a0b\u5220\u9664\u7f13\u5b58\uff08\u5b9e\u9645\u4e0a\u7f13\u5b58\u91cc\u4e5f\u6ca1\u6709\u8fd9\u4e2a\u503c\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">6<\/td>\n<td style=\"text-align: center;\">\u8bfb\u7ebf\u7a0b\u5c06\u65e7\u503c\u5199\u5165\u7f13\u5b58<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u76f8\u5bf9\u4e8e\u5148\u5220\u7f13\u5b58\u518d\u66f4\u65b0\u6570\u636e\u5e93\u65b9\u6848\u5b58\u5728\u7684\u810f\u6570\u636e\u95ee\u9898\uff0c\u6807\u51c6cache-aside\u7b56\u7565\u5b58\u5728\u7684\u95ee\u9898\u53d1\u751f\u6982\u7387\u8f83\u4f4e\uff0c\u53ea\u4f1a\u5728\u7f13\u5b58\u8fc7\u671f\u65f6\u6070\u597d\u6709\u7ebf\u7a0b\u8bfb\u5199\u6570\u636e\u65f6\u53d1\u751f\u3002\u6b64\u5916\uff0c\u5982\u679c\u5220\u9664\u7f13\u5b58\u5931\u8d25\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u7f13\u5b58\u4e0d\u4e00\u81f4\u3002<\/p>\n<h2>10.4 \u5ef6\u8fdf\u53cc\u5220\u7b56\u7565<\/h2>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0c\u5ef6\u8fdf\u53cc\u5220\u7b56\u7565\u88ab\u63d0\u51fa\u4e86\u3002\u4ee5\u6807\u51c6cache-aside\u7b56\u7565\u4e3a\u4f8b\uff0c\u5f15\u5165\u5ef6\u8fdf\u53cc\u5220\u673a\u5236\u540e\uff0c\u5199\u6570\u636e\u7684\u6d41\u7a0b\u53d8\u4e3a\uff1a<br \/>\n\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u6210\u529f\u540e\u518d\u5220\u9664\u7f13\u5b58\u3002\u7ecf\u8fc71\u79d2\u4f11\u7720\u540e\uff0c\u518d\u6b21\u5220\u9664\u7f13\u5b58\u3002\uff08\u5177\u4f53\u7684\u4f11\u7720\u65f6\u95f4\u5e94\u5f53\u8bc4\u4f30\u4e1a\u52a1\u5b9e\u9645\u8017\u65f6\u8fdb\u884c\u8c03\u6574\uff09<br \/>\n\u4ece\u6027\u80fd\u89d2\u5ea6\u548c\u907f\u514d\u7f13\u5b58\u5220\u9664\u5931\u8d25\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u7b2c\u4e8c\u6b21\u5220\u9664\u5e94\u5f53\u8bbe\u8ba1\u6210\u5f02\u6b65\u5220\u9664\uff0c\u5e76\u5f15\u5165<strong>\u6d88\u606f\u961f\u5217<\/strong>\uff0c\u5f53\u6355\u83b7\u5230\u5220\u9664\u5931\u8d25\u7684\u5f02\u5e38\u65f6\uff0c\u518d\u6b21\u5411\u6d88\u606f\u961f\u5217\u4e2d\u52a0\u5165\u4e00\u4e2a\u5220\u9664\u4efb\u52a1\u3002<br \/>\n\u8fd9\u6837\u4e00\u6765\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u8bfb\u8bf7\u6c42\u7ed3\u675f\u540e\uff0c\u5199\u8bf7\u6c42\u53ef\u4ee5\u5220\u9664\u8bfb\u8bf7\u6c42\u9020\u6210\u7684\u7f13\u5b58\u810f\u6570\u636e\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/147028497\">https:\/\/zhuanlan.zhihu.com\/p\/147028497<\/a><br \/>\n<a href=\"https:\/\/note.dolyw.com\/cache\/00-DataBaseConsistency.html\">https:\/\/note.dolyw.com\/cache\/00-DataBaseConsistency.html<\/a><\/p>\n<h1>11. redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501<\/h1>\n<p>\u7528<code>SETNX<\/code>(SET if Not eXsist)\u7cfb\u5217\u547d\u4ee4\uff0c\u83b7\u5f97\u9501\u3002<br \/>\n<a href=\"https:\/\/segmentfault.com\/a\/1190000038988087\">https:\/\/segmentfault.com\/a\/1190000038988087<\/a><\/p>\n<h1>12. redis\u9ad8\u53ef\u7528\u5b9e\u73b0<\/h1>\n<p>redis\u5b9e\u73b0\u9ad8\u53ef\u7528\u4e3b\u8981\u6709\u4e09\u79cd\u65b9\u5f0f\uff1a\u4e3b\u4ece\u590d\u5236\u3001\u54e8\u5175\u6a21\u5f0f\u3001redis\u96c6\u7fa4<\/p>\n<h2>12.1 Redis\u4e3b\u4ece\u590d\u5236<\/h2>\n<p>\u4e0eMySQL\u81ea\u5e26\u7684\u4e3b\u4ece\u590d\u5236\u529f\u80fd\u7c7b\u4f3c\uff0credis\u63d0\u4f9b\u4e86\u590d\u5236(replicate)\u529f\u80fd\uff0c\u652f\u6301\u4e00\u4e3b\u591a\u4ece\u7684\u590d\u5236\u6a21\u5f0f\u3002\u901a\u8fc7<code>SLAVEOF<\/code>\u547d\u4ee4\u53ef\u4ee5\u5f00\u542f\u4e3b\u4ece\u590d\u5236\u529f\u80fd\u3002<\/p>\n<h2>12.2 \u54e8\u5175\u6a21\u5f0f<\/h2>\n<p>\u54e8\u5175(sentinel)\u6a21\u5f0f\u662fredis\u7684\u9ad8\u53ef\u7528\u6027\u89e3\u51b3\u65b9\u6848\uff0c\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u54e8\u5175\u5b9e\u4f8b\u7ec4\u6210\u7684\u54e8\u5175\u7cfb\u7edf\u53ef\u4ee5\u76d1\u89c6\u4efb\u610f\u591a\u4e2a\u4e3b\u670d\u52a1\u5668\u548c\u4ed6\u4eec\u4e0b\u5c5e\u7684\u4ece\u670d\u52a1\u5668\uff0c\u5f53\u76d1\u89c6\u7684\u4e3b\u670d\u52a1\u5668\u4e0b\u7ebf\u5e76\u8d85\u8fc7\u8bbe\u5b9a\u7684\u65f6\u9650\u540e\uff0c\u54e8\u5175\u7cfb\u7edf\u4f1a\u6267\u884c\u6545\u969c\u8f6c\u79fb\u64cd\u4f5c\uff0c\u9009\u62e9\u5176\u4e0b\u5c5e\u7684\u67d0\u4e2a\u4ece\u670d\u52a1\u5668\u4f5c\u4e3a\u65b0\u7684\u4e3b\u670d\u52a1\u5668\u6765\u5904\u7406\u547d\u4ee4\u8bf7\u6c42\uff0c\u5e76\u5411\u5176\u4f59\u4ece\u670d\u52a1\u5668\u53d1\u9001\u65b0\u7684\u590d\u5236\u6307\u4ee4\uff0c\u7ec4\u6210\u65b0\u7684\u4e3b\u4ece\u7ed3\u6784\u3002<br \/>\n\u54e8\u5175\u672c\u8d28\u4e0a\u53ea\u662f\u8fd0\u884c\u5728\u7279\u6b8a\u6a21\u5f0f\u4e0b\u7684redis\u670d\u52a1\u5668\uff0c\u5176\u4e3b\u4e8b\u4ef6\u5faa\u73af\u7684\u8fd0\u884c\u903b\u8f91\u4e0e\u6b63\u5e38\u6a21\u5f0f\u4e0b\u7684redis\u4e0d\u540c\u3002\u54e8\u5175\u4f1a\u4e0e\u4e3b\u4ece\u670d\u52a1\u5668\u90fd\u5efa\u7acb\u547d\u4ee4\u8fde\u63a5\u548c\u8ba2\u9605\u8fde\u63a5\uff0c\u901a\u8fc7\u547d\u4ee4\u8fde\u63a5\u53d1\u9001\u4fe1\u606f\uff0c\u901a\u8fc7\u8ba2\u9605\u8fde\u63a5\u63a5\u53d7\u4fe1\u606f\u3002\u54e8\u5175\u7cfb\u7edf\u5185\u7684\u5404\u4e2a\u54e8\u5175\u5b9e\u4f8b\u4e4b\u95f4\u4e5f\u4f1a\u5efa\u7acb\u547d\u4ee4\u8fde\u63a5\uff0c\u4f46\u662f\u4e0d\u521b\u5efa\u8ba2\u9605\u8fde\u63a5\u3002<br \/>\n\u54e8\u5175\u5b9e\u4f8b\u6bcf\u79d2\u4f1a\u5411\u6240\u6709\u521b\u5efa\u4e86\u547d\u4ee4\u94fe\u63a5\u7684\u5b9e\u4f8b\u53d1\u9001PING\u547d\u4ee4\uff0c\u6839\u636e\u56de\u590d\u5224\u65ad\u5b9e\u4f8b\u662f\u5426\u5728\u7ebf\uff1b\u5f53\u53d1\u73b0\u67d0\u4e2a\u4e3b\u670d\u52a1\u5668\u56de\u590d\u8d85\u65f6\u540e\uff0c\u54e8\u5175\u8ba4\u4e3a\u8be5\u5b9e\u4f8b\u201c\u4e3b\u89c2\u4e0b\u7ebf\u201d\uff0c\u6b64\u65f6\u9700\u8981\u5411\u76d1\u89c6\u8be5\u4e3b\u670d\u52a1\u5668\u7684\u5176\u4ed6\u54e8\u5175\u5b9e\u4f8b\u8be2\u95ee\uff0c\u786e\u8ba4\u5176\u4e0b\u7ebf\u72b6\u6001\uff0c\u6536\u5230\u8db3\u591f\u591a\u7684\u4e0b\u7ebf\u5224\u65ad\u540e\u624d\u8ba4\u5b9a\u8be5\u4e3b\u670d\u52a1\u5668\u4e3a\u201c\u5ba2\u89c2\u4e0b\u7ebf\u201d\u72b6\u6001\u3002<br \/>\n\u786e\u8ba4\u4e3b\u670d\u52a1\u5668\u5ba2\u89c2\u4e0b\u7ebf\u540e\uff0c\u76d1\u89c6\u8be5\u4e3b\u670d\u52a1\u5668\u7684\u5404\u54e8\u5175\u5b9e\u4f8b\u4f1a\u9009\u4e3e\u51fa\u4e00\u4e2a\u9996\u9886\u54e8\u5175\u6765\u6267\u884c\u6545\u969c\u8f6c\u79fb\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u9009\u4e3e\u6b65\u9aa4\u5b9e\u73b0\u4e86raft\u7b97\u6cd5\u4e2d\u7684\u9886\u5934\u9009\u4e3e\u7b97\u6cd5\u3002<\/p>\n<h2>12.3 redis\u96c6\u7fa4<\/h2>\n<p>\u54e8\u5175\u6a21\u5f0f\u5f52\u6839\u7ed3\u5e95\u4ecd\u7136\u662f\u4e3b\u4ece\u590d\u5236\u6a21\u5f0f\u7684\u9ad8\u53ef\u7528\u6027\u6539\u826f\u3002\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u4e3b\u4ece\u590d\u5236\u6a21\u5f0f\u76f8\u6bd4\u8d77\u5355\u673a\u6570\u636e\u5e93\u5728\u8bfb\u6027\u80fd\u65b9\u9762\u5f97\u5230\u4e86\u663e\u8457\u63d0\u9ad8\uff0c\u4f46\u662f\u7cfb\u7edf\u7684\u5199\u6027\u80fd\u4f9d\u7136\u53d7\u5230\u4e3b\u670d\u52a1\u5668\u6027\u80fd\u4e0a\u9650\u7684\u9650\u5236\uff0c\u5b58\u50a8\u80fd\u529b\u4e5f\u540c\u7406\u3002\u4e3a\u4e86\u6269\u5c55\u591a\u673a\u7cfb\u7edf\u7684\u5199\u6027\u80fd\u548c\u5b58\u50a8\u6027\u80fd\uff0c\u9700\u8981\u5f15\u5165\u771f\u6b63\u7684\u5206\u5e03\u5f0f\u65b9\u6848\u3002<br \/>\nRedis\u96c6\u7fa4\u901a\u8fc7\u5206\u7247(sharding)\u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5171\u4eab\uff0c\u5c06\u96c6\u7fa4\u7684\u6574\u4e2a\u6570\u636e\u5e93\u5206\u4e3a16384\u4e2a\u69fd(slot)\uff0c\u6570\u636e\u5e93\u7684\u6bcf\u4e2a\u952e\u90fd\u5c5e\u4e8e\u67d0\u4e00\u4e2a\u69fd\uff0c\u96c6\u7fa4\u4e2d\u7684\u6bcf\u4e2aredis\u8282\u70b9\u88ab\u6307\u6d3e(assign)\u5904\u7406\u67d0\u4e2a\u6216\u67d0\u4e9b\u69fd\u3002\u5f53\u6240\u6709\u7684\u69fd\u90fd\u6709\u8d1f\u8d23\u5904\u7406\u7684\u8282\u70b9\u65f6\uff0c\u6b64redis\u96c6\u7fa4\u8fdb\u5165\u4e0a\u7ebf\u72b6\u6001(<code>CLUSTER INFO<\/code>\u547d\u4ee4\u8fd4\u56de\u7684\u7ed3\u679c\u4e2d\uff0ccluster_state\u9879\u4e3aok)\u3002\u901a\u8fc7\u952e\u503c\u7684CRC-16\u6821\u9a8c\u548c\uff0c\u518d\u4e0e16384\u8fdb\u884c\u6309\u4f4d\u4e0e\u8fd0\u7b97\u53ef\u4ee5\u83b7\u5f97\u952e\u503c\u5c5e\u4e8e\u7684\u69fd\u7684\u7f16\u53f7\u3002<br \/>\nRedis\u96c6\u7fa4\u4e5f\u63d0\u4f9b\u590d\u5236\u548c\u6545\u969c\u8f6c\u79fb\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u67d0\u4e9b\u8282\u70b9\u8bbe\u7f6e\u4e3a\u67d0\u4e00\u8282\u70b9\u7684\u4ece\u8282\u70b9\uff0c\u4e3b\u8282\u70b9\u7528\u4e8e\u5904\u7406\u69fd\uff0c\u4ece\u8282\u70b9\u590d\u5236\u4e3b\u8282\u70b9\u7684\u6570\u636e\u5e93\uff0c\u5e76\u5728\u4e3b\u8282\u70b9\u4e0b\u7ebf\u65f6\u4ee3\u66ff\u5904\u7406\u547d\u4ee4\u8bf7\u6c42\u3002<\/p>\n<h1>13.<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>1. NOSQL\u7684\u7279\u70b9 1.1 \u5206\u7c7b \u952e\u503c\u5bf9\/\u534a\u7ed3\u6784\u5316\u6587\u6863(json)\/\u56fe\u6570\u636e\u5e93 1.2 \u4f18\u70b9 \u5185\u5b58\u7ea7\u6570\u636e\u5e93\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[23],"class_list":["post-207","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-10","tag-redis"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}