{"id":168771,"date":"2024-11-06T16:01:18","date_gmt":"2024-11-06T08:01:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168771\/"},"modified":"2024-11-06T16:01:19","modified_gmt":"2024-11-06T08:01:19","slug":"tp5-%e5%92%8c-redis-%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f-web-%e9%96%8b%e7%99%bc%ef%bc%88tp5-redis-%e5%bf%ab%e5%8f%96%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168771\/","title":{"rendered":"TP5 \u548c Redis \u5feb\u53d6\u52a0\u901f Web \u958b\u767c\uff08tp5 redis \u5feb\u53d6\uff09"},"content":{"rendered":"<h1 id=\"tp5-%e5%92%8c-redis-%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f-web-%e9%96%8b%e7%99%bc-XPARSZthFv\">TP5 \u548c Redis \u5feb\u53d6\u52a0\u901f Web \u958b\u767c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u6027\u80fd\u512a\u5316\u662f\u6bcf\u4f4d\u958b\u767c\u8005\u5fc5\u9808\u9762\u5c0d\u7684\u91cd\u8981\u8ab2\u984c\u3002\u96a8\u8457\u7528\u6236\u5c0d\u7db2\u7ad9\u901f\u5ea6\u7684\u8981\u6c42\u4e0d\u65b7\u63d0\u9ad8\uff0c\u958b\u767c\u8005\u9700\u8981\u5c0b\u627e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u97ff\u61c9\u901f\u5ea6\u3002TP5\uff08ThinkPHP 5\uff09\u4f5c\u70ba\u4e00\u6b3e\u6d41\u884c\u7684 PHP \u6846\u67b6\uff0c\u7d50\u5408 Redis \u5feb\u53d6\u6280\u8853\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8 Web \u61c9\u7528\u7684\u6027\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e TP5 \u548c Redis \u5feb\u53d6\u7684\u6574\u5408\u53ca\u5176\u5728 Web \u958b\u767c\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-tp5%ef%bc%9f-XPARSZthFv\">\u4ec0\u9ebc\u662f TP5\uff1f<\/h2>\n<p>TP5 \u662f\u4e00\u6b3e\u57fa\u65bc PHP \u7684\u958b\u6e90\u6846\u67b6\uff0c\u65e8\u5728\u7c21\u5316 Web \u61c9\u7528\u7684\u958b\u767c\u904e\u7a0b\u3002\u5b83\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u8def\u7531\u3001\u6a21\u578b\u3001\u8996\u5716\u548c\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u69cb\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u69cb\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5f0f\u3002TP5 \u7684\u8a2d\u8a08\u7406\u5ff5\u662f\u7c21\u55ae\u6613\u7528\uff0c\u4e26\u4e14\u5177\u6709\u826f\u597d\u7684\u64f4\u5c55\u6027\uff0c\u9069\u5408\u5404\u7a2e\u898f\u6a21\u7684\u9805\u76ee\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-XPARSZthFv\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis \u662f\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u7528\u65bc\u5feb\u53d6\u548c\u6578\u64da\u6301\u4e45\u5316\u3002\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u64cd\u4f5c\u547d\u4ee4\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u5167\u5b58\u5b58\u53d6\u7279\u6027\uff0cRedis \u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u9700\u8981\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u7684\u5834\u666f\uff0c\u5982 Web \u61c9\u7528\u7684\u5feb\u53d6\u5c64\u3002<\/p>\n<h2 id=\"tp5-%e8%88%87-redis-%e7%9a%84%e6%95%b4%e5%90%88-XPARSZthFv\">TP5 \u8207 Redis \u7684\u6574\u5408<\/h2>\n<p>\u5728 TP5 \u4e2d\u4f7f\u7528 Redis \u5feb\u53d6\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 TP5 \u4e2d\u6574\u5408 Redis \u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-redis-%e6%93%b4%e5%b1%95-XPARSZthFv\">1. \u5b89\u88dd Redis \u64f4\u5c55<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u7684 PHP \u74b0\u5883\u4e2d\u5df2\u5b89\u88dd Redis \u64f4\u5c55\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code>pecl install redis<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae-tp5-%e4%bd%bf%e7%94%a8-redis-XPARSZthFv\">2. \u914d\u7f6e TP5 \u4f7f\u7528 Redis<\/h3>\n<p>\u5728 TP5 \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff08\u901a\u5e38\u662f `config\/cache.php`\uff09\uff0c\u5c07\u5feb\u53d6\u9a45\u52d5\u8a2d\u7f6e\u70ba Redis\uff1a<\/p>\n<pre><code>\nreturn [\n    'type'   =&gt; 'redis',\n    'host'   =&gt; '127.0.0.1',\n    'port'   =&gt; 6379,\n    'password' =&gt; '',\n    'timeout' =&gt; 0,\n];\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e5%bf%ab%e5%8f%96%e5%8a%9f%e8%83%bd-XPARSZthFv\">3. \u4f7f\u7528\u5feb\u53d6\u529f\u80fd<\/h3>\n<p>\u5728 TP5 \u4e2d\u4f7f\u7528\u5feb\u53d6\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Redis \u5feb\u53d6\u4f86\u5b58\u5132\u548c\u7372\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>\nuse thinkCache;\n\n\/\/ \u5b58\u5132\u6578\u64da\nCache::set('key', 'value', 3600); \/\/ 3600\u79d2\u5f8c\u904e\u671f\n\n\/\/ \u7372\u53d6\u6578\u64da\n$value = Cache::get('key');\nif ($value) {\n    echo $value; \/\/ \u8f38\u51fa 'value'\n} else {\n    echo '\u6578\u64da\u5df2\u904e\u671f\u6216\u4e0d\u5b58\u5728';\n}\n<\/code><\/pre>\n<h2 id=\"redis-%e5%bf%ab%e5%8f%96%e7%9a%84%e5%84%aa%e5%8b%a2-XPARSZthFv\">Redis \u5feb\u53d6\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0cRedis \u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u6578\u64da\u5eab\u67e5\u8a62\u7684\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u6578\u64da\u5eab\u8ca0\u64d4\uff1a<\/strong>\u4f7f\u7528\u5feb\u53d6\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u964d\u4f4e\u6578\u64da\u5eab\u7684\u8ca0\u64d4\uff0c\u63d0\u5347\u6574\u9ad4\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>Redis \u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-XPARSZthFv\">\u7d50\u8ad6<\/h2>\n<p>TP5 \u548c Redis \u7684\u7d50\u5408\u70ba Web \u958b\u767c\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u6709\u6548\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002\u901a\u904e\u7c21\u55ae\u7684\u914d\u7f6e\u548c\u4f7f\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07 Redis \u5feb\u53d6\u96c6\u6210\u5230\u4ed6\u5011\u7684 TP5 \u61c9\u7528\u4e2d\uff0c\u5f9e\u800c\u5be6\u73fe\u66f4\u5feb\u7684\u6578\u64da\u5b58\u53d6\u548c\u66f4\u9ad8\u7684\u7cfb\u7d71\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684 TP5 \u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 TP5 \u548c Redis \u5feb\u53d6\u5982\u4f55\u52a0\u901f Web \u958b\u767c\uff0c\u63d0\u5347\u6027\u80fd\u8207\u6548\u7387\uff0c\u8b93\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u904b\u884c\u66f4\u6d41\u66a2\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-168771","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168771","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=168771"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168771\/revisions"}],"predecessor-version":[{"id":168772,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168771\/revisions\/168772"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}