{"id":201308,"date":"2025-05-10T09:06:47","date_gmt":"2025-05-10T01:06:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201308\/"},"modified":"2025-05-10T09:06:47","modified_gmt":"2025-05-10T01:06:47","slug":"redis%e5%86%85%e5%ad%98%e5%9b%9e%e6%94%b6%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201308\/","title":{"rendered":"Redis\u5185\u5b58\u56de\u6536\u7b56\u7565"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5185\u5b58\u56de\u6536\u7b56\u7565<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aRedis\u5185\u5b58\u56de\u6536\u7b56\u7565\u300b\uff0c\u4e3b\u8981\u5305\u62ecRedis\u5185\u5b58\u56de\u6536\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<h2><span style=\"background-color: initial\">\u6982\u8ff0<\/span><\/h2>\n<p>Redis\u4e5f\u4f1a\u56e0\u4e3a\u5185\u5b58\u4e0d\u8db3\u800c\u4ea7\u751f\u9519\u8bef \uff0c \u4e5f\u53ef\u80fd\u56e0\u4e3a\u56de\u6536\u8fc7\u4e45\u800c\u5bfc\u81f4\u7cfb\u7edf\u957f\u671f\u7684\u505c\u987f\uff0c\u56e0\u6b64\u638c\u63e1\u6267\u884c\u56de\u6536\u7b56\u7565\u5341\u5206\u6709\u5fc5\u8981\u3002\u5728 Redis \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u5f53 Redis \u7684\u5185\u5b58\u8fbe\u5230\u89c4\u5b9a\u7684\u6700\u5927\u503c\u65f6\uff0c\u5141\u8bb8\u914d\u7f6e 6 \u79cd\u7b56\u7565\u4e2d\u7684\u4e00\u79cd\u8fdb\u884c\u6dd8\u6c70\u952e\u503c\uff0c\u5e76\u4e14\u5c06\u4e00\u4e9b\u952e\u503c\u5bf9\u8fdb\u884c\u56de\u6536\u3002<\/p>\n<h2>maxmemory-policy \u53c2\u6570<\/h2>\n<pre>\n# Set a memory usage limit to the specified amount of bytes.\n# When the memory limit is reached Redis will try to remove keys\n# according to the eviction policy selected (see maxmemory-policy).\n#\n# If Redis can't remove keys according to the policy, or if the policy is\n# set to 'noeviction', Redis will start to reply with errors to commands\n# that would use more memory, like SET, LPUSH, and so on, and will continue\n# to reply to read-only commands like GET.\n#\n# This option is usually useful when using Redis as an LRU or LFU cache, or to\n# set a hard memory limit for an instance (using the 'noeviction' policy).\n#\n# WARNING: If you have slaves attached to an instance with maxmemory on,\n# the size of the output buffers needed to feed the slaves are subtracted\n# from the used memory count, so that network problems \/ resyncs will\n# not trigger a loop where keys are evicted, and in turn the output\n# buffer of slaves is full with DELs of keys evicted triggering the deletion\n# of more keys, and so forth until the database is completely emptied.\n#\n# In short... if you have slaves attached it is suggested that you set a lower\n# limit for maxmemory so that there is some free RAM on the system for slave\n# output buffers (but this is not needed if the policy is 'noeviction').\n#\n# maxmemory \n   \n    \n\n# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory\n# is reached. You can select among five behaviors:\n#\n# volatile-lru -&gt; Evict using approximated LRU among the keys with an expire set.\n# allkeys-lru -&gt; Evict any key using approximated LRU.\n# volatile-lfu -&gt; Evict using approximated LFU among the keys with an expire set.\n# allkeys-lfu -&gt; Evict any key using approximated LFU.\n# volatile-random -&gt; Remove a random key among the ones with an expire set.\n# allkeys-random -&gt; Remove a random key, any key.\n# volatile-ttl -&gt; Remove the key with the nearest expire time (minor TTL)\n# noeviction -&gt; Don't evict anything, just return an error on write operations.\n#\n# LRU means Least Recently Used\n# LFU means Least Frequently Used\n#\n# Both LRU, LFU and volatile-ttl are implemented using approximated\n# randomized algorithms.\n#\n# Note: with any of the above policies, Redis will return an error on write\n#       operations, when there are no suitable keys for eviction.\n#\n#       At the date of writing these commands are: set setnx setex append\n#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd\n#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby\n#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby\n#       getset mset msetnx exec sort\n#\n# The default is:\n#\n# maxmemory-policy noeviction\n   <\/pre>\n<h2>\u4e3b\u52a8\u6e05\u7406\u7b56\u7565<\/h2>\n<p>\u4e3b\u52a8\u6e05\u7406\u7b56\u7565\u5728Redis 4.0 \u4e4b\u524d\u4e00\u5171\u5b9e\u73b0\u4e86 6 \u79cd\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\uff0c\u5728 4.0 \u4e4b\u540e\uff0c\u53c8\u589e\u52a0\u4e86 2 \u79cd\u7b56\u7565\uff0c\u603b\u51718\u79cd\uff1a<\/p>\n<p>\u3010\u9488\u5bf9\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684key\u505a\u5904\u7406\u3011<\/p>\n<ul>\n<li>volatile-ttl\uff1a\u5728\u7b5b\u9009\u65f6\uff0c\u4f1a\u9488\u5bf9\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u503c\u5bf9\uff0c\u6839\u636e\u8fc7\u671f\u65f6\u95f4\u7684\u5148\u540e\u8fdb\u884c\u5220\u9664\uff0c\u8d8a\u65e9\u8fc7\u671f\u7684\u8d8a\u5148\u88ab\u5220\u9664\u3002<\/li>\n<li>volatile-random\uff1a\u5c31\u50cf\u5b83\u7684\u540d\u79f0\u4e00\u6837\uff0c\u5728\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u503c\u5bf9\u4e2d\uff0c\u8fdb\u884c\u968f\u673a\u5220\u9664\u3002<\/li>\n<li>volatile-lru\uff1a\u4f1a\u4f7f\u7528 LRU \u7b97\u6cd5\u7b5b\u9009\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u503c\u5bf9\u5220\u9664\u3002<\/li>\n<li>volatile-lfu\uff1a\u4f1a\u4f7f\u7528 LFU \u7b97\u6cd5\u7b5b\u9009\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u503c\u5bf9\u5220\u9664<\/li>\n<\/ul>\n<p>\u3010 \u9488\u5bf9\u6240\u6709\u7684key\u505a\u5904\u7406\u3011<\/p>\n<ul>\n<li>allkeys-random\uff1a\u4ece\u6240\u6709\u952e\u503c\u5bf9\u4e2d\u968f\u673a\u9009\u62e9\u5e76\u5220\u9664\u6570\u636e\u3002<\/li>\n<li>allkeys-lru\uff1a\u4f7f\u7528 LRU \u7b97\u6cd5\u5728\u6240\u6709\u6570\u636e\u4e2d\u8fdb\u884c\u7b5b\u9009\u5220\u9664\u3002<\/li>\n<li>allkeys-lfu\uff1a\u4f7f\u7528 LFU \u7b97\u6cd5\u5728\u6240\u6709\u6570\u636e\u4e2d\u8fdb\u884c\u7b5b\u9009\u5220\u9664\u3002<\/li>\n<\/ul>\n<p>\u3010 \u4e0d\u5904\u7406 \uff08\u9ed8\u8ba4\uff09\u3011<\/p>\n<p>noeviction\uff1a\u4e0d\u4f1a\u5254\u9664\u4efb\u4f55\u6570\u636e\uff0c\u62d2\u7edd\u6240\u6709\u5199\u5165\u64cd\u4f5c\u5e76\u8fd4\u56de\u5ba2\u6237\u7aef\u9519\u8bef\u4fe1\u606f&#8221;(error) OOM command not allowed when used memory&#8221;\uff0c\u6b64\u65f6Redis\u53ea\u54cd\u5e94\u8bfb\u64cd\u4f5c\u3002<\/p>\n<p>Redis \u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u91c7\u7528 noeviction \u7b56\u7565\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u5185\u5b58\u5df1\u6ee1 \uff0c \u5219\u4e0d\u518d\u63d0\u4f9b\u5199\u5165\u64cd\u4f5c \uff0c \u800c\u53ea\u63d0\u4f9b\u8bfb\u53d6\u64cd\u4f5c \u3002 \u663e\u7136\u8fd9\u5f80\u5f80\u5e76\u4e0d\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u8981\u6c42\uff0c\u56e0\u4e3a\u5bf9\u4e8e\u4e92\u8054\u7f51\u7cfb\u7edf\u800c\u8a00 \uff0c \u5e38\u5e38\u4f1a\u6d89\u53ca\u6570\u4ee5\u767e\u4e07\u751a\u81f3\u66f4\u591a\u7684\u7528\u6237 \uff0c \u6240\u4ee5\u5f80\u5f80\u9700\u8981\u8bbe\u7f6e\u56de\u6536\u7b56\u7565\u3002<\/p>\n<h2>\u7b56\u7565\u9009\u62e9<\/h2>\n<p>LRU \u7b97\u6cd5\uff08Least Recently Used\uff0c\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff09\uff1a\u6dd8\u6c70\u5f88\u4e45\u6ca1\u88ab\u8bbf\u95ee\u8fc7\u7684\u6570\u636e\uff0c\u4ee5\u6700\u8fd1\u4e00\u6b21\u8bbf\u95ee\u65f6\u95f4\u4f5c\u4e3a\u53c2\u8003<\/p>\n<p>LFU \u7b97\u6cd5\uff08Least Frequently Used\uff0c\u6700\u4e0d\u7ecf\u5e38\u4f7f\u7528\uff09\uff1a\u6dd8\u6c70\u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u88ab\u8bbf\u95ee\u6b21\u6570\u6700\u5c11\u7684\u6570\u636e\uff0c\u4ee5\u6b21\u6570\u4f5c\u4e3a\u53c2\u8003<\/p>\n<p>\u9700\u8981\u6307\u51fa\u7684\u662f \uff1a LRU \u7b97\u6cd5\u6216\u8005 TTL \u7b97\u6cd5\u90fd\u662f\u4e0d\u662f\u5f88\u7cbe\u786e\u7b97\u6cd5\uff0c\u800c\u662f\u4e00\u4e2a\u8fd1\u4f3c\u7684\u7b97\u6cd5\u3002 Redis \u4e0d\u4f1a\u901a\u8fc7\u5bf9\u5168\u90e8\u7684\u952e\u503c\u5bf9\u8fdb\u884c\u6bd4\u8f83\u6765\u786e\u5b9a\u6700\u7cbe\u786e\u7684\u65f6\u95f4\u503c\uff0c\u4ece\u800c\u786e\u5b9a\u5220\u9664\u54ea\u4e2a\u952e\u503c\u5bf9 \uff0c \u56e0\u4e3a\u8fd9\u5c06\u6d88\u8017\u592a\u591a\u7684\u65f6\u95f4 \uff0c \u5bfc\u81f4\u56de\u6536\u5783\u573e\u6267\u884c\u7684\u65f6\u95f4\u592a\u957f \uff0c \u9020\u6210\u670d\u52a1\u505c\u987f.<\/p>\n<p>\u5f53\u5b58\u5728\u70ed\u70b9\u6570\u636e\u65f6\uff0cLRU\u7684\u6548\u7387\u5f88\u597d\uff0c\u4f46\u5076\u53d1\u6027\u7684\u3001\u5468\u671f\u6027\u7684\u6279\u91cf\u64cd\u4f5c\u4f1a\u5bfc\u81f4LRU\u547d\u4e2d\u7387\u6025\u5267\u4e0b\u964d\uff0c\u7f13\u5b58\u6c61\u67d3\u60c5\u51b5\u6bd4\u8f83\u4e25\u91cd\u3002\u8fd9\u65f6\u4f7f\u7528LFU\u53ef\u80fd\u66f4\u597d\u70b9<\/p>\n<p>\u6839\u636e\u81ea\u8eab\u4e1a\u52a1\u7c7b\u578b\uff0c\u914d\u7f6e\u597dmaxmemory-policy(\u9ed8\u8ba4\u662fnoeviction)\uff0c\u63a8\u8350\u4f7f\u7528volatile-lru\u3002<\/p>\n<h2>maxmemory-sample<\/h2>\n<p>\u800c\u5728Redis \u7684\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\u4e2d \uff0c \u5b58\u5728\u7740\u53c2\u6570 maxmemory-sample<\/p>\n<pre>\n# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated\n# algorithms (in order to save memory), so you can tune it for speed or\n# accuracy. For default Redis will check five keys and pick the one that was\n# used less recently, you can change the sample size using the following\n# configuration directive.\n#\n# The default of 5 produces good enough results. 10 Approximates very closely\n# true LRU but costs more CPU. 3 is faster but not very accurate.\n#\n# maxmemory-samples 5<\/pre>\n<p>\u5f53\u8bbe\u7f6e maxmemory-samples\u8d8a\u5927\uff0c\u5219 Redis \u5220\u9664\u7684\u5c31\u8d8a\u7cbe\u786e\uff0c\u4f46\u662f\u4e0e\u6b64\u540c\u65f6\u5e26\u6765\u4e0d\u5229\u7684\u662f\uff0c Redis \u4e5f\u5c31\u9700\u8981\u82b1\u66f4\u591a\u7684\u65f6\u53bb\u8ba1\u7b97\u5339\u914d\u66f4\u4e3a\u7cbe\u786e\u7684\u503c \u3002<\/p>\n<p>\u56de\u6536\u8d85\u65f6\u7b56\u7565\u7684\u7f3a\u70b9\u662f\u5fc5\u987b\u6307\u660e\u8d85\u65f6\u7684\u952e\u503c\u5bf9 \uff0c\u8fd9\u4f1a\u7ed9\u7a0b\u5e8f\u5f00\u53d1\u5e26\u6765\u4e00\u4e9b\u8bbe\u7f6e\u8d85\u65f6\u7684\u4ee3\u7801\uff0c\u65e0\u7591\u589e\u52a0\u4e86\u5f00\u53d1\u8005\u7684\u5de5\u4f5c\u91cf\u3002<\/p>\n<p>\u5bf9\u6240\u6709\u7684\u952e\u503c\u5bf9\u8fdb\u884c\u56de\u6536\uff0c\u6709\u53ef\u80fd\u628a\u6b63\u5728\u4f7f\u7528\u7684\u952e\u503c\u5bf9\u5220\u6389\uff0c\u589e\u52a0\u4e86\u5b58\u50a8\u7684\u4e0d\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u5bf9\u4e8e\u5783\u573e\u56de\u6536\u7684\u7b56\u7565\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\u56de\u6536\u7684\u65f6\u95f4\uff0c\u56e0\u4e3a\u5728 Redis \u5bf9\u5783\u573e\u7684\u56de\u6536\u671f\u95f4\uff0c \u4f1a\u9020\u6210\u7cfb\u7edf\u7f13\u6162\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u63a7\u5236\u5176\u56de\u6536\u65f6\u95f4\u6709\u4e00\u5b9a\u597d\u5904\uff0c\u53ea\u662f\u8fd9\u4e2a\u65f6\u95f4\u4e0d\u80fd\u8fc7\u77ed\u6216\u8fc7\u957f\u3002\u8fc7\u77ed\u5219\u4f1a\u9020\u6210\u56de\u6536\u6b21\u6570\u8fc7\u4e8e\u9891\u7e41\uff0c\u8fc7\u957f\u5219\u5bfc\u81f4\u7cfb\u7edf\u5355\u6b21\u5783\u573e\u56de\u6536\u505c\u987f\u65f6\u95f4\u8fc7\u957f\uff0c\u90fd\u4e0d\u5229\u4e8e\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\uff0c\u8fd9\u4e9b\u90fd\u9700\u8981\u8bbe\u8ba1\u8005\u5728\u5b9e\u9645\u7684\u5de5\u4f5c\u4e2d\u8fdb\u884c\u601d\u8003 \u3002<\/p>\n<p>\u5982\u679c\u4e0d\u8bbe\u7f6e\u6700\u5927\u5185\u5b58\uff0c\u5f53 Redis \u5185\u5b58\u8d85\u51fa\u7269\u7406\u5185\u5b58\u9650\u5236\u65f6\uff0c\u5185\u5b58\u7684\u6570\u636e\u4f1a\u5f00\u59cb\u548c\u78c1\u76d8\u4ea7\u751f\u9891\u7e41\u7684\u4ea4\u6362 (swap)\uff0c\u4f1a\u8ba9 Redis \u7684\u6027\u80fd\u6025\u5267\u4e0b\u964d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5185\u5b58\u56de\u6536\u7b56\u7565 \u6536\u85cf \u672c&#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-201308","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201308","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=201308"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201308\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}