{"id":23252,"date":"2024-11-21T14:08:50","date_gmt":"2024-11-21T06:08:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23252\/"},"modified":"2024-11-21T14:08:50","modified_gmt":"2024-11-21T06:08:50","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8redis%e8%bf%9b%e8%a1%8c%e7%bc%93%e5%ad%98%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23252\/","title":{"rendered":"\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u7f51\u7ad9\u7684\u8bbf\u95ee\u91cf\u4e5f\u8d8a\u6765\u8d8a\u9ad8\uff0c\u8fd9\u7ed9\u7f51\u7ad9\u7684\u6027\u80fd\u5e26\u6765\u4e86\u5f88\u5927\u7684\u6311\u6218\u3002\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7ad9\u7684\u8bbf\u95ee\u901f\u5ea6\uff0c\u5f88\u591a\u7f51\u7ad9\u90fd\u91c7\u7528\u4e86\u7f13\u5b58\u6280\u672f\u3002\u800c\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6027\u80fd\u3001\u5185\u5b58\u5b58\u50a8\u6570\u636e\u5e93\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7f13\u5b58\u3001\u6d88\u606f\u961f\u5217\u3001\u4f4d\u7f6e\u4fe1\u606f\u7b49\u573a\u666f\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5Redis\u6269\u5c55<br \/>\u9996\u5148\uff0c\u9700\u8981\u5728php.ini\u6587\u4ef6\u4e2d\u542f\u7528Redis\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b89\u88c5Redis\u6269\u5c55\uff1a<\/p>\n<pre>pecl install redis<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u65e0\u6cd5\u6210\u529f\u5b89\u88c5\uff0c\u5219\u53ef\u4ee5\u624b\u52a8\u4e0b\u8f7dRedis\u6269\u5c55\u7684\u6e90\u7801\u8fdb\u884c\u5b89\u88c5\uff0c\u5177\u4f53\u65b9\u6cd5\u8bf7\u53c2\u8003\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728php.ini\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u884c\u914d\u7f6e\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>extension=redis.so<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u91cd\u542fPHP\u540e\u5373\u53ef\u4f7f\u7528Redis\u3002<\/p>\n<ol>\n<li>\n<p>\u914d\u7f6eRedis<br \/>\u5728ThinkPHP6\u4e2d\uff0c\u5173\u4e8eRedis\u7684\u914d\u7f6e\u90fd\u5728config\/cache.php\u6587\u4ef6\u4e2d\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fddcache.php\u6587\u4ef6\u4e2d\u7684Redis\u9a71\u52a8\u88ab\u542f\u7528\uff1a<\/p>\n<pre>'default' =&amp;gt; [\n 'type'  =&amp;gt; 'redis',\n ...\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u9700\u8981\u914d\u7f6eRedis\u7684\u8fde\u63a5\u4fe1\u606f\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>'default' =&amp;gt; [\n 'type'  =&amp;gt; 'redis',\n 'host'  =&amp;gt; '127.0.0.1',\n 'port'  =&amp;gt; 6379,\n 'password' =&amp;gt; '',\n 'select' =&amp;gt; 0,\n 'timeout' =&amp;gt; 0,\n 'expire' =&amp;gt; 0,\n 'persistent' =&amp;gt; false,\n 'prefix' =&amp;gt; '',\n],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c&#8217;host&#8217;\u8868\u793aRedis\u7684IP\u5730\u5740\u6216\u4e3b\u673a\u540d\uff0c&#8217;port&#8217;\u8868\u793aRedis\u7684\u7aef\u53e3\u53f7\uff0c&#8217;password&#8217;\u8868\u793a\u8fde\u63a5Redis\u65f6\u7684\u5bc6\u7801\uff0c&#8217;select&#8217;\u8868\u793a\u9009\u62e9\u7684\u6570\u636e\u5e93\u7f16\u53f7\uff0c&#8217;timeout&#8217;\u8868\u793a\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0c&#8217;expire&#8217;\u8868\u793a\u7f13\u5b58\u6709\u6548\u65f6\u95f4\uff0c&#8217;prefix&#8217;\u8868\u793a\u7f13\u5b58\u524d\u7f00\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c<br \/>\u5728\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\u3002\u5bf9\u4e8eThinkPHP6\u4e2d\u7684\u7f13\u5b58\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528cache\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>cache('key_name', 'value', 3600);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5219\u4f1a\u5c06&#8217;value&#8217;\u7f13\u5b58\u5230Redis\u4e2d\uff0c\u6709\u6548\u671f\u4e3a3600\u79d2\u3002\u5982\u679c\u60f3\u8981\u53d6\u51fa\u7f13\u5b58\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<pre>$value = cache('key_name');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5373\u53ef\u4eceRedis\u4e2d\u53d6\u51fa\u7f13\u5b58\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u540c\u65f6\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Redis\u63d0\u4f9b\u7684\u5176\u4ed6API\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\uff0c\u4f8b\u5982set\u3001get\u7b49\u65b9\u6cd5\u3002\u5177\u4f53\u7684\u64cd\u4f5c\u8bf7\u53c2\u8003Redis\u5b98\u7f51\u6587\u6863\u3002<\/p>\n<p>\u603b\u7ed3<br \/>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u638c\u63e1\u4e86\u5728ThinkPHP6\u4e2d\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\uff0c\u51cf\u8f7b\u670d\u52a1\u5668\u7684\u8d1f\u62c5\uff0c\u662f\u73b0\u4ee3\u7f51\u7ad9\u5fc5\u987b\u7684\u6280\u672f\u4e4b\u4e00\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\uff1f\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>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u7f51\u7ad9\u7684\u8bbf\u95ee\u91cf\u4e5f\u8d8a\u6765\u8d8a\u9ad8\uff0c\u8fd9\u7ed9\u7f51\u7ad9\u7684\u6027\u80fd\u5e26\u6765\u4e86\u5f88\u5927\u7684\u6311\u6218\u3002\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7ad9\u7684\u8bbf\u95ee\u901f\u5ea6\uff0c\u5f88\u591a\u7f51\u7ad9\u90fd\u91c7\u7528\u4e86\u7f13\u5b58\u6280\u672f\u3002\u800c\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6027\u80fd\u3001\u5185\u5b58\u5b58\u50a8\u6570\u636e\u5e93\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7f13\u5b58\u3001\u6d88\u606f\u961f\u5217\u3001\u4f4d\u7f6e\u4fe1\u606f\u7b49\u573a\u666f\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u64cd\u4f5c\u3002 \u5b89\u88c5Redis\u6269\u5c55\u9996\u5148\uff0c\u9700\u8981\u5728php.ini\u6587\u4ef6\u4e2d\u542f\u7528Redis\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b89\u88c5Redis\u6269\u5c55\uff1a pecl install redis \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u65e0\u6cd5\u6210\u529f\u5b89\u88c5\uff0c\u5219\u53ef\u4ee5\u624b\u52a8\u4e0b\u8f7dRedis\u6269\u5c55\u7684\u6e90\u7801\u8fdb\u884c\u5b89\u88c5\uff0c\u5177\u4f53\u65b9\u6cd5\u8bf7\u53c2\u8003\u5b98\u65b9\u6587\u6863\u3002 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728php.ini\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u884c\u914d\u7f6e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b extension=redis.so \u767b\u5f55\u540e\u590d\u5236 \u91cd\u542fPHP\u540e\u5373\u53ef\u4f7f\u7528Redis\u3002 \u914d\u7f6eRedis\u5728ThinkPHP6\u4e2d\uff0c\u5173\u4e8eRedis\u7684\u914d\u7f6e\u90fd\u5728config\/cache.php\u6587\u4ef6\u4e2d\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fddcache.php\u6587\u4ef6\u4e2d\u7684Redis\u9a71\u52a8\u88ab\u542f\u7528\uff1a &#8216;default&#8217; =&amp;gt; [ &#8216;type&#8217; =&amp;gt; &#8216;redis&#8217;, &#8230; ] \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u9700\u8981\u914d\u7f6eRedis\u7684\u8fde\u63a5\u4fe1\u606f\uff0c\u5982\u4e0b\uff1a &#8216;default&#8217; =&amp;gt; [ &#8216;type&#8217; =&amp;gt; &#8216;redis&#8217;, &#8216;host&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;port&#8217; =&amp;gt; 6379, &#8216;password&#8217; =&amp;gt; &#8221;, &#8216;select&#8217; =&amp;gt; 0, &#8216;timeout&#8217; =&amp;gt; 0, &#8216;expire&#8217; =&amp;gt; 0, &#8216;persistent&#8217; =&amp;gt; false, &#8216;prefix&#8217; =&amp;gt; &#8221;, ], [&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-23252","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23252","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=23252"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23252\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}