{"id":201327,"date":"2025-05-10T10:52:41","date_gmt":"2025-05-10T02:52:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201327\/"},"modified":"2025-05-10T10:52:41","modified_gmt":"2025-05-10T02:52:41","slug":"%e8%ae%be%e7%bd%aeredis%e6%9c%80%e5%a4%a7%e5%8d%a0%e7%94%a8%e5%86%85%e5%ad%98%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201327\/","title":{"rendered":"\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u7684\u5b9e\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u7684\u5b9e\u73b0<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cgolang\u5b66\u4e60\u7f51\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u7684\u5b9e\u73b0\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86Redis\u6700\u5927\u5360\u7528\u5185\u5b58\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>Redis\u9700\u8981\u8bbe\u7f6e\u6700\u5927\u5360\u7528\u5185\u5b58\u5417\uff1f\u5982\u679cRedis\u5185\u5b58\u4f7f\u7528\u8d85\u51fa\u4e86\u8bbe\u7f6e\u7684\u6700\u5927\u503c\u4f1a\u600e\u6837\uff1f<\/p>\n<h2>\u6253\u5f00redis\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>\u627e\u5230\u5982\u4e0b\u6bb5\u843d\uff0c\u8bbe\u7f6emaxmemory\u53c2\u6570\uff0cmaxmemory\u662fbytes\u5b57\u8282\u7c7b\u578b\uff0c\u6ce8\u610f\u8f6c\u6362\u3002\u4fee\u6539\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre># 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    \nmaxmemory 268435456\n   <\/pre>\n<p>\u672c\u673a\u670d\u52a1\u5668redis\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff1a\/usr\/local\/openresty\/lualib\/redis\/redis.conf\uff0c\u7531\u4e8e\u672c\u673a\u81ea\u5e26\u5185\u5b58\u53ea\u67094G\uff0c\u4e00\u822c\u63a8\u8350Redis\u8bbe\u7f6e\u5185\u5b58\u4e3a\u6700\u5927\u7269\u7406\u5185\u5b58\u7684\u56db\u5206\u4e4b\u4e09\uff0c\u6240\u4ee5\u8bbe\u7f6e3G\uff0c\u6362\u6210Byte\u662f3221225472.<\/p>\n<h2>\u6211\u4eec\u53ef\u4ee5\u5728CentOS\u4e0b\u8f93\u5165\u547d\u4ee4\uff1afind \/ -name redis\u67e5\u627eredis\u76ee\u5f55\uff1a<\/h2>\n<pre>[root@VM-8-8-centos ~]# find \/ -name redis\n\/etc\/selinux\/targeted\/active\/modules\/100\/redis\n\/usr\/local\/openresty\/lualib\/redis<\/pre>\n<h2>Redis\u4f7f\u7528\u8d85\u8fc7\u8bbe\u7f6e\u7684\u6700\u5927\u503c<\/h2>\n<p>\u5982\u679cRedis\u7684\u4f7f\u7528\u8d85\u8fc7\u4e86\u8bbe\u7f6e\u7684\u6700\u5927\u503c\u4f1a\u600e\u6837\uff1f\u8ba9\u6211\u4eec\u6765\u6539\u4e00\u6539\u4e0a\u9762\u7684\u914d\u7f6e\uff0c\u6545\u610f\u628a\u6700\u5927\u503c\u8bbe\u4e3a1\u4e2abyte\u8bd5\u8bd5\u3002<\/p>\n<pre># output buffers (but this is not needed if the policy is 'noeviction').\n#\n# maxmemory \n   \n    \nmaxmemory 1\n   <\/pre>\n<p>\u6253\u5f00debug\u6a21\u5f0f\u4e0b\u7684\u9875\u9762\uff0c\u63d0\u793a\u9519\u8bef\uff1a<strong>OOM command not allowed when used memory &gt; \u2018maxmemory\u2019<\/strong>.<\/p>\n<p>\u8bbe\u7f6e\u4e86maxmemory\u7684\u9009\u9879\uff0credis\u5185\u5b58\u4f7f\u7528\u8fbe\u5230\u4e0a\u9650\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<strong>LRU\u7b97\u6cd5<\/strong>\u6765<strong>\u5220\u9664\u90e8\u5206key<\/strong>\uff0c\u91ca\u653e\u7a7a\u95f4\u3002\u9ed8\u8ba4\u662f<strong>\u6309\u7167\u8fc7\u671f\u65f6\u95f4<\/strong>\u7684,\u5982\u679cset\u65f6\u5019\u6ca1\u6709\u52a0\u4e0a\u8fc7\u671f\u65f6\u95f4\u5c31\u4f1a\u5bfc\u81f4\u6570\u636e\u5199\u6ee1maxmemory\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u8bbe\u7f6emaxmemory\u6216\u8005\u8bbe\u7f6e\u4e3a0\uff0c64\u4f4d\u7cfb\u7edf\u4e0d\u9650\u5236\u5185\u5b58\uff0c32\u4f4d\u7cfb\u7edf\u6700\u591a\u4f7f\u75283GB\u5185\u5b58\u3002<\/p>\n<p>LRU\u662fLeast Recently Used \u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7b97\u6cd5\u3002<\/p>\n<ul>\n<li>volatile-lru -&gt; \u6839\u636eLRU\u7b97\u6cd5\u751f\u6210\u7684\u8fc7\u671f\u65f6\u95f4\u6765\u5220\u9664<\/li>\n<li>allkeys-lru -&gt; \u6839\u636eLRU\u7b97\u6cd5\u5220\u9664\u4efb\u4f55key<\/li>\n<li>volatile-random -&gt; \u6839\u636e\u8fc7\u671f\u8bbe\u7f6e\u6765\u968f\u673a\u5220\u9664key<\/li>\n<li>allkeys-&gt;random -&gt; \u65e0\u5dee\u522b\u968f\u673a\u5220<\/li>\n<li>volatile-ttl -&gt; \u6839\u636e\u6700\u8fd1\u8fc7\u671f\u65f6\u95f4\u6765\u5220\u9664\uff08\u8f85\u4ee5TTL\uff09<\/li>\n<li>noeviction -&gt; \u8c01\u4e5f\u4e0d\u5220\uff0c\u76f4\u63a5\u5728\u5199\u64cd\u4f5c\u65f6\u8fd4\u56de\u9519\u8bef \u5982\u679c\u8bbe\u7f6e\u4e86maxmemory\uff0c\u4e00\u822c\u90fd\u8981\u8bbe\u7f6e\u8fc7\u671f\u7b56\u7565\u3002\u6253\u5f00Redis\u7684\u914d\u7f6e\u6587\u4ef6\u6709\u5982\u4e0b\u63cf\u8ff0\uff0cRedis\u6709\u516d\u79cd\u8fc7\u671f\u7b56\u7565\uff1a<\/li>\n<\/ul>\n<pre># volatile-lru -&gt; remove the key with an expire set using an LRU algorithm\n# allkeys-lru -&gt; remove any key accordingly to the LRU algorithm\n# volatile-random -&gt; remove a random key 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 expire at all, just return an error on write operations<\/pre>\n<p>\u90a3\u4e48\u6253\u5f00\u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0\u5982\u4e0b\u4e00\u884c\uff0c\u4f7f\u7528volatile-lru\u7684\u8fc7\u671f\u7b56\u7565\uff1a<\/p>\n<pre>maxmemory-policy volatile-lru<\/pre>\n<p><strong>\u4fdd\u5b58\u6587\u4ef6\u9000\u51fa\uff0c\u91cd\u542fredis\u670d\u52a1\u3002<\/strong><\/p>\n<h2>\u4f7f\u7528info\u547d\u4ee4\u67e5\u770bRedis\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/h2>\n<p>\u5982\u670d\u52a1\u5668Redis\u6240\u5728\u76ee\u5f55\uff1a\/usr\/local\/openresty\/lualib\/redis\/src<\/p>\n<p>\u5728\u7ec8\u7aef\u8f93\u5165.\/redis-cli\uff0c\u6253\u5f00Redis\u5ba2\u6237\u7aef\uff0c\u8f93\u5165info\u547d\u4ee4\u3002<\/p>\n<p>\u51fa\u6765\u5982\u4e0b\u4fe1\u606f\uff1a<\/p>\n<pre>[root@iZ94r80gdghZ src]# .\/redis-cli\n127.0.0.1:6379&gt; info\n# Server\nredis_version:3.0.7\nredis_git_sha1:00000000\nredis_git_dirty:0\nredis_build_id:f07a42660a61a05e\nredis_mode:standalone\nos:Linux 3.10.0-327.10.1.el7.x86_64 x86_64\narch_bits:64\nmultiplexing_api:epoll\ngcc_version:4.8.5\nprocess_id:2165\nrun_id:8ec8a8dc969d6e2f2867d9188ccb90850bfc9acb\ntcp_port:6379\nuptime_in_seconds:668\nuptime_in_days:0\nhz:10\nlru_clock:15882419\nconfig_file:\/etc\/redis\/6379.conf\n\n# Clients\nconnected_clients:1\nclient_longest_output_list:0\nclient_biggest_input_buf:0\nblocked_clients:0\n\n# Memory\nused_memory:816232\nused_memory_human:797.10K\nused_memory_rss:7655424\nused_memory_peak:816232\nused_memory_peak_human:797.10K\nused_memory_lua:36864\nmem_fragmentation_ratio:9.38\nmem_allocator:jemalloc-3.6.0\n\n# Persistence\nloading:0\nrdb_changes_since_last_save:0\nrdb_bgsave_in_progress:0\nrdb_last_save_time:1458722327\nrdb_last_bgsave_status:ok\nrdb_last_bgsave_time_sec:-1\nrdb_current_bgsave_time_sec:-1\naof_enabled:0\naof_rewrite_in_progress:0\naof_rewrite_scheduled:0\naof_last_rewrite_time_sec:-1\naof_current_rewrite_time_sec:-1\naof_last_bgrewrite_status:ok\naof_last_write_status:ok\n\n# Stats\ntotal_connections_received:1\ntotal_commands_processed:0\ninstantaneous_ops_per_sec:0\ntotal_net_input_bytes:14\ntotal_net_output_bytes:0\ninstantaneous_input_kbps:0.00\ninstantaneous_output_kbps:0.00\nrejected_connections:0\nsync_full:0\nsync_partial_ok:0\nsync_partial_err:0\nexpired_keys:0\nevicted_keys:0\nkeyspace_hits:0\nkeyspace_misses:0\npubsub_channels:0\npubsub_patterns:0\nlatest_fork_usec:0\nmigrate_cached_sockets:0\n\n# Replication\nrole:master\nconnected_slaves:0\nmaster_repl_offset:0\nrepl_backlog_active:0\nrepl_backlog_size:1048576\nrepl_backlog_first_byte_offset:0\nrepl_backlog_histlen:0\n\n# CPU\nused_cpu_sys:0.30\nused_cpu_user:0.29\nused_cpu_sys_children:0.00\nused_cpu_user_children:0.00\n\n# Cluster\ncluster_enabled:0\n\n# Keyspace\ndb0:keys=1,expires=1,avg_ttl=425280<\/pre>\n<p>\u5176\u4e2dused_memory:816232\uff0c\u4ec5\u7528\u4e860.7M\u5de6\u53f3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u7684\u5b9e\u73b0&#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-201327","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201327","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=201327"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201327\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}