{"id":29499,"date":"2024-11-25T13:16:15","date_gmt":"2024-11-25T05:16:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29499\/"},"modified":"2024-11-25T13:16:15","modified_gmt":"2024-11-25T05:16:15","slug":"php%e5%be%ae%e4%bf%a1%e5%bc%80%e5%8f%91%e7%94%a8cache-%e8%a7%a3%e5%86%b3%e6%95%b0%e6%8d%ae%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29499\/","title":{"rendered":"PHP\u5fae\u4fe1\u5f00\u53d1\u7528Cache \u89e3\u51b3\u6570\u636e\u7f13\u5b58"},"content":{"rendered":"<p style=\"text-align: left\">\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\uff0c\u89e3\u51b3\u5fae\u4fe1\u5f00\u53d1\u65f6\u6570\u636e\u7684\u95ee\u9898,\u8fd9\u91cc\u7528 \u7c7b\u4e3e\u4f8b\u8bf4\u660e,\u5177\u6709\u53c2\u8003\u4ef7\u503c,\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p style=\"text-align: left\">\u7528php\u8fdb\u884c\u5fae\u4fe1\u5f00\u53d1\u65f6\uff0c\u78b0\u5230access_token\u957f\u4e45\u4fdd\u5b58\u7684\u95ee\u9898\uff0c\u4ee5\u524d\u90fd\u662f\u7528\u91cc\u7684Cache\u76f4\u63a5\u3001get\u4e00\u4e0b\u5c31\u5b8c\u4e86\u3002\u73b0\u5728\u6ca1\u6846\u67b6\u53ef\u7528\u4e86\uff0c\u53ea\u597d\u81ea\u5df1\u52a8\u624b\u5199\u4e00\u4e2acache\u6682\u65f6\u7528\u3002<\/p>\n<p style=\"text-align: left\">\u8fd9\u4e2aCache\u7c7b\u7528\u6765\u7f13\u5b58\u4e00\u4e9b\u5177\u6709\u65f6\u6548\u6027\u7684\u6570\u636e\uff0c\u6bd4\u5982\u5fae\u4fe1\u57fa\u7840\u7684access_token\u3001\u7f51\u9875Auth\u9a8c\u8bc1\u7684access_token\u7b49<\/p>\n<p style=\"text-align: left\"><strong>\u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u672c\u5730\u6587\u4ef6\u8fdb\u884c\u6570\u636e\u7684\u7f13\u5b58\uff0c<\/strong><\/p>\n<pre>\/\/\u6d4b\u8bd5\r\n $cache = new Cache();\r\n $cache-&gt;dir = \"..\/cc\/\";\r\n \/\/$cache-&gt;setCache(\"zhang\", \"zhangsan\", 100);\r\n echo $cache-&gt;getCache(\"zhang\");\r\n \/\/$cache-&gt;removeCache(\"zhang\");\r\n \r\n $cache-&gt;setCache(\"liu\", \"liuqi\", 100);\r\n echo $cache-&gt;getCache(\"liu\");\r\n\r\n class Cache{\r\n public $cacheFile = \"cache.json\"; \/\/\u6587\u4ef6\r\n public $dir = \".\/cach2\/\"; \/\/\u76ee\u5f55\r\n\r\n \/\/\u7f13\u5b58\r\n public function setCache($name, $val, $expires_time){\r\n $file = $this-&gt;hasFile();\r\n \/\/\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4\r\n $str = file_get_contents($file);\r\n $arr = json_decode($str, true);\r\n \r\n \/\/\u503c\u4e3a\u7a7a\uff0c\u5219\u79fb\u9664\u8be5\u7f13\u5b58\r\n if(empty($val)){\r\n unset($arr[$name]);\r\n }else{\r\n $arr[$name] = array(\"value\"=&gt;$val, \"expires_time\"=&gt;$expires_time, \"add_time\"=&gt;time());\r\n } \r\n \/\/\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32\r\n $str = json_encode($arr);\r\n file_put_contents($file, $str);\r\n }\r\n public function getCache($name){\r\n $file = $this-&gt;hasFile();\r\n \r\n \/\/\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4\r\n $allArr = json_decode($str, true);\r\n $arr = $allArr[$name];\r\n\r\n if(!$arr || time() &gt; ($arr[\"expires_time\"] + $arr[\"add_time\"])){\r\n $this-&gt;removeCache($name); \/\/\u8fc7\u671f\u79fb\u9664\r\n return false;\r\n }\r\n return $arr[\"value\"];\r\n }\r\n public function removeCache($name){\r\n $this-&gt;setCache($name, '', 0);\r\n }\r\n \r\n private function hasFile(){\r\n \/\/\u5982\u679c\u4e0d\u5b58\u5728\u7f13\u5b58\u6587\u4ef6\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\r\n if(!file_exists($this-&gt;dir)){\r\n mkdir($this-&gt;dir);\r\n }\r\n if(!file_exists($this-&gt;dir . $this-&gt;cacheFile)){\r\n touch($this-&gt;dir . $this-&gt;cacheFile);\r\n }\r\n return $this-&gt;dir . $this-&gt;cacheFile;\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4e0a\u9762\u7684Cache\u7c7b\u5171\u6709set\u3001get\u3001remove\u4e09\u79cd\u64cd\u4f5c\u3002\u53e6\u5916\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7f13\u5b58\u6587\u4ef6\u7684\u4fdd\u5b58\u8def\u5f84\uff0c\u53ea\u8981\u8bbe\u7f6eCache\u7684dir\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p style=\"text-align: left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u4ee5\u4e0a\u5c31\u662fPHP \u5fae\u4fe1\u5f00\u53d1\u65f6\u6570\u636e\u7f13\u5b58\u7684\u65b9\u6cd5\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9.<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPHP\u5fae\u4fe1\u5f00\u53d1\u7528Cache \u89e3\u51b3\u6570\u636e\u7f13\u5b58\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>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\uff0c\u89e3\u51b3\u5fae\u4fe1\u5f00\u53d1\u65f6\u6570\u636e\u7684\u95ee\u9898,\u8fd9\u91cc\u7528 \u7c7b\u4e3e\u4f8b\u8bf4\u660e,\u5177\u6709\u53c2\u8003\u4ef7\u503c,\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e0b \u7528php\u8fdb\u884c\u5fae\u4fe1\u5f00\u53d1\u65f6\uff0c\u78b0\u5230access_token\u957f\u4e45\u4fdd\u5b58\u7684\u95ee\u9898\uff0c\u4ee5\u524d\u90fd\u662f\u7528\u91cc\u7684Cache\u76f4\u63a5\u3001get\u4e00\u4e0b\u5c31\u5b8c\u4e86\u3002\u73b0\u5728\u6ca1\u6846\u67b6\u53ef\u7528\u4e86\uff0c\u53ea\u597d\u81ea\u5df1\u52a8\u624b\u5199\u4e00\u4e2acache\u6682\u65f6\u7528\u3002 \u8fd9\u4e2aCache\u7c7b\u7528\u6765\u7f13\u5b58\u4e00\u4e9b\u5177\u6709\u65f6\u6548\u6027\u7684\u6570\u636e\uff0c\u6bd4\u5982\u5fae\u4fe1\u57fa\u7840\u7684access_token\u3001\u7f51\u9875Auth\u9a8c\u8bc1\u7684access_token\u7b49 \u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u672c\u5730\u6587\u4ef6\u8fdb\u884c\u6570\u636e\u7684\u7f13\u5b58\uff0c \/\/\u6d4b\u8bd5 $cache = new Cache(); $cache-&gt;dir = &#8220;..\/cc\/&#8221;; \/\/$cache-&gt;setCache(&#8220;zhang&#8221;, &#8220;zhangsan&#8221;, 100); echo $cache-&gt;getCache(&#8220;zhang&#8221;); \/\/$cache-&gt;removeCache(&#8220;zhang&#8221;); $cache-&gt;setCache(&#8220;liu&#8221;, &#8220;liuqi&#8221;, 100); echo $cache-&gt;getCache(&#8220;liu&#8221;); class Cache{ public $cacheFile = &#8220;cache.json&#8221;; \/\/\u6587\u4ef6 public $dir = &#8220;.\/cach2\/&#8221;; \/\/\u76ee\u5f55 \/\/\u7f13\u5b58 public function setCache($name, $val, $expires_time){ $file = $this-&gt;hasFile(); \/\/\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4 $str = file_get_contents($file); $arr = json_decode($str, true); \/\/\u503c\u4e3a\u7a7a\uff0c\u5219\u79fb\u9664\u8be5\u7f13\u5b58 if(empty($val)){ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-29499","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29499","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=29499"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29499\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}