{"id":201192,"date":"2025-05-10T09:12:34","date_gmt":"2025-05-10T01:12:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201192\/"},"modified":"2025-05-10T09:12:34","modified_gmt":"2025-05-10T01:12:34","slug":"%e6%b5%85%e8%b0%88redis%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e6%b7%98%e6%b1%b0%e7%ad%96%e7%95%a5%e5%92%8c%e8%bf%87%e6%9c%9f%e9%94%ae%e5%88%a0%e9%99%a4%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201192\/","title":{"rendered":"\u6d45\u8c08Redis\u4e2d\u7684\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u548c\u8fc7\u671f\u952e\u5220\u9664\u7b56\u7565"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u6d45\u8c08Redis\u4e2d\u7684\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u548c\u8fc7\u671f\u952e\u5220\u9664\u7b56\u7565<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u6d45\u8c08Redis\u4e2d\u7684\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u548c\u8fc7\u671f\u952e\u5220\u9664\u7b56\u7565\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>redis\u662f\u6211\u4eec\u73b0\u5728\u6700\u5e38\u7528\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u5e2e\u52a9\u6211\u4eec\u5efa\u8bbe\u7cfb\u7edf\u7684\u9ad8\u53ef\u7528\uff0c\u9ad8\u6027\u80fd\u3002<\/p>\n<p>\u800c\u4e14\u6211\u4eec\u90fd\u77e5\u9053redis\u662f\u4e00\u4e2a\u5b8c\u5168\u57fa\u4e8e\u5185\u5b58\u7684\u5de5\u5177\uff0c\u8fd9\u4e5f\u662fredis\u901f\u5ea6\u5feb\u7684\u4e00\u4e2a\u539f\u56e0\uff0c\u5f53\u6211\u4eec\u5f80redis\u4e2d\u4e0d\u65ad\u7f13\u5b58\u6570\u636e\u7684\u65f6\u5019\uff0c\u5176\u5185\u5b58\u603b\u6709\u6ee1\u7684\u65f6\u5019(\u800c\u4e14\u5185\u5b58\u662f\u5f88\u8d35\u7684\u4e1c\u897f\uff0c\u5c3d\u91cf\u7701\u7740\u70b9\u7528)\uff0c\u6240\u4ee5\u5c3d\u53ef\u80fd\u628a\u6709\u7528\u7684\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528\u9891\u7e41\u7684\u6570\u636e\u7f13\u5b58\u5728redis\u4e2d\uff0c\u7269\u5c3d\u5176\u7528\u3002<\/p>\n<p>\u90a3\u4e48\u5982\u679c\u6b63\u5728\u4f7f\u7528\u7684redis\u5185\u5b58\u7528\u5b8c\u4e86\uff0c\u6211\u4eec\u5e94\u8be5\u600e\u4e48\u53d6\u820dredis\u4e2d\u5df2\u5b58\u5728\u7684\u6570\u636e\u548c\u5373\u5c06\u8981\u5b58\u5165\u7684\u6570\u636e\u5462\uff0c\u6211\u4eec\u8981\u600e\u4e48\u5904\u7406\u5462\uff1f<\/p>\n<p><strong>redis\u5b98\u65b9\u63d0\u4f9b\u4e868\u79cd\u4e0d\u540c\u7684\u6dd8\u6c70\u7b56\u7565<\/strong><\/p>\n<p>redis.conf \u662f\u4e2a\u597d\u4e1c\u897f\uff0c\u51e0\u4e4eredis\u7684\u6240\u6709\u914d\u7f6e\u90fd\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff0c\u6839\u636econf\u4e2d\u7684\u8bf4\u660e\u4e5f\u5c31\u80fd\u64cd\u4f5c\u4e86<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245650263afa9368ffa0.jpg?2021815112913\" style=\"max-width:100%\" class=\"aligncenter\"><\/p>\n<p>\u6211\u4eec\u770b\u4e0bredis.conf\u4e2d\u5173\u4e8e8\u4e2d\u6dd8\u6c70\u7b56\u7565\u7684\u8bf4\u660e(\u672c\u6587\u4f7f\u7528redis\u7248\u672c\u662f4.0.9)<\/p>\n<pre>\nvolatile-lru -&gt; Evict using approximated LRU among the keys with an expire set.\nallkeys-lru -&gt; Evict any key using approximated LRU.\nvolatile-lfu -&gt; Evict using approximated LFU among the keys with an expire set.\nallkeys-lfu -&gt; Evict any key using approximated LFU.\nvolatile-random -&gt; Remove a random key among the ones with an expire set.\nallkeys-random -&gt; Remove a random key, any key.\nvolatile-ttl -&gt; Remove the key with the nearest expire time (minor TTL)\nnoeviction -&gt; Don't evict anything, just return an error on write operations.\n\n# LRU means Least Recently Used(\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\uff0c\u65f6\u95f4)\n# LFU means Least Frequently Used(\u6700\u4e0d\u7ecf\u5e38\u4f7f\u7528\u7684\uff0c\u6b21\u6570)\n# The default is:\nmaxmemory-policy noeviction\n\n<\/pre>\n<p>\u4e0a\u6587\u662f\u4eceredis.conf\u4e2d\u6458\u51fa\u6765\u5173\u4e8e\u6dd8\u6c70\u7b56\u7565\u76848\u79cd\u914d\u7f6e\u4ee5\u53ca\u8bbe\u7f6e\u8bf4\u660e\uff0c\u5176\u4e2dmaxmemory-policy noeviction \u4ee3\u8868\u4e86\u6dd8\u6c70\u7b56\u7565\u9ed8\u8ba4\u7684\u662fnoeviction,\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u4e1a\u52a1\u9700\u6c42\u4fee\u6539\u5408\u9002\u7684\u7b56\u7565\u3002<br \/> \u82f1\u6587\u770b\u4e0d\u61c2\u7684\u770b\u4e0b\u9762\u7684\u4e2d\u6587<\/p>\n<h2>8\u79cd\u6dd8\u6c70\u7b56\u7565<\/h2>\n<ul>\n<li>volatile-lru\uff1a\u5728\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u4e2d\u6dd8\u6c70\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u3002<\/li>\n<li>allkeys-lru\uff1a\u5728\u6240\u6709\u7684\u6570\u636e\u4e2d\u6dd8\u6c70\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u3002<\/li>\n<li>volatile-lfu\uff1a\u5728\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u4e2d\u6dd8\u6c70\u4f7f\u7528\u9891\u7387\u6700\u4f4e\u7684\u6570\u636e\u3002<\/li>\n<li>allkeys-lfu\uff1a\u5728\u6240\u6709\u7684\u6570\u636e\u4e2d\u6dd8\u6c70\u4f7f\u7528\u4f7f\u7528\u9891\u7387\u6700\u4f4e\u7684\u6570\u636e\u3002<\/li>\n<li>volatile-random\uff1a\u5728\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u4e2d\u6dd8\u6c70\u4efb\u610f\u968f\u673a\u6570\u636e\u3002<\/li>\n<li>allkeys-random\uff1a\u5728\u6240\u6709\u7684\u6570\u636e\u4e2d\u968f\u673a\u6dd8\u6c70\u6570\u636e\u3002<\/li>\n<li>volatile-ttl\uff1a\u5728\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684\u6570\u636e\u4e2d\u6dd8\u6c70\u6700\u65e9\u8fc7\u671f\u7684\u6570\u636e\u3002<\/li>\n<li>noeviction\uff1a\u9ed8\u8ba4\u7b56\u7565\uff0c\u4e0d\u6dd8\u6c70\u6570\u636e\uff0c\u65b0\u589e\u6216\u8005\u4fee\u6539\u6570\u636e\u4f1a\u629b\u5f02\u5e38\uff0c\u4f46\u662f\u8bfb\u64cd\u4f5c\u6b63\u5e38\u8fdb\u884c\uff0c\u4e0d\u53d7\u5f71\u54cd<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u662f\u5185\u5b58\u4e0d\u8db3\u7684\u6dd8\u6c70\u7b56\u7565\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u8fc7\u671f\u952e\u7684\u5220\u9664\u7b56\u7565\uff0c\u4e24\u8005\u662f\u4e0d\u540c\uff0c\u4e0d\u8981\u641e\u6df7\u4e86<\/p>\n<h2>\u8fc7\u671f\u952e\u7684\u5220\u9664\u7b56\u7565<\/h2>\n<ul>\n<li>\u5b9a\u65f6\u8fc7\u671f\uff1a\u6bcf\u4e2a\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684key\u90fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u5230\u8fc7\u671f\u65f6\u95f4\u5c31\u4f1a\u7acb\u5373\u6e05\u9664\u3002\u8be5\u7b56\u7565\u53ef\u4ee5\u7acb\u5373\u6e05\u9664\u8fc7\u671f\u7684\u6570\u636e\uff0c\u5bf9\u5185\u5b58\u5f88\u53cb\u597d\uff1b\u4f46\u662f\u4f1a\u5360\u7528\u5927\u91cf\u7684CPU\u8d44\u6e90\u53bb\u5904\u7406\u8fc7\u671f\u7684\u6570\u636e\uff0c\u4ece\u800c\u5f71\u54cd\u7f13\u5b58\u7684\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u3002<\/li>\n<li>\u60f0\u6027\u8fc7\u671f\uff1a\u53ea\u6709\u5f53\u8bbf\u95ee\u4e00\u4e2akey\u65f6\uff0c\u624d\u4f1a\u5224\u65ad\u8be5key\u662f\u5426\u5df2\u8fc7\u671f\uff0c\u8fc7\u671f\u5219\u6e05\u9664\u3002\u8be5\u7b56\u7565\u53ef\u4ee5\u6700\u5927\u5316\u5730\u8282\u7701CPU\u8d44\u6e90\uff0c\u5374\u5bf9\u5185\u5b58\u975e\u5e38\u4e0d\u53cb\u597d\u3002\u6781\u7aef\u60c5\u51b5\u53ef\u80fd\u51fa\u73b0\u5927\u91cf\u7684\u8fc7\u671fkey\u6ca1\u6709\u518d\u6b21\u88ab\u8bbf\u95ee\uff0c\u4ece\u800c\u4e0d\u4f1a\u88ab\u6e05\u9664\uff0c\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002<\/li>\n<li>\u5b9a\u671f\u8fc7\u671f\uff1a\u6bcf\u9694\u4e00\u5b9a\u7684\u65f6\u95f4\uff0c\u4f1a\u626b\u63cf\u4e00\u5b9a\u6570\u91cf\u7684\u6570\u636e\u5e93\u7684expires\u5b57\u5178\u4e2d\u4e00\u5b9a\u6570\u91cf\u7684key\uff0c\u5e76\u6e05\u9664\u5176\u4e2d\u5df2\u8fc7\u671f\u7684key\u3002\u8be5\u7b56\u7565\u662f\u524d\u4e24\u8005\u7684\u4e00\u4e2a\u6298\u4e2d\u65b9\u6848\u3002\u901a\u8fc7\u8c03\u6574\u5b9a\u65f6\u626b\u63cf\u7684\u65f6\u95f4\u95f4\u9694\u548c\u6bcf\u6b21\u626b\u63cf\u7684\u9650\u5b9a\u8017\u65f6\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u4f7f\u5f97CPU\u548c\u5185\u5b58\u8d44\u6e90\u8fbe\u5230\u6700\u4f18\u7684\u5e73\u8861\u6548\u679c\u3002<\/li>\n<\/ul>\n<p>expires\u5b57\u5178\u4f1a\u4fdd\u5b58\u6240\u6709\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684key\u7684\u8fc7\u671f\u65f6\u95f4\u6570\u636e\uff0c\u5176\u4e2d\uff0c<br \/> key\u662f\u6307\u5411\u952e\u7a7a\u95f4\u4e2d\u7684\u67d0\u4e2a\u952e\u7684\u6307\u9488\uff0c<br \/> value\u662f\u8be5\u952e\u7684\u6beb\u79d2\u7cbe\u5ea6\u7684UNIX\u65f6\u95f4\u6233\u8868\u793a\u7684\u8fc7\u671f\u65f6\u95f4\u3002<br \/> \u952e\u7a7a\u95f4\u662f\u6307\u8be5Redis\u96c6\u7fa4\u4e2d\u4fdd\u5b58\u7684\u6240\u6709\u952e\u3002<\/p>\n<h2>\u603b\u7ed3<br \/><\/h2>\n<p>Redis\u7684\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u7684\u9009\u53d6\u5e76\u4e0d\u4f1a\u5f71\u54cd\u8fc7\u671f\u7684key\u7684\u5904\u7406\u3002\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u7528\u4e8e\u5904\u7406\u5185\u5b58\u4e0d\u8db3\u65f6\u7684\u9700\u8981\u7533\u8bf7\u989d\u5916\u7a7a\u95f4\u7684\u6570\u636e\uff1b\u8fc7\u671f\u7b56\u7565\u7528\u4e8e\u5904\u7406\u8fc7\u671f\u7684\u7f13\u5b58\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d45\u8c08Redis\u4e2d\u7684\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\u548c&#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-201192","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201192","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=201192"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201192\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}