{"id":201673,"date":"2025-05-10T09:20:47","date_gmt":"2025-05-10T01:20:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201673\/"},"modified":"2025-05-10T09:20:47","modified_gmt":"2025-05-10T01:20:47","slug":"%e5%9b%be%e8%a7%a3redis%ef%bc%8credis%e6%9b%b4%e6%96%b0%e7%ad%96%e7%95%a5%e3%80%81%e7%bc%93%e5%ad%98%e4%b8%80%e8%87%b4%e6%80%a7%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201673\/","title":{"rendered":"\u56fe\u89e3Redis\uff0cRedis\u66f4\u65b0\u7b56\u7565\u3001\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u56fe\u89e3Redis\uff0cRedis\u66f4\u65b0\u7b56\u7565\u3001\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;font-family:\">\u300a\u56fe\u89e3Redis\uff0cRedis\u66f4\u65b0\u7b56\u7565\u3001\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;font-family:\">Redis\u3001\u7f13\u5b58\u4e00\u81f4\u6027<\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;font-family:\">\u6570\u636e\u5e93<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;font-family:\">\u6570\u636e\u5e93<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230502\/1683035328645114c0bf254.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<h2><span style=\"color: #515151\">\u4e00\u3001\u4e3a\u4ec0\u4e48Redis\u96c6\u7fa4\u7684\u6700\u5927\u69fd\u6570\u662f16384\u4e2a\uff1f<\/span><\/h2>\n<p>2^14^=16384\u30012^16^=65536\u3002<\/p>\n<p>\u5982\u679c\u69fd\u4f4d\u662f65536\u4e2a\uff0c\u53d1\u9001\u5fc3\u8df3\u4fe1\u606f\u7684\u6d88\u606f\u5934\u662f65536\/8\/1024 = 8k\u3002<\/p>\n<p>\u5982\u679c\u69fd\u4f4d\u662f16384\u4e2a\uff0c\u53d1\u9001\u5fc3\u8df3\u4fe1\u606f\u7684\u6d88\u606f\u5934\u662f16384\/8\/1024 = 2k\u3002<\/p>\n<p>\u56e0\u4e3aRedis\u6bcf\u79d2\u90fd\u4f1a\u53d1\u9001\u4e00\u5b9a\u6570\u91cf\u7684\u5fc3\u8df3\u5305\uff0c\u5982\u679c\u6d88\u606f\u5934\u662f8k\uff0c\u672a\u514d\u6709\u4e9b\u592a\u5927\u4e86\uff0c\u6d6a\u8d39\u7f51\u7edc\u8d44\u6e90\u3002<\/p>\n<p>\u4e0a\u9762\u63d0\u8fc7\uff0cRedis\u7684\u96c6\u7fa4\u4e3b\u8282\u70b9\u6570\u91cf\u4e00\u822c\u4e0d\u4f1a\u8d85\u8fc71000\u4e2a\u3002\u96c6\u7fa4\u4e2d\u8282\u70b9\u8d8a\u591a\uff0c\u5fc3\u8df3\u5305\u7684\u6d88\u606f\u4f53\u5185\u7684\u6570\u636e\u5c31\u8d8a\u591a\uff0c\u5982\u679c\u8282\u70b9\u8fc7\u591a\uff0c\u4e5f\u4f1a\u9020\u6210\u7f51\u7edc\u62e5\u5835\u3002\u56e0\u6b64Redis\u7684\u4f5c\u8005Salvatore Sanfilippo\u4e0d\u5efa\u8baeRedis Cluster\u7684\u8282\u70b9\u8d85\u8fc71000\u4e2a\uff0c\u5bf9\u4e8e\u8282\u70b9\u6570\u57281000\u4e2a\u4ee5\u5185\u7684Redis Cluster\uff0c16384\u4e2a\u69fd\u4f4d\u5b8c\u5168\u591f\u7528\u3002<\/p>\n<p>Redis\u4e3b\u8282\u70b9\u7684\u54c8\u5e0c\u69fd\u4fe1\u606f\u662f\u901a\u8fc7bitmap\u5b58\u50a8\u7684\uff0c\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u5bf9bitmap\u8fdb\u884c\u538b\u7f29\uff0cbitmap\u7684\u586b\u5145\u7387\u8d8a\u4f4e\uff0c\u538b\u7f29\u7387\u8d8a\u9ad8\u3002<\/p>\n<p>bitmap \u586b\u5145\u7387 = slots \/ N (N\u8868\u793a\u8282\u70b9\u6570)\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4slots\u8d8a\u5c0f\uff0c\u586b\u5145\u7387\u5c31\u4f1a\u8d8a\u5c0f\uff0c\u538b\u7f29\u7387\u5c31\u4f1a\u8d8a\u9ad8\uff0c\u4f20\u8f93\u6548\u7387\u5c31\u4f1a\u8d8a\u9ad8\u3002\u200b<\/p>\n<h2><span style=\"color: #515151\">\u4e8c\u3001Redis\u96c6\u7fa4\u662f\u4ec0\u4e48\uff1f<\/span><\/h2>\n<p>\u7531\u4e8e\u6570\u636e\u91cf\u8fc7\u5927\uff0c\u5355\u4e2amaster\u590d\u5236\u96c6\u96be\u4ee5\u627f\u62c5\uff0c\u56e0\u6b64\u9700\u8981\u591a\u4e2amaster\u8fdb\u884c\u627f\u62c5\u5de5\u4f5c\uff0c\u6bcf\u4e2amaster\u5b58\u50a8\u90e8\u5206\u6570\u636e\uff0c\u8fd9\u5c31\u662fRedis\u96c6\u7fa4\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230502\/1683035329645114c12d40e.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p>Redis\u96c6\u7fa4\u5305\u542b\u591a\u4e2amaster\uff0c\u4e00\u4e2amaster\u5bf9\u5e94\u591a\u4e2aslave\uff0c\u7531\u4e8e\u96c6\u7fa4\u81ea\u5e26\u6545\u969c\u8f6c\u79fb\u673a\u5236\uff0c\u56e0\u6b64Redis\u96c6\u7fa4\u4e0d\u7528\u518d\u4f7f\u7528\u54e8\u5175sentinel\u529f\u80fd\u3002<\/p>\n<p>Redis Cluster\u662fRedis3.0\u5f15\u5165\u7684\u4e00\u79cd\u65e0\u4e2d\u5fc3\u5316\u7684\u96c6\u7fa4\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u5411\u4efb\u4f55\u4e00\u4e2a\u8282\u70b9\u901a\u4fe1\uff0c\u4e0d\u540c\u8282\u70b9\u95f4\u7684\u6570\u636e\u4e0d\u4e92\u901a\uff0cRedis Cluster\u5c06\u6570\u636e\u7684key\u901a\u8fc7\u5c06CRC16\u7b97\u6cd5\u7684\u7ed3\u679c\u53d6\u6a2116383\u540e\uff0c\u5206\u7ed916384\u4e2aslot\u69fd\uff0c\u96c6\u7fa4\u7684\u6bcf\u4e2a\u8282\u70b9\u8d1f\u8d23\u4e00\u90e8\u5206hash\u69fd\uff0c\u8282\u70b9\u53ea\u8d1f\u8d23\u7ba1\u7406\u6620\u5c04\u5230\u8fd9\u4e2a\u69fd\u7684KV\u6570\u636e\uff0c\u5bf9\u4e8e\u4e0d\u662f\u5f53\u524d\u69fd\u7684KV\u6570\u636e\uff0c\u4f1a\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2aMOVED\uff0c\u8868\u793a\u9700\u8981\u5ba2\u6237\u7aef\u91cd\u65b0\u91cd\u5b9a\u5411\u5230\u5176\u5b83\u8282\u70b9\u3002<\/p>\n<p>\u4f7f\u7528Redis\u96c6\u7fa4\u65f6\uff0c\u6211\u4eec\u5c06\u9700\u8981\u5b58\u50a8\u7684\u6570\u636e\u5206\u914d\u5230\u591a\u53f0Redis\u670d\u52a1\u5668\u4e0a\uff0c\u79f0\u4e3a\u5206\u7247\u3002<\/p>\n<h3><span style=\"color: #515151\">\u6570\u636e\u5982\u4f55\u5206\u7247\uff1f<\/span><\/h3>\n<p>\u5bf9key\u8fdb\u884cCRC16(key)\u7b97\u6cd5\u5904\u7406\u5e76\u901a\u8fc7\u5bf9\u603b\u5206\u7247\u6570\u91cf\u53d6\u6a21\uff0c\u7136\u540e\u4f7f\u7528\u786e\u5b9a\u6027\u54c8\u5e0c\u51fd\u6570\uff0c\u5c06\u6307\u5b9a\u7684key\u591a\u6b21\u6620\u5c04\u5230\u540c\u4e00\u4e2a\u5206\u7247\u4e0a\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u5728\u8fdb\u884c\u670d\u52a1\u5668\u6269\u5bb9\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u5f71\u54cd\u96c6\u7fa4\u7684\u4f7f\u7528\u72b6\u6001\u3002<\/p>\n<p>Redis\u96c6\u7fa4\u4e0d\u4fdd\u8bc1\u5f3a\u4e00\u81f4\u6027\uff0c\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\uff0cRedis\u96c6\u7fa4\u53ef\u80fd\u4f1a\u4e22\u6389\u4e00\u4e9b\u547d\u4ee4\u3002<\/p>\n<h2><span style=\"color: #515151\">\u4e09\u3001slot\u69fd\u4f4d\u6620\u5c04\u7684\u65b9\u5f0f<\/span><\/h2>\n<h3><span style=\"color: #515151\">1\u3001\u54c8\u5e0c\u53d6\u4f59\u5206\u533a<\/span><\/h3>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230502\/1683035329645114c16b53a.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p>\u54c8\u5e0c\u53d6\u4f59\u5206\u533a\u7684\u4f18\u70b9\u662f\u5206\u914d\u5747\u5300\uff0c\u4f7f\u7528hash(key)\/3\u7684\u5f62\u5f0f\u8ba9\u56fa\u5b9a\u7684\u4e00\u90e8\u5206\u8bf7\u6c42\u5b58\u5165\u6307\u5b9a\u7684master\uff0c\u6bcf\u53f0master\u5904\u7406\u4e00\u90e8\u5206\u6570\u636e\uff0c\u8d77\u5230\u4e86\u8d1f\u8f7d\u5747\u8861\u7684\u6548\u679c\u3002<\/p>\n<p>\u54c8\u5e0c\u53d6\u4f59\u5206\u533a\u6700\u5927\u7684\u7f3a\u70b9\u5c31\u662f\u4e0d\u65b9\u4fbf\u6269\u5bb9\uff0c\u5f53\u9700\u8981\u6269\u5bb9\u65f6\uff0c\u6620\u5c04\u5173\u7cfb\u9700\u8981\u8fdb\u884c\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<h3><span style=\"color: #515151\">2\u3001\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5<\/span><\/h3>\n<h4>\uff081\uff09\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u662f\u4ec0\u4e48\uff1f<\/h4>\n<p>\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u57281997\u5e74\u7531\u9ebb\u7701\u7406\u5de5\u5b66\u9662\u63d0\u51fa\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u76ee\u7684\u662f\u89e3\u51b3\u5206\u5e03\u5f0f\u7f13\u5b58\u7684\u95ee\u9898\u3002\u5728\u79fb\u9664\u6216\u8005\u6dfb\u52a0\u4e00\u4e2a\u670d\u52a1\u5668\u65f6\uff0c\u80fd\u591f\u5c3d\u53ef\u80fd\u5c0f\u5730\u6539\u53d8\u5df2\u5b58\u5728\u7684\u670d\u52a1\u8bf7\u6c42\u4e0e\u5904\u7406\u8bf7\u6c42\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002\u4e00\u81f4\u6027\u54c8\u5e0c\u89e3\u51b3\u4e86\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u5728\u5206\u5e03\u5f0f\u54c8\u5e0c\u8868( Distributed Hash Table\uff0cDHT) \u4e2d\u5b58\u5728\u7684\u52a8\u6001\u4f38\u7f29\u7b49\u95ee\u9898\u3002<\/p>\n<p>\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u5c06\u6574\u4e2a\u54c8\u5e0c\u503c\u7a7a\u95f4\u6620\u5c04\u6210\u4e00\u4e2a\u865a\u62df\u7684\u5706\u73af\uff0c\u6574\u4e2a\u54c8\u5e0c\u7a7a\u95f4\u7684\u53d6\u503c\u8303\u56f4\u4e3a0~2^32^-1\uff0c\u6574\u4e2a\u7a7a\u95f4\u6309\u987a\u65f6\u9488\u65b9\u5411\u7ec4\u7ec7\uff0c0~2^32^-1\u5728\u96f6\u70b9\u4e2d\u65b9\u5411\u91cd\u5408\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u4f7f\u7528\u5982\u4e0b\u7b97\u6cd5\u5bf9\u670d\u52a1\u8bf7\u6c42\u8fdb\u884c\u6620\u5c04\uff0c\u5c06\u670d\u52a1\u8bf7\u6c42\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u7b97\u51fa\u5bf9\u5e94\u7684hash\u503c\uff0c\u7136\u540e\u6839\u636ehash\u503c\u7684\u4f4d\u7f6e\u6cbf\u5706\u73af\u987a\u65f6\u9488\u67e5\u627e\uff0c\u7b2c\u4e00\u53f0\u9047\u5230\u7684\u670d\u52a1\u5668\u5c31\u662f\u6240\u5bf9\u5e94\u7684\u5904\u7406\u8bf7\u6c42\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5f53\u589e\u52a0\u4e00\u53f0\u65b0\u7684\u670d\u52a1\u5668\uff0c\u53d7\u5f71\u54cd\u7684\u6570\u636e\u4ec5\u4ec5\u662f\u65b0\u6dfb\u52a0\u7684\u670d\u52a1\u5668\u5230\u5176\u73af\u7a7a\u95f4\u4e2d\u524d\u4e00\u53f0\u7684\u670d\u52a1\u5668\uff08\u4e5f\u5c31\u662f\u987a\u7740\u9006\u65f6\u9488\u65b9\u5411\u9047\u5230\u7684\u7b2c\u4e00\u53f0\u670d\u52a1\u5668\uff09\u4e4b\u95f4\u7684\u6570\u636e\uff0c\u5176\u4ed6\u90fd\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u5bf9\u4e8e\u8282\u70b9\u7684\u589e\u51cf\u90fd\u53ea\u9700\u91cd\u5b9a\u4f4d\u73af\u7a7a\u95f4\u4e2d\u7684\u4e00\u5c0f\u90e8\u5206\u6570\u636e\uff0c\u5177\u6709\u8f83\u597d\u7684\u5bb9\u9519\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230502\/1683035329645114c1bcfd1.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<h4>\uff082\uff09\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u7684\u4f18\u70b9<\/h4>\n<ol>\n<li>\u53ef\u6269\u5c55\u6027\u3002<\/li>\n<li>\u66f4\u597d\u7684\u9002\u5e94\u6570\u636e\u7684\u5feb\u901f\u589e\u957f\uff0c\u5f53\u67d0\u4e2a\u5206\u7247\u5b58\u50a8\u6570\u636e\u8fc7\u591a\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u4e00\u5206\u4e3a\u4e8c\uff0c\u4e0d\u9700\u8981\u5bf9\u5168\u90e8\u7684\u6570\u636e\u8fdb\u884c\u91cd\u65b0hash\u8ba1\u7b97\u548c\u5212\u5206\u3002<\/li>\n<\/ol>\n<h4>\uff083\uff09\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u7684\u7f3a\u70b9<\/h4>\n<p>\u5f53\u670d\u52a1\u8282\u70b9\u592a\u5c11\u65f6\uff0c\u5bb9\u6613\u9020\u6210\u6570\u636e\u503e\u659c\uff0c\u5206\u914d\u4e0d\u5747\u3002<\/p>\n<p>\u5927\u91cf\u7684\u7f13\u5b58\u6570\u636e\u96c6\u4e2d\u5230\u4e86\u4e00\u53f0\u6216\u8005\u51e0\u53f0\u670d\u52a1\u8282\u70b9\u4e0a\uff0c\u79f0\u4e3a\u6570\u636e\u503e\u659c\u3002<\/p>\n<h2><span style=\"color: #515151\">\u56db\u3001Redis\u66f4\u65b0\u7b56\u7565<\/span><\/h2>\n<p>1\u3001\u5982\u679cRedis\u4e2d\u6709\u6570\u636e\uff0c\u9700\u8981\u548c\u6570\u636e\u5e93\u4e2d\u7684\u503c\u76f8\u540c\u3002<\/p>\n<p>2\u3001\u5982\u679cRedis\u4e2d\u65e0\u6570\u636e\uff0c\u6570\u636e\u5e93\u4e2d\u7684\u6700\u65b0\u503c\u8981\u5bf9Redis\u8fdb\u884c\u540c\u6b65\u66f4\u65b0\u3002<\/p>\n<h2><span style=\"color: #515151\">\u4e94\u3001Redis\u8bfb\u5199\u7f13\u5b58<\/span><\/h2>\n<h3><span style=\"color: #515151\">1\u3001\u540c\u6b65\u76f4\u5199\u7b56\u7565<\/span><\/h3>\n<p>\u5199\u5165\u6570\u636e\u5e93\u4e5f\u540c\u6b65\u5199Redis\u7f13\u5b58\uff0c\u7f13\u5b58\u548c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u4e00\u81f4\uff1b\u5bf9\u4e8e\u8bfb\u5199\u7f13\u5b58\u6765\u8bf4\uff0c\u8981\u4fdd\u8bc1\u7f13\u5b58\u548c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u4e00\u81f4\uff0c\u5c31\u8981\u4fdd\u8bc1\u540c\u6b65\u76f4\u5199\u7b56\u7565\u3002<\/p>\n<h3><span style=\"color: #515151\">2\u3001\u5f02\u6b65\u7f13\u5199\u7b56\u7565<\/span><\/h3>\n<p>\u67d0\u4e9b\u4e1a\u52a1\u8fd0\u884c\u4e2d\uff0cMySQL\u6570\u636e\u66f4\u65b0\u4e4b\u540e\uff0c\u5141\u8bb8\u5728\u4e00\u5b9a\u65f6\u95f4\u540e\u518d\u8fdb\u884cRedis\u6570\u636e\u540c\u6b65\uff0c\u6bd4\u5982\u7269\u6d41\u7cfb\u7edf\u3002<\/p>\n<p>\u5f53\u51fa\u73b0\u5f02\u5e38\u60c5\u51b5\u65f6\uff0c\u4e0d\u5f97\u4e0d\u5c06\u5931\u8d25\u7684\u52a8\u4f5c\u91cd\u65b0\u4fee\u8865\uff0c\u9700\u8981\u501f\u52a9rabbitmq\u6216kafka\u8fdb\u884c\u91cd\u5199\u3002<\/p>\n<h2><span style=\"color: #515151\">\u516d\u3001\u53cc\u68c0\u52a0\u9501\u7b56\u7565<\/span><\/h2>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53bb\u67e5\u8be2\u6570\u636e\u5e93\u7684\u8fd9\u6761\u6570\u636e\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u7b2c\u4e00\u4e2a\u67e5\u8be2\u6570\u636e\u7684\u8bf7\u6c42\u4e0a\u4f7f\u7528\u4e00\u4e2a \u4e92\u65a5\u9501\u6765\u9501\u4f4f\u5b83\u3002<\/p>\n<p>\u5176\u4ed6\u7684\u7ebf\u7a0b\u8d70\u5230\u8fd9\u4e00\u6b65\u62ff\u4e0d\u5230\u9501\u5c31\u7b49\u7740\uff0c\u7b49\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u67e5\u8be2\u5230\u4e86\u6570\u636e\uff0c\u7136\u540e\u505a\u7f13\u5b58\u3002<\/p>\n<p>\u540e\u9762\u7684\u7ebf\u7a0b\u8fdb\u6765\u53d1\u73b0\u5df2\u7ecf\u6709\u7f13\u5b58\u4e86\uff0c\u5c31\u76f4\u63a5\u8d70\u7f13\u5b58\u3002<\/p>\n<p><span style=\"color: #d73a49\">public<\/span> <span style=\"color: #22863a\">String<\/span> <span style=\"color: #005cc5\">get<\/span>(<span style=\"color: #22863a\">String<\/span> <span>key<\/span>){<br \/><span style=\"color: #6a737d\">\/\/ \u4eceRedis\u7f13\u5b58\u4e2d\u8bfb\u53d6<\/span><br \/><span style=\"color: #22863a\">String<\/span> <span>value<\/span> <span style=\"color: #d73a49\">=<\/span> <span>redisTemplate<\/span>.<span>get<\/span>(<span>key<\/span>);<\/p>\n<p><span style=\"color: #d73a49\">if<\/span>(<span>value<\/span> <span style=\"color: #d73a49\">!=<\/span> <span style=\"color: #905\">null<\/span>){<br \/><span style=\"color: #d73a49\">return<\/span> <span>value<\/span>;<br \/> }<\/p>\n<p><span style=\"color: #d73a49\">synchronized<\/span> (<span>RedisTest<\/span>.<span style=\"color: #d73a49\">class<\/span>){<br \/><span style=\"color: #6a737d\">\/\/ \u91cd\u65b0\u5c1d\u8bd5\u4eceRedis\u7f13\u5b58\u4e2d\u8bfb\u53d6<\/span><br \/><span>value<\/span> <span style=\"color: #d73a49\">=<\/span> <span>redisTemplate<\/span>.<span>get<\/span>(<span>key<\/span>);<br \/><span style=\"color: #d73a49\">if<\/span>(<span>value<\/span> <span style=\"color: #d73a49\">!=<\/span> <span style=\"color: #905\">null<\/span>){<br \/><span style=\"color: #d73a49\">return<\/span> <span>value<\/span>;<br \/> }<\/p>\n<p><span style=\"color: #6a737d\">\/\/ \u4eceMySQL\u6570\u636e\u5e93\u4e2d\u67e5\u8be2<\/span><br \/><span>value<\/span> <span style=\"color: #d73a49\">=<\/span> <span>studentDao<\/span>.<span>get<\/span>(<span>key<\/span>);<br \/><span style=\"color: #6a737d\">\/\/ \u5199\u5165Redis\u7f13\u5b58<\/span><br \/><span>redisTemplate<\/span>.<span>setnx<\/span>(<span>key<\/span>,<span>value<\/span>,<span>time<\/span>);<br \/><span style=\"color: #d73a49\">return<\/span> <span>value<\/span>;<br \/> }<br \/>}<\/p>\n<h2><span style=\"color: #515151\">\u4e03\u3001\u6570\u636e\u5e93\u548c\u7f13\u5b58\u4e00\u81f4\u6027\u7684\u66f4\u65b0\u7b56\u7565<\/span><\/h2>\n<h3><span style=\"color: #515151\">1\u3001\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u518d\u66f4\u65b0Redis<\/span><\/h3>\n<p>\u6309\u7167\u5e38\u7406\u51fa\u724c\u7684\u8bdd\uff0c\u5e94\u8be5\u90fd\u662f\u5982\u6b64\u5427\uff1f\u90a3\u4e48\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f1a\u6709\u5565\u95ee\u9898\u5462\uff1f<\/p>\n<p>\u5982\u679c\u66f4\u65b0\u6570\u636e\u5e93\u6210\u529f\u540e\uff0c\u66f4\u65b0Redis\u4e4b\u524d\u5f02\u5e38\u4e86\uff0c\u4f1a\u51fa\u73b0\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f<\/p>\n<p>\u6570\u636e\u5e93\u4e0eRedis\u5185\u7f13\u5b58\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/p>\n<h3><span style=\"color: #515151\">2\u3001\u5148\u66f4\u65b0\u7f13\u5b58\uff0c\u518d\u66f4\u65b0\u6570\u636e\u5e93<\/span><\/h3>\n<p>\u591a\u7ebf\u7a0b\u60c5\u51b5\u4e0b\uff0c\u4f1a\u6709\u95ee\u9898\u3002<\/p>\n<p>\u6bd4\u5982<\/p>\n<ol>\n<li>\u7ebf\u7a0b1\u66f4\u65b0redis = 200\uff1b<\/li>\n<li>\u7ebf\u7a0b2\u66f4\u65b0redis = 100\uff1b<\/li>\n<li>\u7ebf\u7a0b2\u66f4\u65b0MySQL = 100\uff1b<\/li>\n<li>\u7ebf\u7a0b1\u66f4\u65b0MySQL = 200\uff1b<\/li>\n<\/ol>\n<p>\u7ed3\u679c\u5462\uff0cRedis=100\u3001MySQL=200\uff1b\u6211\u64e6\uff01<\/p>\n<h3><span style=\"color: #515151\">3\u3001\u5148\u5220\u9664\u7f13\u5b58\uff0c\u518d\u66f4\u65b0\u6570\u636e\u5e93<\/span><\/h3>\n<ol>\n<li>\u7ebf\u7a0b1\u5220\u9664\u4e86Redis\u7684\u7f13\u5b58\u6570\u636e\uff0c\u7136\u540e\u53bb\u66f4\u65b0MySQL\u6570\u636e\u5e93\u3002<\/li>\n<li>\u8fd8\u6ca1\u7b49MySQL\u66f4\u65b0\u5b8c\u6bd5\uff0c\u7ebf\u7a0b2\u6740\u6765\uff0c\u8bfb\u53d6\u7f13\u5b58\u6570\u636e\u3002<\/li>\n<li>\u4f46\u662f\uff0c\u6b64\u65f6MySQL\u6570\u636e\u5e93\u8fd8\u6ca1\u66f4\u65b0\uff0c\u7ebf\u7a0b2\u8bfb\u53d6\u4e86MySQL\u4e2d\u7684\u65e7\u503c\uff0c\u7136\u540e\u7ebf\u7a0b2\uff0c\u8fd8\u4f1a\u5c06\u65e7\u503c\u5199\u5165Redis\u4f5c\u4e3a\u6570\u636e\u7f13\u5b58\u3002<\/li>\n<li>\u7ebf\u7a0b1\u66f4\u65b0\u5b8cMySQL\u6570\u636e\u540e\uff0c\u53d1\u73b0Redis\u4e2d\u5df2\u7ecf\u6709\u6570\u636e\u4e86\uff0c\u4e4b\u524d\u90fd\u5220\u8fc7\u4e86\uff0c\u90a3\u6211\u5c31\u4e0d\u66f4\u65b0\u4e86\u3002<\/li>\n<\/ol>\n<p>\u5b8c\u86cb\u4e86\u3002\u3002<\/p>\n<h4>\u5ef6\u65f6\u53cc\u5220<\/h4>\n<p>\u5ef6\u65f6\u53cc\u5220\u53ef\u4ee5\u89e3\u51b3\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u53ea\u8981sleep\u7684\u65f6\u95f4\u5927\u4e8e\u7ebf\u7a0b2\u8bfb\u53d6\u6570\u636e\u518d\u5199\u5165\u7f13\u5b58\u7684\u65f6\u95f4\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e5f\u5c31\u662f\u7ebf\u7a0b1\u7684\u4e8c\u6b21\u6e05\u7f13\u5b58\u64cd\u4f5c\u8981\u5728\u7ebf\u7a0b2\u5199\u5165\u7f13\u5b58\u4e4b\u540e\uff0c\u8fd9\u6837\u624d\u80fd\u4fdd\u8bc1Redis\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u662f\u6700\u65b0\u7684\u3002<\/p>\n<p><span style=\"color: #6a737d\">\/**<\/span><br \/><span style=\"color: #6a737d\">* \u5ef6\u65f6\u53cc\u5220<\/span><br \/><span style=\"color: #6a737d\">* @autor \u54ea\u5412\u7f16\u7a0b<\/span><br \/><span style=\"color: #6a737d\">*\/<\/span><br \/><span style=\"color: #d73a49\">public<\/span> <span style=\"color: #22863a\">void<\/span> <span style=\"color: #005cc5\">deleteRedisData<\/span>(<span>Student<\/span> <span>stu<\/span>){<br \/><span style=\"color: #6a737d\">\/\/ \u5220\u9664Redis\u4e2d\u7684\u7f13\u5b58\u6570\u636e<\/span><br \/><span>jedis<\/span>.<span>del<\/span>(<span>stu<\/span>);<\/p>\n<p><span style=\"color: #6a737d\">\/\/ \u66f4\u65b0MySQL\u6570\u636e\u5e93\u6570\u636e<\/span><br \/><span>studentDao<\/span>.<span>update<\/span>(<span>stu<\/span>);<\/p>\n<p><span style=\"color: #6a737d\">\/\/ \u4f11\u606f\u4e24\u79d2<\/span><br \/><span style=\"color: #d73a49\">try<\/span> {<br \/><span>TimeUnit<\/span>.<span>SECONDS<\/span>.<span>sleep<\/span>(<span style=\"color: #005cc5\">2<\/span>);<br \/> } <span style=\"color: #d73a49\">catch<\/span> (<span>InterruptedException<\/span> <span>e<\/span>) {<br \/><span>e<\/span>.<span>printStackTrace<\/span>();<br \/> }<\/p>\n<p><span style=\"color: #6a737d\">\/\/ \u5220\u9664Redis\u4e2d\u7684\u7f13\u5b58\u6570\u636e<\/span><br \/><span>jedis<\/span>.<span>del<\/span>(<span>stu<\/span>);<br \/>}<\/p>\n<p>\u5ef6\u8fdf\u53cc\u5220\u6700\u5927\u7684\u95ee\u9898\u5c31\u662fsleep\uff0c\u5728\u6548\u7387\u4e3a\u738b\u7684\u4eca\u5929\uff0csleep\u80fd\u4e0d\u7528\u8fd8\u662f\u4e0d\u7528\u4e3a\u597d\u3002<\/p>\n<p>\u4f60\u4e0d\u7761\u6211\u90fd\u5acc\u4f60\u6162\uff0c\u4f60\u8fd8\u7761\u4e0a\u4e86&#8230;<\/p>\n<h3><span style=\"color: #515151\">4\u3001\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u518d\u5220\u9664\u7f13\u5b58<\/span><\/h3>\n<ol>\n<li>\u7ebf\u7a0b1\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u518d\u5220\u9664Redis\u7f13\u5b58\u3002<\/li>\n<li>\u7ebf\u7a0b2\u5728\u7ebf\u7a0b1\u5220\u9664Redis\u7f13\u5b58\u4e4b\u524d\u53d1\u8d77\u8bf7\u6c42\uff0c\u5f97\u5230\u4e86\u672a\u5220\u9664\u7684Redis\u7f13\u5b58\u3002<\/li>\n<li>\u7ebf\u7a0b1\u6b64\u65f6\u624d\u5220\u9664Redis\u7f13\u5b58\u6570\u636e\u3002<\/li>\n<\/ol>\n<p>\u95ee\u9898\u8fd8\u662f\u6709\uff0c\u8fd9\u7ffb\u6765\u8986\u53bb\u7684\uff0c\u6ca1\u5b8c\u6ca1\u4e86\u4e86\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u5982\u4f55\u89e3\u51b3\u5462\uff1f<\/p>\n<p>\u5f15\u5165\u6d88\u606f\u4e2d\u95f4\u4ef6\u89e3\u51b3\u6218\u6597\uff0c\u518d\u4e00\u6b21\u8be6\u7ec6\u7684\u590d\u76d8\u4e00\u4e0b\u3002<\/p>\n<ol>\n<li>\u66f4\u65b0\u6570\u636e\u5e93\u3002<\/li>\n<li>\u6570\u636e\u5e93\u5c06\u64cd\u4f5c\u4fe1\u606f\u5199\u5165binlog\u65e5\u5fd7\u3002<\/li>\n<li>\u8ba2\u9605\u7a0b\u5e8f\u63d0\u53d6\u51fakey\u548c\u6570\u636e\u3002<\/li>\n<li>\u5c1d\u8bd5\u5220\u9664\u7f13\u5b58\u64cd\u4f5c\uff0c\u53d1\u73b0\u5220\u9664\u5931\u8d25\u3002<\/li>\n<li>\u5c06\u8fd9\u4e9b\u6570\u636e\u4fe1\u606f\u53d1\u9001\u5230\u6d88\u606f\u4e2d\u95f4\u4ef6\u4e2d\u3002<\/li>\n<li>\u4ece\u6d88\u606f\u4e2d\u95f4\u4ef6\u4e2d\u83b7\u53d6\u8be5\u6570\u636e\uff0c\u91cd\u65b0\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<h3><span style=\"color: #515151\">5\u3001\u603b\u7ed3<\/span><\/h3>\n<p>\u54ea\u5412\u63a8\u8350\u4f7f\u7528\u7b2c\u56db\u79cd\u65b9\u5f0f\uff0c\u5148\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u518d\u5220\u9664\u7f13\u5b58\u3002<\/p>\n<p>\u65b9\u5f0f\u2460\u548c\u65b9\u5f0f\u2461\u7f3a\u70b9\u592a\u8fc7\u660e\u663e\uff0c\u4e0d\u8003\u8651\uff1b\u65b9\u5f0f\u2462\u4e2d\u7684sleep\uff0c\u603b\u662f\u8ba9\u4eba\u5934\u75bc\uff1b\u65b9\u5f0f\u2463\u662f\u4e00\u4e2a\u6bd4\u8f83\u5168\u9762\u7684\u65b9\u6848\uff0c\u4f46\u662f\u589e\u52a0\u4e86\u5b66\u4e60\u6210\u672c\u3001\u7ef4\u62a4\u6210\u672c\uff0c\u56e0\u4e3a\u589e\u52a0\u4e86\u6d88\u606f\u4e2d\u95f4\u4ef6\u3002<\/p>\n<h2><span style=\"color: #515151\">\u516b\u3001MySQL\u4e3b\u4ece\u590d\u5236\u5de5\u4f5c\u539f\u7406<\/span><\/h2>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230502\/1683035330645114c215499.png\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p>1\u3001\u5f53 master \u4e3b\u670d\u52a1\u5668\u4e0a\u7684\u6570\u636e\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u5219\u5c06\u5176\u6539\u53d8\u5199\u5165\u4e8c\u8fdb\u5236\u4e8b\u4ef6\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>2\u3001salve \u4ece\u670d\u52a1\u5668\u4f1a\u5728\u4e00\u5b9a\u65f6\u95f4\u95f4\u9694\u5185\u5bf9 master \u4e3b\u670d\u52a1\u5668\u4e0a\u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\u8fdb\u884c\u63a2\u6d4b\uff0c\u63a2\u6d4b\u5176\u662f\u5426\u53d1\u751f\u8fc7\u6539\u53d8\u3002<\/p>\n<p>\u5982\u679c\u63a2\u6d4b\u5230 master \u4e3b\u670d\u52a1\u5668\u7684\u4e8c\u8fdb\u5236\u4e8b\u4ef6\u65e5\u5fd7\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u5219\u5f00\u59cb\u4e00\u4e2a I\/O Thread \u8bf7\u6c42 master \u4e8c\u8fdb\u5236\u4e8b\u4ef6\u65e5\u5fd7\u3002<\/p>\n<p>3\u3001\u540c\u65f6 master \u4e3b\u670d\u52a1\u5668\u4e3a\u6bcf\u4e2a I\/O Thread \u542f\u52a8\u4e00\u4e2adump &nbsp;Thread\uff0c\u7528\u4e8e\u5411\u5176\u53d1\u9001\u4e8c\u8fdb\u5236\u4e8b\u4ef6\u65e5\u5fd7\u3002<\/p>\n<p>4\u3001slave \u4ece\u670d\u52a1\u5668\u5c06\u63a5\u6536\u5230\u7684\u4e8c\u8fdb\u5236\u4e8b\u4ef6\u65e5\u5fd7\u4fdd\u5b58\u81f3\u81ea\u5df1\u672c\u5730\u7684\u4e2d\u7ee7\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>5\u3001salve \u4ece\u670d\u52a1\u5668\u5c06\u542f\u52a8 SQL Thread \u4ece\u4e2d\u7ee7\u65e5\u5fd7\u4e2d\u8bfb\u53d6\u4e8c\u8fdb\u5236\u65e5\u5fd7\uff0c\u5728\u672c\u5730\u91cd\u653e\uff0c\u4f7f\u5f97\u5176\u6570\u636e\u548c\u4e3b\u670d\u52a1\u5668\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p>6\u3001\u6700\u540e I\/O Thread \u548c SQL Thread \u5c06\u8fdb\u5165\u7761\u7720\u72b6\u6001\uff0c\u7b49\u5f85\u4e0b\u4e00\u6b21\u88ab\u5524\u9192\u3002<\/p>\n<p>\u672c\u6587\u8f6c\u8f7d\u81ea\u5fae\u4fe1\u516c\u4f17\u53f7\u300c\u54ea\u5412\u7f16\u7a0b\u300d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e8c\u7ef4\u7801\u5173\u6ce8\u3002\u8f6c\u8f7d\u672c\u6587\u8bf7\u8054\u7cfb\u54ea\u5412\u7f16\u7a0b\u516c\u4f17\u53f7\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230502\/1683035330645114c252bbb.png\" style=\"max-width:100%\" class=\"aligncenter\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56fe\u89e3Redis\uff0cRedis\u66f4\u65b0\u7b56&#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-201673","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201673","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=201673"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201673\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}