{"id":190414,"date":"2024-11-12T12:23:20","date_gmt":"2024-11-12T04:23:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/190414\/"},"modified":"2024-11-12T12:23:21","modified_gmt":"2024-11-12T04:23:21","slug":"%e7%82%baweb%e6%87%89%e7%94%a8%e6%8f%90%e9%80%9ftp%e6%a1%86%e6%9e%b6%e8%aa%bf%e7%94%a8redis%e5%bf%ab%e5%8f%96%ef%bc%88tp%e8%aa%bf%e7%94%a8redis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/190414\/","title":{"rendered":"\u70baWeb\u61c9\u7528\u63d0\u901fTP\u6846\u67b6\u8abf\u7528Redis\u5feb\u53d6\uff08tp\u8abf\u7528redis\uff09"},"content":{"rendered":"<h1 id=\"%e7%82%baweb%e6%87%89%e7%94%a8%e6%8f%90%e9%80%9ftp%e6%a1%86%e6%9e%b6%e8%aa%bf%e7%94%a8redis%e5%bf%ab%e5%8f%96%ef%bc%88tp%e8%aa%bf%e7%94%a8redis%ef%bc%89-oUPPmTCqeG\">\u70baWeb\u61c9\u7528\u63d0\u901fTP\u6846\u67b6\u8abf\u7528Redis\u5feb\u53d6\uff08tp\u8abf\u7528redis\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u901f\u5ea6\u548c\u6548\u80fd\u662f\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u7684\u95dc\u9375\u56e0\u7d20\u3002\u96a8\u8457Web\u61c9\u7528\u7684\u65e5\u76ca\u589e\u9577\uff0c\u958b\u767c\u8005\u5011\u9700\u8981\u5c0b\u627e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u63d0\u5347\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u548c\u8655\u7406\u80fd\u529b\u3002TP\u6846\u67b6\uff08ThinkPHP\uff09\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u512a\u5316Web\u61c9\u7528\u7684\u6027\u80fd\uff0c\u5176\u4e2d\u4f7f\u7528Redis\u5feb\u53d6\u662f\u4e00\u500b\u975e\u5e38\u6709\u6548\u7684\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-oUPPmTCqeG\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u901a\u5e38\u7528\u4f5c\u6578\u64da\u5feb\u53d6\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\u5177\u6709\u6301\u4e45\u5316\u529f\u80fd\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\u4e2d\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%bd%9c%e7%82%ba%e5%bf%ab%e5%8f%96%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%ef%bc%9f-oUPPmTCqeG\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f5c\u70ba\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u80fd\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u5728\u5fae\u79d2\u7d1a\u5225\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70baWeb\u61c9\u7528\u7684\u7406\u60f3\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u4e0d\u50c5\u652f\u6301\u7c21\u55ae\u7684\u9375\u503c\u5c0d\uff0c\u9084\u652f\u6301\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u9748\u6d3b\u5730\u5b58\u5132\u548c\u6aa2\u7d22\u6578\u64da\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u9078\u9805\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6301\u4e45\u5316\u7b56\u7565\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9c%a8tp%e6%a1%86%e6%9e%b6%e4%b8%ad%e4%bd%bf%e7%94%a8redis%e5%bf%ab%e5%8f%96-oUPPmTCqeG\">\u5728TP\u6846\u67b6\u4e2d\u4f7f\u7528Redis\u5feb\u53d6<\/h2>\n<p>\u5728TP\u6846\u67b6\u4e2d\u96c6\u6210Redis\u5feb\u53d6\u76f8\u5c0d\u7c21\u55ae\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88ddRedis\u64f4\u5c55\uff0c\u7136\u5f8c\u5728TP\u6846\u67b6\u4e2d\u914d\u7f6eRedis\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis%e6%93%b4%e5%b1%95-oUPPmTCqeG\">1. \u5b89\u88ddRedis\u64f4\u5c55<\/h3>\n<p>\u5728PHP\u4e2d\u4f7f\u7528Redis\uff0c\u60a8\u9700\u8981\u5b89\u88dd\u76f8\u61c9\u7684\u64f4\u5c55\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pecl install redis<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%aetp%e6%a1%86%e6%9e%b6%e4%b8%ad%e7%9a%84redis-oUPPmTCqeG\">2. \u914d\u7f6eTP\u6846\u67b6\u4e2d\u7684Redis<\/h3>\n<p>\u5728TP\u6846\u67b6\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u60a8\u9700\u8981\u6dfb\u52a0Redis\u7684\u9023\u63a5\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\u914d\u7f6e\uff1a<\/p>\n<pre><code>\nreturn [\n    'cache' =&gt; [\n        'type' =&gt; 'redis',\n        'host' =&gt; '127.0.0.1',\n        'port' =&gt; 6379,\n        'password' =&gt; '',\n        'timeout' =&gt; 0,\n    ],\n];\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8redis%e5%bf%ab%e5%8f%96%e6%95%b8%e6%93%9a-oUPPmTCqeG\">3. \u4f7f\u7528Redis\u5feb\u53d6\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u914d\u7f6e\u5b8c\u6210\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728TP\u6846\u67b6\u4e2d\u4f7f\u7528Redis\u9032\u884c\u6578\u64da\u5feb\u53d6\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Redis\u5feb\u53d6\u7528\u6236\u4fe1\u606f\uff1a<\/p>\n<pre><code>\nuse thinkCache;\n\n\/\/ \u5617\u8a66\u5f9e\u5feb\u53d6\u4e2d\u7372\u53d6\u7528\u6236\u4fe1\u606f\n$user = Cache::get('user_1');\n\nif (!$user) {\n    \/\/ \u5982\u679c\u5feb\u53d6\u4e2d\u6c92\u6709\uff0c\u5247\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\n    $user = Db::table('users')-&gt;find(1);\n    \n    \/\/ \u5c07\u7528\u6236\u4fe1\u606f\u5b58\u5165\u5feb\u53d6\n    Cache::set('user_1', $user, 3600); \/\/ \u8a2d\u7f6e\u5feb\u53d6\u6709\u6548\u671f\u70ba1\u5c0f\u6642\n}\n\n\/\/ \u4f7f\u7528\u7528\u6236\u4fe1\u606f\necho $user['name'];\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-oUPPmTCqeG\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Redis\u5feb\u53d6\u53ef\u4ee5\u986f\u8457\u63d0\u5347TP\u6846\u67b6Web\u61c9\u7528\u7684\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u5834\u666f\u4e0b\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u548c\u4f7f\u7528Redis\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u8ca0\u64d4\uff0c\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002\u96a8\u8457Web\u61c9\u7528\u7684\u767c\u5c55\uff0c\u9078\u64c7\u5408\u9069\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\u5c07\u6210\u70ba\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u7684\u95dc\u9375\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u63d0\u5347\u60a8\u7684Web\u61c9\u7528\u6027\u80fd\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0c\u4e26\u5229\u7528Redis\u5feb\u53d6\u4f86\u512a\u5316\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u901a\u904eTP\u6846\u67b6\u9ad8\u6548\u8abf\u7528Redis\u5feb\u53d6\uff0c\u63d0\u5347Web\u61c9\u7528\u6027\u80fd\uff0c\u5be6\u73fe\u5feb\u901f\u6578\u64da\u5b58\u53d6\u8207\u97ff\u61c9\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-190414","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190414","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=190414"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190414\/revisions"}],"predecessor-version":[{"id":190415,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190414\/revisions\/190415"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=190414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=190414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=190414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}