{"id":118596,"date":"2024-10-24T22:14:38","date_gmt":"2024-10-24T14:14:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/118596\/"},"modified":"2024-10-24T22:14:39","modified_gmt":"2024-10-24T14:14:39","slug":"redis-%e5%bf%ab%e5%8f%96%e6%b7%98%e6%b1%b0%e7%ad%96%e7%95%a5%e5%8f%8a%e5%85%b6%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/118596\/","title":{"rendered":"Redis \u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\u53ca\u5176\u5de5\u4f5c\u539f\u7406"},"content":{"rendered":"<h1 id=\"redis-%e5%bf%ab%e5%8f%96%e6%b7%98%e6%b1%b0%e7%ad%96%e7%95%a5%e5%8f%8a%e5%85%b6%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86-SircpJWqNr\">Redis \u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\u53ca\u5176\u5de5\u4f5c\u539f\u7406<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u5feb\u53d6\u6280\u8853\u5df2\u6210\u70ba\u63d0\u5347\u6027\u80fd\u7684\u91cd\u8981\u624b\u6bb5\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5feb\u53d6\u6578\u64da\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Redis \u7684\u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\u53ca\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7ba1\u7406\u5feb\u53d6\u6578\u64da\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%bf%ab%e5%8f%96%e6%b7%98%e6%b1%b0%e7%ad%96%e7%95%a5%ef%bc%9f-SircpJWqNr\">\u4ec0\u9ebc\u662f\u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\uff1f<\/h2>\n<p>\u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\u662f\u6307\u5728\u5feb\u53d6\u7a7a\u9593\u4e0d\u8db3\u6642\uff0c\u6c7a\u5b9a\u54ea\u4e9b\u6578\u64da\u61c9\u8a72\u88ab\u79fb\u9664\u7684\u898f\u5247\u3002Redis \u63d0\u4f9b\u4e86\u591a\u7a2e\u6dd8\u6c70\u7b56\u7565\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u61c9\u7528\u5834\u666f\u7684\u9700\u6c42\u3002\u9019\u4e9b\u7b56\u7565\u4e3b\u8981\u5206\u70ba\u4ee5\u4e0b\u5e7e\u985e\uff1a<\/p>\n<ul>\n<li><strong>LRU\uff08Least Recently Used\uff09<\/strong>\uff1a\u79fb\u9664\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6578\u64da\u3002<\/li>\n<li><strong>LFU\uff08Least Frequently Used\uff09<\/strong>\uff1a\u79fb\u9664\u4f7f\u7528\u983b\u7387\u6700\u4f4e\u7684\u6578\u64da\u3002<\/li>\n<li><strong>TTL\uff08Time To Live\uff09<\/strong>\uff1a\u6839\u64da\u8a2d\u7f6e\u7684\u904e\u671f\u6642\u9593\u81ea\u52d5\u79fb\u9664\u6578\u64da\u3002<\/li>\n<li><strong>Random<\/strong>\uff1a\u96a8\u6a5f\u79fb\u9664\u4e00\u500b\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"redis-%e7%9a%84%e6%b7%98%e6%b1%b0%e7%ad%96%e7%95%a5%e8%a9%b3%e8%a7%a3-SircpJWqNr\">Redis \u7684\u6dd8\u6c70\u7b56\u7565\u8a73\u89e3<\/h2>\n<h3 id=\"1-lru%ef%bc%88least-recently-used%ef%bc%89-SircpJWqNr\">1. LRU\uff08Least Recently Used\uff09<\/h3>\n<p>LRU \u7b56\u7565\u6703\u8ffd\u8e64\u6bcf\u500b\u9375\u7684\u4f7f\u7528\u6642\u9593\uff0c\u7576\u5feb\u53d6\u7a7a\u9593\u4e0d\u8db3\u6642\uff0c\u6703\u512a\u5148\u79fb\u9664\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u9375\u3002\u9019\u7a2e\u7b56\u7565\u9069\u5408\u65bc\u90a3\u4e9b\u4f7f\u7528\u6a21\u5f0f\u96c6\u4e2d\u5728\u6700\u8fd1\u7684\u6578\u64da\u4e0a\u3002<\/p>\n<pre><code>\n# \u8a2d\u7f6e Redis \u4f7f\u7528 LRU \u7b56\u7565\nCONFIG SET maxmemory-policy allkeys-lru\n<\/code><\/pre>\n<h3 id=\"2-lfu%ef%bc%88least-frequently-used%ef%bc%89-SircpJWqNr\">2. LFU\uff08Least Frequently Used\uff09<\/h3>\n<p>LFU \u7b56\u7565\u5247\u662f\u6839\u64da\u6bcf\u500b\u9375\u7684\u4f7f\u7528\u983b\u7387\u4f86\u6c7a\u5b9a\u6dd8\u6c70\u7684\u5c0d\u8c61\u3002\u9019\u7a2e\u7b56\u7565\u9069\u5408\u65bc\u90a3\u4e9b\u4f7f\u7528\u983b\u7387\u4e0d\u5747\u7684\u5834\u666f\uff0c\u80fd\u5920\u6709\u6548\u4fdd\u7559\u7d93\u5e38\u88ab\u8a2a\u554f\u7684\u6578\u64da\u3002<\/p>\n<pre><code>\n# \u8a2d\u7f6e Redis \u4f7f\u7528 LFU \u7b56\u7565\nCONFIG SET maxmemory-policy allkeys-lfu\n<\/code><\/pre>\n<h3 id=\"3-ttl%ef%bc%88time-to-live%ef%bc%89-SircpJWqNr\">3. TTL\uff08Time To Live\uff09<\/h3>\n<p>TTL \u7b56\u7565\u5141\u8a31\u958b\u767c\u8005\u70ba\u6bcf\u500b\u9375\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff0c\u7576\u6642\u9593\u5230\u9054\u6642\uff0c\u8a72\u9375\u6703\u81ea\u52d5\u88ab\u522a\u9664\u3002\u9019\u7a2e\u7b56\u7565\u9069\u5408\u65bc\u90a3\u4e9b\u4e0d\u518d\u9700\u8981\u7684\u81e8\u6642\u6578\u64da\u3002<\/p>\n<pre><code>\n# \u8a2d\u7f6e\u9375\u7684\u904e\u671f\u6642\u9593\nEXPIRE mykey 60  # 60\u79d2\u5f8c\u904e\u671f\n<\/code><\/pre>\n<h3 id=\"4-random-SircpJWqNr\">4. Random<\/h3>\n<p>\u96a8\u6a5f\u6dd8\u6c70\u7b56\u7565\u6703\u96a8\u6a5f\u9078\u64c7\u4e00\u500b\u9375\u9032\u884c\u522a\u9664\uff0c\u9019\u7a2e\u7b56\u7565\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u53ef\u4ee5\u7c21\u5316\u7ba1\u7406\uff0c\u4f46\u4e0d\u4e00\u5b9a\u80fd\u5920\u6709\u6548\u4fdd\u7559\u91cd\u8981\u6578\u64da\u3002<\/p>\n<pre><code>\n# \u8a2d\u7f6e Redis \u4f7f\u7528\u96a8\u6a5f\u6dd8\u6c70\u7b56\u7565\nCONFIG SET maxmemory-policy volatile-random\n<\/code><\/pre>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%81%b8%e6%93%87%e5%90%88%e9%81%a9%e7%9a%84%e6%b7%98%e6%b1%b0%e7%ad%96%e7%95%a5-SircpJWqNr\">\u5982\u4f55\u9078\u64c7\u5408\u9069\u7684\u6dd8\u6c70\u7b56\u7565<\/h2>\n<p>\u9078\u64c7\u5408\u9069\u7684\u6dd8\u6c70\u7b56\u7565\u53d6\u6c7a\u65bc\u61c9\u7528\u7684\u7279\u6027\u548c\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8003\u91cf\u56e0\u7d20\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u4f7f\u7528\u6a21\u5f0f<\/strong>\uff1a\u5982\u679c\u6578\u64da\u4f7f\u7528\u96c6\u4e2d\u5728\u6700\u8fd1\u7684\u5e7e\u500b\u9375\u4e0a\uff0c\u5247 LRU \u7b56\u7565\u53ef\u80fd\u66f4\u5408\u9069\u3002<\/li>\n<li><strong>\u6578\u64da\u7684\u4f7f\u7528\u983b\u7387<\/strong>\uff1a\u5982\u679c\u67d0\u4e9b\u6578\u64da\u7d93\u5e38\u88ab\u8a2a\u554f\uff0cLFU \u7b56\u7565\u53ef\u4ee5\u5e6b\u52a9\u4fdd\u7559\u9019\u4e9b\u6578\u64da\u3002<\/li>\n<li><strong>\u6578\u64da\u7684\u6709\u6548\u671f<\/strong>\uff1a\u5982\u679c\u6578\u64da\u662f\u81e8\u6642\u7684\uff0c\u5247\u4f7f\u7528 TTL \u7b56\u7565\u6703\u66f4\u6709\u6548\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SircpJWqNr\">\u7e3d\u7d50<\/h2>\n<p>Redis \u7684\u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u6578\u64da\u7ba1\u7406\u9078\u64c7\u3002\u901a\u904e\u5408\u7406\u9078\u64c7\u548c\u914d\u7f6e\u9019\u4e9b\u7b56\u7565\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u8cc7\u6e90\u5229\u7528\u7387\u3002\u7121\u8ad6\u662f\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u9084\u662f\u5176\u4ed6\u4f3a\u670d\u5668\uff0c\u4e86\u89e3\u9019\u4e9b\u7b56\u7565\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u8a2d\u8a08\u548c\u512a\u5316\u7cfb\u7d71\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Redis\u5feb\u53d6\u6dd8\u6c70\u7b56\u7565\u53ca\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u63a2\u7d22\u5982\u4f55\u6709\u6548\u7ba1\u7406\u8a18\u61b6\u9ad4\u548c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-118596","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/118596","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=118596"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/118596\/revisions"}],"predecessor-version":[{"id":118597,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/118596\/revisions\/118597"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=118596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=118596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=118596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}