{"id":1165,"date":"2021-04-15T14:46:32","date_gmt":"2021-04-15T06:46:32","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=1165"},"modified":"2023-08-07T20:57:23","modified_gmt":"2023-08-07T12:57:23","slug":"mysql%e4%b8%8eredis%e4%b8%ad%e5%af%b9lru%e7%ae%97%e6%b3%95%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2021\/04\/15\/mysql%e4%b8%8eredis%e4%b8%ad%e5%af%b9lru%e7%ae%97%e6%b3%95%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"MySQL\u4e0eRedis\u4e2d\u5bf9LRU\u7b97\u6cd5\u7684\u4f7f\u7528"},"content":{"rendered":"<p>MySQL\u4e0eRedis\u4e2d\u5bf9LRU\u7b97\u6cd5\u7684\u4f7f\u7528<\/p>\n<p>LRU\u4e5f\u79f0\u4e3a\u6700\u8fd1\u6700\u5c11\u672a\u4f7f\u7528\u7b97\u6cd5\uff0c\u4f5c\u4e3a\u6700\u5e38\u7528\u7684\u5185\u5b58\u6dd8\u6c70\u7b97\u6cd5\uff0c\u5728\u4e3b\u6d41\u7684\u7cfb\u7edf\u4e2d\u90fd\u53ef\u4ee5\u89c1\u5230\u76f8\u5e94\u7684\u4f7f\u7528\u573a\u666f\uff0c\u800c\u5728MySQL\u4e0eRedis\u4e2d\u4e5f\u6709\u4f7f\u7528\uff0c\u53ef\u4ee5\u8bf4\u90fd\u662f\u7528\u6765\u5bf9\u5b58\u50a8\u7a7a\u95f4\u8fdb\u884c\u7ba1\u7406\uff0c\u53ca\u65f6\u6dd8\u6c70\u66f4\u65b0\u6570\u636e\uff0c\u63d0\u9ad8\u5b58\u50a8\u7a7a\u95f4\u5229\u7528\u7387\u3002<\/p>\n<h3>Redis\u5185\u5b58\u6dd8\u6c70\u673a\u5236<\/h3>\n<p>\u5728redis.conf\u4e2d\u6709\u4e00\u884c\u53c2\u6570\u7528\u6765\u914d\u7f6e\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u7684<\/p>\n<p><code>maxmemory-policy volatile-LRU<\/code><\/p>\n<p><strong>volatile-LRU<\/strong>\uff1a\u4ece\u5df2\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u96c6\uff08server.db[i].expires\uff09\u4e2d\u6311\u9009\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u6dd8\u6c70<br \/>\n   <strong>volatile-ttl<\/strong>\uff1a\u4ece\u5df2\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u96c6\uff08server.db[i].expires\uff09\u4e2d\u6311\u9009\u5c06\u8981\u8fc7\u671f\u7684\u6570\u636e\u6dd8\u6c70<br \/>\n   <strong>volatile-random<\/strong>\uff1a\u4ece\u5df2\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u96c6\uff08server.db[i].expires\uff09\u4e2d\u4efb\u610f\u9009\u62e9\u6570\u636e\u6dd8\u6c70<br \/>\n   <strong>allkeys-LRU<\/strong>\uff1a\u4ece\u6570\u636e\u96c6\uff08server.db[i].dict\uff09\u4e2d\u6311\u9009\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u6dd8\u6c70<br \/>\n   <strong>allkeys-random<\/strong>\uff1a\u4ece\u6570\u636e\u96c6\uff08server.db[i].dict\uff09\u4e2d\u4efb\u610f\u9009\u62e9\u6570\u636e\u6dd8\u6c70<br \/>\n   <strong>no-enviction<\/strong>\uff08\u9a71\u9010\uff09\uff1a\u7981\u6b62\u9a71\u9010\u6570\u636e\uff0c\u65b0\u5199\u5165\u64cd\u4f5c\u4f1a\u62a5\u9519<br \/>\n\u53ef\u4ee5\u770b\u5f97\u51fa\u6765\u5982\u679c\u4f7f\u7528\u4e86 <strong>volatile-LRU<\/strong>\u7b56\u7565\uff0c\u5219\u5c06\u4f1a\u4f7f\u7528LRU\u6570\u636e\u7ed3\u6784\u6765\u5bf9\u5185\u5b58\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6e05\u7406\u3002<\/p>\n<h3>Redis\u7684LRU\u5177\u4f53\u5b9e\u73b0\uff1a<\/h3>\n<p>\u4f20\u7edf\u7684LRU\u662f\u4f7f\u7528\u6808\u7684\u5f62\u5f0f\uff0c\u6bcf\u6b21\u90fd\u5c06\u6700\u65b0\u4f7f\u7528\u7684\u79fb\u5165\u6808\u9876\uff0c\u4f46\u662f\u7528\u6808\u7684\u5f62\u5f0f\u4f1a\u5bfc\u81f4\u6267\u884cselect *\u7684\u65f6\u5019\u5927\u91cf\u975e\u70ed\u70b9\u6570\u636e\u5360\u9886\u5934\u90e8\u6570\u636e\uff0c\u6240\u4ee5\u9700\u8981\u6539\u8fdb\u3002Redis\u6bcf\u6b21\u6309key\u83b7\u53d6\u4e00\u4e2a\u503c\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u66f4\u65b0value\u4e2d\u7684LRU\u5b57\u6bb5\u4e3a\u5f53\u524d\u79d2\u7ea7\u522b\u7684\u65f6\u95f4\u6233\u3002Redis\u521d\u59cb\u7684\u5b9e\u73b0\u7b97\u6cd5\u5f88\u7b80\u5355\uff0c\u968f\u673a\u4ecedict\u4e2d\u53d6\u51fa\u4e94\u4e2akey\uff0c\u6dd8\u6c70\u4e00\u4e2aLRU\u5b57\u6bb5\u503c\u6700\u5c0f\u7684\u3002\u57283.0\u7684\u65f6\u5019\uff0c\u53c8\u6539\u8fdb\u4e86\u4e00\u7248\u7b97\u6cd5\uff0c\u9996\u5148\u7b2c\u4e00\u6b21\u968f\u673a\u9009\u53d6\u7684key\u90fd\u4f1a\u653e\u5165\u4e00\u4e2apool\u4e2d(pool\u7684\u5927\u5c0f\u4e3a16)\uff0cpool\u4e2d\u7684key\u662f\u6309LRU\u5927\u5c0f\u987a\u5e8f\u6392\u5217\u7684\u3002\u63a5\u4e0b\u6765\u6bcf\u6b21\u968f\u673a\u9009\u53d6\u7684keyLRU\u503c\u5fc5\u987b\u5c0f\u4e8epool\u4e2d\u6700\u5c0f\u7684LRU\u624d\u4f1a\u7ee7\u7eed\u653e\u5165\uff0c\u76f4\u5230\u5c06pool\u653e\u6ee1\u3002\u653e\u6ee1\u4e4b\u540e\uff0c\u6bcf\u6b21\u5982\u679c\u6709\u65b0\u7684key\u9700\u8981\u653e\u5165\uff0c\u9700\u8981\u5c06pool\u4e2dLRU\u6700\u5927\u7684\u4e00\u4e2akey\u53d6\u51fa\u3002\u6dd8\u6c70\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u4ecepool\u4e2d\u9009\u53d6\u4e00\u4e2aLRU\u6700\u5c0f\u7684\u503c\u7136\u540e\u5c06\u5176\u6dd8\u6c70\u3002<\/p>\n<h3>MySQL\u4e2dInnoDB\u5f15\u64ce\u4e2d\u5bf9LRU\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/uos.rain1024.com\/image\/image-20210415142630548.png\" alt=\"image-20210415142630548\" \/><\/p>\n<p>\u7f13\u51b2\u6c60\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u5185\u5b58\u533a\u57df\uff0c\u5176\u4e2d\u5b58\u653e\u5404\u79cd\u7c7b\u578b\u7684\u9875\u3002\u6570\u636e\u5e93\u4e2d\u7684\u7f13\u51b2\u6c60\u662f\u901a\u8fc7LRU\u7b97\u6cd5\u6765\u8fdb\u884c\u7ba1\u7406\u7684\u3002\u56e0\u4e3a\u7f13\u51b2\u6c60\u4e2d\u7684\u9875\u8fd8\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u7ed9\u81ea\u9002\u5e94\u54c8\u5e0c\u7d22\u5f15\u3001Lock\u4fe1\u606f\u3001 Insert Buffer\u7b49\u9875\uff0c\u800c\u8fd9\u90e8\u5206\u9875\u4e0d\u9700\u8981LRU\u7b97\u6cd5\u8fdb\u884c\u7ef4\u62a4\uff0c\u56e0\u6b64\u4e0d\u5b58\u5728\u4e8eLRU\u5217\u8868\u4e2d\u3002<\/p>\n<h3>LRU\u5b58\u50a8\u8fc7\u7a0b<\/h3>\n<p>LRU\u5217\u8868\u7528\u6765\u7ba1\u7406\u5df2\u7ecf\u8bfb\u53d6\u7684\u9875\uff0c\u4f46\u5f53\u6570\u636e\u5e93\u521a\u542f\u52a8\u65f6\uff0cLRU\u5217\u8868\u662f\u7a7a\u7684\uff0c\u5373\u6ca1\u6709\u4efb\u4f55\u7684\u9875\u3002\u8fd9\u65f6\u9875\u90fd\u5b58\u653e\u5728Free\u5217\u8868\u4e2d\uff0c\u5f53\u9700\u8981\u4ece\u7f13\u51b2\u6c60\u4e2d\u5206\u9875\u65f6\uff0c\u9996\u5148\u4eceFree\u5217\u8868\u4e2d\u67e5\u627e\u662f\u5426\u6709\u53ef\u7528\u7684\u7a7a\u95f2\u9875\uff0c\u82e5\u6709\u5219\u5c06\u8be5\u9875\u4eceFree\u5217\u8868\u4e2d\u5220\u9664\uff0c\u653e\u5165\u5230LRU\u5217\u8868\u4e2d\u3002\u5426\u5219,\u6839\u636eLRU\u7b97\u6cd5,\u6dd8\u6c70LRU\u5217\u8868\u672b\u5c3e\u7684\u9875,\u5c06\u8be5\u5185\u5b58\u7a7a\u95f4\u5206\u914d\u7ed9\u65b0\u7684\u9875\u3002<\/p>\n<p>\u6700\u9891\u7e41\u4f7f\u7528\u7684\u9875\u5728LRU\u5217\u8868\u7684\u524d\u7aef\uff0c\u800c\u6700\u5c11\u4f7f\u7528\u7684\u9875\u5728LRU\u5217\u8868\u7684\u5c3e\u7aef\u3002\u5f53\u7f13\u51b2\u6c60\u4e0d\u80fd\u5b58\u653e\u65b0\u8bfb\u53d6\u5230\u7684\u9875\u65f6\uff0c\u5c06\u9996\u5148\u91ca\u653eLRU\u5217\u8868\u4e2d\u5c3e\u7aef\u7684\u9875\u3002\u5728 InnoDB\u5b58\u50a8\u5f15\u64ce\u4e2d\uff0c\u7f13\u51b2\u6c60\u4e2d\u9875\u7684\u5927\u5c0f\u9ed8\u8ba4\u4e3a16KB\uff0c\u540c\u6837\u4f7f\u7528LRU\u7b97\u6cd5\u5bf9\u7f13\u51b2\u6c60\u8fdb\u884c\u7ba1\u7406\u3002\u7a0d\u6709\u4e0d\u540c\u7684\u662fInnoDB\u5b58\u50a8\u5f15\u64ce\u5bf9\u4f20\u7edf\u7684LRU\u7b97\u6cd5\u505a\u4e86\u4e00\u4e9b\u4f18\u5316\u5728InnoDB\u7684\u5b58\u50a8\u5f15\u64ce\u4e2d\uff0cLRU\u5217\u8868\u4e2d\u8fd8\u52a0\u5165\u4e86 midpoint\u4f4d\u7f6e\u3002\u65b0\u8bfb\u53d6\u5230\u7684\u9875\uff0c\u867d\u7136\u662f\u6700\u65b0\u8bbf\u95ee\u7684\u9875\uff0c\u4f46\u5e76\u4e0d\u662f\u76f4\u63a5\u653e\u5165\u5230LRU\u5217\u8868\u7684\u9996\u90e8\uff0c\u800c\u662f\u653e\u5165\u5230LRU\u5217\u8868\u7684 midpoint \u4f4d\u7f6e\u3002\u8fd9\u4e2a\u7b97\u6cd5\u5728 InnoDB\u5b58\u50a8\u5f15\u64ce\u4e0b\u79f0\u4e3a midpoint insertion strategy\u3002\u5728\u9ed8\u8ba4\u914d\u7f6e\u4e0b,\u8be5\u4f4d\u7f6e\u5728LRU\u5217\u8868\u957f\u5ea6\u768458\u5904\u3002 midpoint 1\u4f4d\u7f6e\u53ef\u7531\u53c2\u6570 innodb old blocks pct:\u63a7\u5236\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u91c7\u7528\u6734\u7d20\u7684LRU\u7b97\u6cd5\uff0c\u76f4\u63a5\u5c06\u8bfb\u53d6\u7684\u9875\u653e\u5165\u5230LRU\u5217\u8868\u7684\u9996\u90e8\u5462\uff0c\u8fd9\u662f\u56e0\u4e3a\u82e5\u76f4\u63a5\u5c06\u8bfb\u53d6\u5230\u7684\u9875\u653e\u5165\u5230LRU\u7684\u9996\u90e8\uff0c\u90a3\u4e48\u67d0\u4e9bSQL\u64cd\u4f5c\u53ef\u80fd\u4f1a\u4f7f\u7f13\u51b2\u6c60\u4e2d\u7684\u9875\u88ab\u5237\u65b0\u51fa\uff0c\u4ece\u800c\u5f71\u54cd\u7f13\u51b2\u6c60\u7684\u6548\u7387\u3002\u5e38\u89c1\u7684\u8fd9\u7c7b\u64cd\u4f5c\u4e3a\u7d22\u5f15\u6216\u6570\u636e\u7684\u626b\u63cf\u64cd\u4f5c\u3002\u8fd9\u7c7b\u64cd\u4f5c\u9700\u8981\u8bbf\u95ee\u8868\u4e2d\u7684\u8bb8\u591a\u9875\uff0c\u751a\u81f3\u662f\u5168\u90e8\u7684\u9875\uff0c\u800c\u8fd9\u4e9b\u9875\u901a\u5e38\u6765\u8bf4\u53c8\u4ec5\u5728\u8fd9\u6b21\u67fb\u8be2\u64cd\u4f5c\u4e2d\u9700\u8981\uff0c\u5e76\u4e0d\u662f\u6d3b\u8dc3\u7684\u70ed\u70b9\u6570\u636e\u3002\u5982\u679c\u9875\u88ab\u653e\u5165LRU\u5217\u8868\u7684\u9996\u90e8\uff0c\u90a3\u4e48\u975e\u5e38\u53ef\u80fd\u5c06\u6240\u9700\u8981\u7684\u70ed\u70b9\u6570\u636e\u9875\u4eceLRU\u5217\u8868\u4e2d\u79fb\u9664\uff0c\u800c\u5728\u4e0b\u4e00\u6b21\u9700\u8981\u8bfb\u53d6\u8be5\u9875\u65f6\uff0cInnoDB\u5b58\u50a8\u5f15\u64ce\u9700\u8981\u518d\u6b21\u8bbf\u95ee\u78c1\u76d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LRU\u4e5f\u79f0\u4e3a\u6700\u8fd1\u6700\u5c11\u672a\u4f7f\u7528\u7b97\u6cd5\uff0c\u4f5c\u4e3a\u6700\u5e38\u7528\u7684\u5185\u5b58\u6dd8\u6c70\u7b97\u6cd5\uff0c\u5728\u4e3b\u6d41\u7684\u7cfb\u7edf\u4e2d\u90fd\u53ef\u4ee5\u89c1\u5230\u76f8\u5e94\u7684\u4f7f\u7528\u573a\u666f\uff0c\u800c\u5728MySQL\u4e0eRedis\u4e2d\u4e5f\u6709\u4f7f\u7528\uff0c\u53ef\u4ee5\u8bf4\u90fd\u662f\u7528\u6765\u5bf9\u5b58\u50a8\u7a7a\u95f4\u8fdb\u884c\u7ba1\u7406\uff0c\u53ca\u65f6\u6dd8\u6c70\u66f4\u65b0\u6570\u636e\uff0c\u63d0\u9ad8\u5b58\u50a8\u7a7a\u95f4\u5229\u7528\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1165","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=1165"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1165\/revisions"}],"predecessor-version":[{"id":1334,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1165\/revisions\/1334"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=1165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=1165"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=1165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}