{"id":22979,"date":"2024-11-21T16:04:03","date_gmt":"2024-11-21T08:04:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22979\/"},"modified":"2024-11-21T16:04:03","modified_gmt":"2024-11-21T08:04:03","slug":"thinkphp6%e7%bc%93%e5%ad%98%e6%97%b6%e9%97%b4%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22979\/","title":{"rendered":"thinkphp6\u7f13\u5b58\u65f6\u95f4\u8bbe\u7f6e"},"content":{"rendered":"<p>thinkphp6\u662f\u4e00\u4e2a\u9ad8\u6548\u3001\u7b80\u6d01\u3001\u7075\u6d3b\u7684php\u540e\u53f0\u5f00\u53d1\u6846\u67b6\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u7f13\u5b58\u6280\u672f\u3002\u7136\u800c\uff0c\u7f13\u5b58\u7684\u6709\u6548\u671f\u9650\u4e00\u65e6\u8fc7\u671f\uff0c\u5c31\u4f1a\u5f71\u54cd\u7f51\u7ad9\u7684\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>ThinkPHP6\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u79cd\u7f13\u5b58\u9a71\u52a8\uff0c\u5305\u62ec\u6587\u4ef6\u7f13\u5b58\u3001Redis\u7f13\u5b58\u3001Memcached\u7f13\u5b58\u7b49\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4e3b\u8981\u4ecb\u7ecd\u5982\u4f55\u8bbe\u7f6e\u6587\u4ef6\u7f13\u5b58\u548cRedis\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u3002<\/p>\n<p>\u4e00\u3001\u6587\u4ef6\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u8bbe\u7f6e<\/p>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6(config\/cache.php)\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u4e0d\u540c\u7684\u7f13\u5b58\u9a71\u52a8\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u7f13\u5b58\u65f6\u95f4\u3002<\/p>\n<pre>return [\n\n    \/\/ \u9ed8\u8ba4\u7f13\u5b58\u9a71\u52a8\n    'default' =&amp;gt; env('cache.driver', 'file'),\n\n    \/\/ \u7f13\u5b58\u8fde\u63a5\u914d\u7f6e\uff08\u6839\u636e\u7f13\u5b58\u9a71\u52a8\u9009\u62e9\u914d\u7f6e\uff09\n    'stores'  =&amp;gt; [\n        'file' =&amp;gt; [\n            'driver' =&amp;gt; 'file',\n            'cache_subdir' =&amp;gt; true,\n            'prefix' =&amp;gt; '',\n            'path' =&amp;gt; env('runtime_path') . 'cache',\n            'expire' =&amp;gt; 3600, \/\/ \u9ed8\u8ba4\u7f13\u5b58\u65f6\u95f41\u5c0f\u65f6\n        ],\n        \/\/ ...\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c&#8217;expire&#8217;\u9009\u9879\u8bbe\u7f6e\u4e86\u6587\u4ef6\u7f13\u5b58\u7684\u9ed8\u8ba4\u7f13\u5b58\u65f6\u95f4\u4e3a1\u5c0f\u65f6\u3002\u5982\u679c\u9700\u8981\u8bbe\u7f6e\u4e0d\u540c\u7684\u7f13\u5b58\u65f6\u95f4\uff0c\u53ef\u4ee5\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\u8fdb\u884c\u8bbe\u7f6e\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u8bbe\u7f6e\u7f13\u5b58\u6709\u6548\u671f\u4e3a10\u5206\u949f\nCache::store('file')-&amp;gt;set('key', 'value', 600);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cset()\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e86\u7f13\u5b58\u65f6\u95f4\u4e3a600\u79d2\uff0c\u537310\u5206\u949f\u3002<\/p>\n<p>\u4e8c\u3001Redis\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u8bbe\u7f6e<\/p>\n<p>Redis\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6(config\/cache.php)\u4e2d\u8fdb\u884c\u8bbe\u7f6e\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>return [\n\n    \/\/ \u9ed8\u8ba4\u7f13\u5b58\u9a71\u52a8\n    'default' =&amp;gt; env('cache.driver', 'redis'),\n\n    \/\/ \u7f13\u5b58\u8fde\u63a5\u914d\u7f6e\uff08\u6839\u636e\u7f13\u5b58\u9a71\u52a8\u9009\u62e9\u914d\u7f6e\uff09\n    'stores'  =&amp;gt; [\n        'redis' =&amp;gt; [\n            'driver' =&amp;gt; 'redis',\n            'connection' =&amp;gt; 'cache',\n            'prefix' =&amp;gt; '',\n            'expire' =&amp;gt; 3600, \/\/ \u9ed8\u8ba4\u7f13\u5b58\u65f6\u95f41\u5c0f\u65f6\n        ],\n        \/\/ ...\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c&#8217;expire&#8217;\u9009\u9879\u8bbe\u7f6e\u4e86Redis\u7f13\u5b58\u7684\u9ed8\u8ba4\u7f13\u5b58\u65f6\u95f4\u4e3a1\u5c0f\u65f6\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\uff1a<\/p>\n<pre>\/\/ \u8bbe\u7f6e\u7f13\u5b58\u6709\u6548\u671f\u4e3a10\u5206\u949f\nCache::store('redis')-&amp;gt;set('key', 'value', 600);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cset()\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e86\u7f13\u5b58\u65f6\u95f4\u4e3a600\u79d2\uff0c\u537310\u5206\u949f\u3002<\/p>\n<p>\u4e09\u3001\u7f13\u5b58\u9a71\u52a8\u81ea\u5b9a\u4e49\u7f13\u5b58\u65f6\u95f4<\/p>\n<p>\u9664\u4e86\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u7f13\u5b58\u65f6\u95f4\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\u81ea\u5b9a\u4e49\u7f13\u5b58\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6587\u4ef6\u7f13\u5b58\u65f6\uff1a<\/p>\n<pre>\/\/ \u8bbe\u7f6e\u7f13\u5b58\u6709\u6548\u671f\u4e3a10\u5206\u949f\nCache::store('file')-&amp;gt;put('key', 'value', now()-&amp;gt;addMinutes(10));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cput()\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e86\u7f13\u5b58\u65f6\u95f4\u4e3a10\u5206\u949f\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728ThinkPHP6\u6846\u67b6\u7684\u7f13\u5b58\u64cd\u4f5c\u4e2d\uff0c\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u7f13\u5b58\u65f6\u95f4\uff0c\u4e5f\u53ef\u4ee5\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\u81ea\u5b9a\u4e49\u7f13\u5b58\u65f6\u95f4\u3002\u5408\u7406\u5730\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u7f51\u7ad9\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp6\u7f13\u5b58\u65f6\u95f4\u8bbe\u7f6e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>thinkphp6\u662f\u4e00\u4e2a\u9ad8\u6548\u3001\u7b80\u6d01\u3001\u7075\u6d3b\u7684php\u540e\u53f0\u5f00\u53d1\u6846\u67b6\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u7f13\u5b58\u6280\u672f\u3002\u7136\u800c\uff0c\u7f13\u5b58\u7684\u6709\u6548\u671f\u9650\u4e00\u65e6\u8fc7\u671f\uff0c\u5c31\u4f1a\u5f71\u54cd\u7f51\u7ad9\u7684\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002 ThinkPHP6\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u79cd\u7f13\u5b58\u9a71\u52a8\uff0c\u5305\u62ec\u6587\u4ef6\u7f13\u5b58\u3001Redis\u7f13\u5b58\u3001Memcached\u7f13\u5b58\u7b49\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4e3b\u8981\u4ecb\u7ecd\u5982\u4f55\u8bbe\u7f6e\u6587\u4ef6\u7f13\u5b58\u548cRedis\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u3002 \u4e00\u3001\u6587\u4ef6\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u8bbe\u7f6e \u5728\u914d\u7f6e\u6587\u4ef6(config\/cache.php)\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u4e0d\u540c\u7684\u7f13\u5b58\u9a71\u52a8\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u7f13\u5b58\u65f6\u95f4\u3002 return [ \/\/ \u9ed8\u8ba4\u7f13\u5b58\u9a71\u52a8 &#8216;default&#8217; =&amp;gt; env(&#8216;cache.driver&#8217;, &#8216;file&#8217;), \/\/ \u7f13\u5b58\u8fde\u63a5\u914d\u7f6e\uff08\u6839\u636e\u7f13\u5b58\u9a71\u52a8\u9009\u62e9\u914d\u7f6e\uff09 &#8216;stores&#8217; =&amp;gt; [ &#8216;file&#8217; =&amp;gt; [ &#8216;driver&#8217; =&amp;gt; &#8216;file&#8217;, &#8216;cache_subdir&#8217; =&amp;gt; true, &#8216;prefix&#8217; =&amp;gt; &#8221;, &#8216;path&#8217; =&amp;gt; env(&#8216;runtime_path&#8217;) . &#8216;cache&#8217;, &#8216;expire&#8217; =&amp;gt; 3600, \/\/ \u9ed8\u8ba4\u7f13\u5b58\u65f6\u95f41\u5c0f\u65f6 ], \/\/ &#8230; ], ]; \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c&#8217;expire&#8217;\u9009\u9879\u8bbe\u7f6e\u4e86\u6587\u4ef6\u7f13\u5b58\u7684\u9ed8\u8ba4\u7f13\u5b58\u65f6\u95f4\u4e3a1\u5c0f\u65f6\u3002\u5982\u679c\u9700\u8981\u8bbe\u7f6e\u4e0d\u540c\u7684\u7f13\u5b58\u65f6\u95f4\uff0c\u53ef\u4ee5\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\u8fdb\u884c\u8bbe\u7f6e\uff0c\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u8bbe\u7f6e\u7f13\u5b58\u6709\u6548\u671f\u4e3a10\u5206\u949f Cache::store(&#8216;file&#8217;)-&amp;gt;set(&#8216;key&#8217;, &#8216;value&#8217;, 600); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cset()\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e86\u7f13\u5b58\u65f6\u95f4\u4e3a600\u79d2\uff0c\u537310\u5206\u949f\u3002 \u4e8c\u3001Redis\u7f13\u5b58\u7684\u7f13\u5b58\u65f6\u95f4\u8bbe\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22979","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22979"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22979\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}