{"id":202356,"date":"2025-05-10T13:26:16","date_gmt":"2025-05-10T05:26:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202356\/"},"modified":"2025-05-10T13:26:16","modified_gmt":"2025-05-10T05:26:16","slug":"php%e4%bd%bf%e7%94%a8redis%e5%86%85%e5%ad%98%e4%b8%8d%e8%b6%b3%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202356\/","title":{"rendered":"php\u4f7f\u7528redis\u5185\u5b58\u4e0d\u8db3\u5982\u4f55\u89e3\u51b3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u4f7f\u7528redis\u5185\u5b58\u4e0d\u8db3\u5982\u4f55\u89e3\u51b3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u4f7f\u7528redis\u5185\u5b58\u4e0d\u8db3\u5982\u4f55\u89e3\u51b3\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<\/p>\n<blockquote>\n<p>php\u4f7f\u7528redis\u5185\u5b58\u4e0d\u8db3\u7684\u89e3\u51b3\u529e\u6cd5\uff1a1\u3001\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u8005\u547d\u4ee4\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u5927\u5c0f\u4e3a100M\uff1b2\u3001\u83b7\u53d6\u5f53\u524d\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\uff1b3\u3001\u901a\u8fc7\u201cconfig set maxmemory-policy allkeys-lru\u201d\u547d\u4ee4\u4fee\u6539\u6dd8\u6c70\u7b56\u7565\u5373\u53ef\u3002<\/p>\n<\/blockquote>\n<h4><strong>Redis\u5360\u7528\u5185\u5b58\u5927\u5c0f<\/strong><\/h4>\n<p>\u6211\u4eec\u77e5\u9053Redis\u662f\u57fa\u4e8e\u5185\u5b58\u7684key-value\u6570\u636e\u5e93\uff0c\u56e0\u4e3a\u7cfb\u7edf\u7684\u5185\u5b58\u5927\u5c0f\u6709\u9650\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u4f7f\u7528Redis\u7684\u65f6\u5019\u53ef\u4ee5\u914d\u7f6eRedis\u80fd\u4f7f\u7528\u7684\u6700\u5927\u7684\u5185\u5b58\u5927\u5c0f\u3002<\/p>\n<h4><strong>1\u3001\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e<\/strong><\/h4>\n<p>\u901a\u8fc7\u5728Redis\u5b89\u88c5\u76ee\u5f55\u4e0b\u9762\u7684redis.conf\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\u8bbe\u7f6e\u5185\u5b58\u5927\u5c0f<\/p>\n<p>\/\/\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u5927\u5c0f\u4e3a100M maxmemory 100mb<\/p>\n<blockquote><p>\n  redis\u7684\u914d\u7f6e\u6587\u4ef6\u4e0d\u4e00\u5b9a\u4f7f\u7528\u7684\u662f\u5b89\u88c5\u76ee\u5f55\u4e0b\u9762\u7684redis.conf\u6587\u4ef6\uff0c\u542f\u52a8redis\u670d\u52a1\u7684\u65f6\u5019\u662f\u53ef\u4ee5\u4f20\u4e00\u4e2a\u53c2\u6570\u6307\u5b9aredis\u7684\u914d\u7f6e\u6587\u4ef6\u7684\n<\/p><\/blockquote>\n<h4><strong>2\u3001\u901a\u8fc7\u547d\u4ee4\u4fee\u6539<\/strong><\/h4>\n<p>Redis\u652f\u6301\u8fd0\u884c\u65f6\u901a\u8fc7\u547d\u4ee4\u52a8\u6001\u4fee\u6539\u5185\u5b58\u5927\u5c0f<\/p>\n<p>\/\/\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u5927\u5c0f\u4e3a100M 127.0.0.1:6379&gt; config set maxmemory 100mb \/\/\u83b7\u53d6\u8bbe\u7f6e\u7684Redis\u80fd\u4f7f\u7528\u7684\u6700\u5927\u5185\u5b58\u5927\u5c0f 127.0.0.1:6379&gt; config get maxmemory<\/p>\n<blockquote><p>\n  \u5982\u679c\u4e0d\u8bbe\u7f6e\u6700\u5927\u5185\u5b58\u5927\u5c0f\u6216\u8005\u8bbe\u7f6e\u6700\u5927\u5185\u5b58\u5927\u5c0f\u4e3a0\uff0c\u572864\u4f4d\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u4e0d\u9650\u5236\u5185\u5b58\u5927\u5c0f\uff0c\u572832\u4f4d\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u6700\u591a\u4f7f\u75283GB\u5185\u5b58\n<\/p><\/blockquote>\n<h4><strong>Redis\u7684\u5185\u5b58\u6dd8\u6c70<\/strong><\/h4>\n<p>\u65e2\u7136\u53ef\u4ee5\u8bbe\u7f6eRedis\u6700\u5927\u5360\u7528\u5185\u5b58\u5927\u5c0f\uff0c\u90a3\u4e48\u914d\u7f6e\u7684\u5185\u5b58\u5c31\u6709\u7528\u5b8c\u7684\u65f6\u5019\u3002\u90a3\u5728\u5185\u5b58\u7528\u5b8c\u7684\u65f6\u5019\uff0c\u8fd8\u7ee7\u7eed\u5f80Redis\u91cc\u9762\u6dfb\u52a0\u6570\u636e\u4e0d\u5c31\u6ca1\u5185\u5b58\u53ef\u7528\u4e86\u5417\uff1f\u5b9e\u9645\u4e0aRedis\u5b9a\u4e49\u4e86\u51e0\u79cd\u7b56\u7565\u7528\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff1a<strong>noeviction(\u9ed8\u8ba4\u7b56\u7565)<\/strong>\uff1a\u5bf9\u4e8e\u5199\u8bf7\u6c42\u4e0d\u518d\u63d0\u4f9b\u670d\u52a1\uff0c\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\uff08DEL\u8bf7\u6c42\u548c\u90e8\u5206\u7279\u6b8a\u8bf7\u6c42\u9664\u5916\uff09<strong>allkeys-lru<\/strong>\uff1a\u4ece\u6240\u6709key\u4e2d\u4f7f\u7528LRU\u7b97\u6cd5\u8fdb\u884c\u6dd8\u6c70<strong>volatile-lru<\/strong>\uff1a\u4ece\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684key\u4e2d\u4f7f\u7528LRU\u7b97\u6cd5\u8fdb\u884c\u6dd8\u6c70<strong>allkeys-random<\/strong>\uff1a\u4ece\u6240\u6709key\u4e2d\u968f\u673a\u6dd8\u6c70\u6570\u636e<strong>volatile-random<\/strong>\uff1a\u4ece\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684key\u4e2d\u968f\u673a\u6dd8\u6c70<strong>volatile-ttl<\/strong>\uff1a\u5728\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684key\u4e2d\uff0c\u6839\u636ekey\u7684\u8fc7\u671f\u65f6\u95f4\u8fdb\u884c\u6dd8\u6c70\uff0c\u8d8a\u65e9\u8fc7\u671f\u7684\u8d8a\u4f18\u5148\u88ab\u6dd8\u6c70<\/p>\n<blockquote><p>\n  \u5f53\u4f7f\u7528<br \/>\n <strong>volatile-lru<\/strong>\u3001<br \/>\n <strong>volatile-random<\/strong>\u3001<br \/>\n <strong>volatile-ttl<\/strong>\u8fd9\u4e09\u79cd\u7b56\u7565\u65f6\uff0c\u5982\u679c\u6ca1\u6709key\u53ef\u4ee5\u88ab\u6dd8\u6c70\uff0c\u5219\u548c<br \/>\n <strong>noeviction<\/strong>\u4e00\u6837\u8fd4\u56de\u9519\u8bef\n<\/p><\/blockquote>\n<h4><strong>\u5982\u4f55\u83b7\u53d6\u53ca\u8bbe\u7f6e\u5185\u5b58\u6dd8\u6c70\u7b56\u7565<\/strong><\/h4>\n<p>\u83b7\u53d6\u5f53\u524d\u5185\u5b58\u6dd8\u6c70\u7b56\u7565\uff1a<\/p>\n<p>127.0.0.1:6379&gt; config get maxmemory-policy<\/p>\n<p>\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u6dd8\u6c70\u7b56\u7565\uff08\u4fee\u6539redis.conf\u6587\u4ef6\uff09\uff1a<\/p>\n<p>maxmemory-policy allkeys-lru<\/p>\n<p>\u901a\u8fc7\u547d\u4ee4\u4fee\u6539\u6dd8\u6c70\u7b56\u7565\uff1a<\/p>\n<p>127.0.0.1:6379&gt; config set maxmemory-policy allkeys-lru<\/p>\n<h4><strong>LRU\u7b97\u6cd5<\/strong><\/h4>\n<h4><strong>\u4ec0\u4e48\u662fLRU?<\/strong><\/h4>\n<p>\u4e0a\u9762\u8bf4\u5230\u4e86Redis\u53ef\u4f7f\u7528\u6700\u5927\u5185\u5b58\u4f7f\u7528\u5b8c\u4e86\uff0c\u662f\u53ef\u4ee5\u4f7f\u7528LRU\u7b97\u6cd5\u8fdb\u884c\u5185\u5b58\u6dd8\u6c70\u7684\uff0c\u90a3\u4e48\u4ec0\u4e48\u662fLRU\u7b97\u6cd5\u5462\uff1f<\/p>\n<blockquote><p>\n <strong>LRU(Least Recently Used)<\/strong>\uff0c\u5373\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff0c\u662f\u4e00\u79cd\u7f13\u5b58\u7f6e\u6362\u7b97\u6cd5\u3002\u5728\u4f7f\u7528\u5185\u5b58\u4f5c\u4e3a\u7f13\u5b58\u7684\u65f6\u5019\uff0c\u7f13\u5b58\u7684\u5927\u5c0f\u4e00\u822c\u662f\u56fa\u5b9a\u7684\u3002\u5f53\u7f13\u5b58\u88ab\u5360\u6ee1\uff0c\u8fd9\u4e2a\u65f6\u5019\u7ee7\u7eed\u5f80\u7f13\u5b58\u91cc\u9762\u6dfb\u52a0\u6570\u636e\uff0c\u5c31\u9700\u8981\u6dd8\u6c70\u4e00\u90e8\u5206\u8001\u7684\u6570\u636e\uff0c\u91ca\u653e\u5185\u5b58\u7a7a\u95f4\u7528\u6765\u5b58\u50a8\u65b0\u7684\u6570\u636e\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528LRU\u7b97\u6cd5\u4e86\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u5728\u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6709\u88ab\u7528\u5230\uff0c\u90a3\u4e48\u5c06\u6765\u88ab\u4f7f\u7528\u5230\u7684\u53ef\u80fd\u6027\u4e5f\u5f88\u5c0f\uff0c\u6240\u4ee5\u5c31\u53ef\u4ee5\u88ab\u6dd8\u6c70\u6389\u3002\n<\/p><\/blockquote>\n<h4><strong>LRU\u5728Redis\u4e2d\u7684\u5b9e\u73b0<\/strong><\/h4>\n<h4><strong>\u8fd1\u4f3cLRU\u7b97\u6cd5<\/strong><\/h4>\n<p>Redis\u4f7f\u7528\u7684\u662f\u8fd1\u4f3cLRU\u7b97\u6cd5\uff0c\u5b83\u8ddf\u5e38\u89c4\u7684LRU\u7b97\u6cd5\u8fd8\u4e0d\u592a\u4e00\u6837\u3002\u8fd1\u4f3cLRU\u7b97\u6cd5\u901a\u8fc7\u968f\u673a\u91c7\u6837\u6cd5\u6dd8\u6c70\u6570\u636e\uff0c\u6bcf\u6b21\u968f\u673a\u51fa5\uff08\u9ed8\u8ba4\uff09\u4e2akey\uff0c\u4ece\u91cc\u9762\u6dd8\u6c70\u6389\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684key\u3002<\/p>\n<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7maxmemory-samples\u53c2\u6570\u4fee\u6539\u91c7\u6837\u6570\u91cf\uff1a\u4f8b\uff1amaxmemory-samples 10 maxmenory-samples\u914d\u7f6e\u7684\u8d8a\u5927\uff0c\u6dd8\u6c70\u7684\u7ed3\u679c\u8d8a\u63a5\u8fd1\u4e8e\u4e25\u683c\u7684LRU\u7b97\u6cd5\n<\/p><\/blockquote>\n<p>Redis\u4e3a\u4e86\u5b9e\u73b0\u8fd1\u4f3cLRU\u7b97\u6cd5\uff0c\u7ed9\u6bcf\u4e2akey\u589e\u52a0\u4e86\u4e00\u4e2a\u989d\u5916\u589e\u52a0\u4e86\u4e00\u4e2a24bit\u7684\u5b57\u6bb5\uff0c\u7528\u6765\u5b58\u50a8\u8be5key\u6700\u540e\u4e00\u6b21\u88ab\u8bbf\u95ee\u7684\u65f6\u95f4\u3002<\/p>\n<h2>PHP\u5f00\u53d1\u73af\u5883\u642d\u5efa\u5de5\u5177\u6709\u54ea\u4e9b<\/h2>\n<p>\u4e00\u3001phpStudy\uff0c\u662f\u4e00\u4e2a\u65b0\u624b\u5165\u95e8\u6700\u5e38\u7528\u7684\u5f00\u53d1\u73af\u5883\u3002<\/p>\n<p>\u4e8c\u3001WampServer\uff0cWampServer\u4e5f\u540c\u6837\u7684\u4e5f\u662f\u548cphpStudy\u4e00\u6837\u64cd\u4f5c\u7b80\u5355\u5bf9\u5c0f\u767d\u6bd4\u8f83\u53cb\u597d\u3002<\/p>\n<p>\u4e09\u3001XAMPP\uff0cXAMPP\uff08Apache+MySQL+PHP+PERL\uff09\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5efa\u7ad9\u96c6\u6210\u8f6f\u4ef6\u5305\uff1b<\/p>\n<p>\u56db\u3001MAMP\uff0cMAMP\u5206\u4e3a\u4e24\u79cdMAMP\u548cMAMP Pro for Mac\u3002<\/p>\n<p>\u4e94\u3001\u5b9d\u5854\u9762\u677f\uff0c\u5b9d\u5854\u9762\u677f\u662f\u4e00\u6b3e\u670d\u52a1\u5668\u7ba1\u7406\u8f6f\u4ef6\uff0c\u652f\u6301windows\u548clinux\u7cfb\u7edf\u3002<\/p>\n<p>\u516d\u3001UPUPW\uff0cUPUPW\u662f\u76ee\u524dWindows\u5e73\u53f0\u4e0b\u6700\u5177\u7279\u8272\u7684Web\u670d\u52a1\u5668PHP\u5957\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u4f7f\u7528redis\u5185\u5b58\u4e0d\u8db3\u5982\u4f55&#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-202356","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202356","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=202356"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202356\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}