{"id":26013,"date":"2024-11-24T15:04:45","date_gmt":"2024-11-24T07:04:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26013\/"},"modified":"2024-11-24T15:04:45","modified_gmt":"2024-11-24T07:04:45","slug":"%e4%bd%bf%e7%94%a8-lithe-%e7%bc%93%e5%ad%98%e6%8f%90%e9%ab%98-php-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e7%9a%84%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26013\/","title":{"rendered":"\u4f7f\u7528 Lithe \u7f13\u5b58\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077511666385.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Lithe \u7f13\u5b58\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u63d2\u56fe\" alt=\"\u4f7f\u7528 Lithe \u7f13\u5b58\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u4f60\u597d\uff0c\u793e\u533a\uff01\u4eca\u5929\uff0c\u6211\u60f3\u548c\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528<strong>lithe cache<\/strong>\uff0c\u4e00\u4e2a\u7b80\u5355\u9ad8\u6548\u7684\u5229\u7528\u6587\u4ef6\u7cfb\u7edf\u7684\u7f13\u5b58\u6a21\u5757\u3002\u5bf9\u4e8e\u4efb\u4f55\u5e0c\u671b\u63d0\u9ad8 php \u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u4eba\u6765\u8bf4\uff0clithe cache \u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u8ba9\u60a8\u53ef\u4ee5\u5feb\u901f\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u8bbe\u7f6e\u5e76\u5728\u60a8\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f lithe \u7f13\u5b58\uff1f <\/h2>\n<p><strong>lithe cache<\/strong>\u662f\u4e00\u4e2a\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u7684\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u907f\u514d\u91cd\u590d\u7684\u6570\u636e\u5e93\u67e5\u8be2\u6216\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u6765\u5e2e\u52a9\u51cf\u5c11\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65f6\u95f4\u3002\u5b83\u5c06\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e0a\u7684\u6587\u4ef6\u4e2d\uff0c\u4f7f\u5176\u6613\u4e8e\u4f7f\u7528\u548c\u5b9e\u73b0\u3002<\/p>\n<h2> \u5b89\u88c5 lithe \u7f13\u5b58 <\/h2>\n<p>\u8981\u5b89\u88c5lithemod\/cache\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer require lithemod\/cache\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u4f7f\u7528\u7075\u6d3b\u7684\u7f13\u5b58 <\/h2>\n<p>\u5b89\u88c5\u540e\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6e\u548c\u4f7f\u7528lithe cache\uff1a<\/p>\n<h3> 1.\u914d\u7f6e\u7f13\u5b58\u76ee\u5f55 <\/h3>\n<p>\u5728\u4f7f\u7528\u7f13\u5b58\u4e4b\u524d\uff0c\u9700\u8981\u5b9a\u4e49\u7f13\u5b58\u6570\u636e\u7684\u5b58\u50a8\u76ee\u5f55\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 cache \u7c7b\u7684 dir \u65b9\u6cd5\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\uff1a<\/p>\n<pre>use lithesupportcache;\n\n\/\/ define the cache directory\ncache::dir(__dir__ . '\/cache');\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 2. \u5728\u7f13\u5b58\u4e2d\u5b58\u50a8\u6570\u636e <\/h3>\n<p>\u8981\u5b58\u50a8\u6570\u636e\uff0c\u8bf7\u4f7f\u7528 add \u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u6307\u5b9a\u5bc6\u94a5\u3001\u8981\u5b58\u50a8\u7684\u6570\u636e\u3001\u8fc7\u671f\u65f6\u95f4\u4ee5\u53ca\u8981\u4f7f\u7528\u7684\u5e8f\u5217\u5316\u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ add data to the cache\ncache::add('my_data', ['foo' =&gt; 'bar'], 3600, 'serialize'); \/\/ using serialize\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 3. \u4ece\u7f13\u5b58\u4e2d\u68c0\u7d22\u6570\u636e <\/h3>\n<p>\u8981\u68c0\u7d22\u5b58\u50a8\u7684\u6570\u636e\uff0c\u8bf7\u4f7f\u7528 get \u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ retrieve data from the cache\n$data = cache::get('my_data');\n\nif ($data === null) {\n    echo \"data not found or expired.\";\n} else {\n    print_r($data);\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 4. \u68c0\u67e5\u7f13\u5b58\u4e2d\u6570\u636e\u662f\u5426\u5b58\u5728 <\/h3>\n<p>\u8981\u68c0\u67e5\u7f13\u5b58\u6761\u76ee\u662f\u5426\u5b58\u5728\u4e14\u6709\u6548\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 has \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u73b0\u5728\u63a5\u53d7\u5355\u4e2a\u952e\u548c\u952e\u6570\u7ec4\uff1a<\/p>\n<pre>\/\/ check if a single key exists\nif (cache::has('my_data')) {\n    echo \"data is in the cache.\";\n}\n\n\/\/ check multiple keys\nif (cache::has(['key1', 'key2'])) {\n    echo \"all keys are in the cache.\";\n} else {\n    echo \"one or more keys were not found or are expired.\";\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 5. \u4f7f\u7f13\u5b58\u6570\u636e\u5931\u6548 <\/h3>\n<p>\u5982\u679c\u9700\u8981\u4ece\u7f13\u5b58\u4e2d\u5220\u9664\u6570\u636e\uff0c\u8bf7\u4f7f\u7528invalidate\u65b9\u6cd5\u3002\u60a8\u73b0\u5728\u53ef\u4ee5\u4f7f\u5355\u4e2a\u952e\u6216\u952e\u6570\u7ec4\u65e0\u6548\uff1a<\/p>\n<pre>\/\/ invalidate a single cache key\ncache::invalidate('my_data');\n\n\/\/ invalidate multiple keys\ncache::invalidate(['key1', 'key2', 'key3']);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> 6.\u4f7f\u7528\u8bb0\u4f4f <\/h3>\n<p>remember \u65b9\u6cd5\u5141\u8bb8\u60a8\u4ece\u7f13\u5b58\u4e2d\u68c0\u7d22\u6570\u636e\uff0c\u6216\u8005\u5982\u679c\u5728\u7f13\u5b58\u4e2d\u627e\u4e0d\u5230\u65b0\u6570\u636e\uff0c\u5219\u6267\u884c\u56de\u8c03\u4ee5\u83b7\u53d6\u65b0\u6570\u636e\uff1a<\/p>\n<pre>$data = Cache::remember('my_key', function () {\n    \/\/ Logic to fetch data if not in cache\n    return ['foo' =&gt; 'bar'];\n}, 3600, 'serialize'); \/\/ Using serialize\n\nprint_r($data);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u6700\u540e\u7684\u8003\u8651\u56e0\u7d20 <\/h2>\n<ul>\n<li> <strong>\u6743\u9650<\/strong>\uff1a\u786e\u4fdd\u7f13\u5b58\u76ee\u5f55\u5177\u6709\u9002\u5f53\u7684\u5199\u5165\u6743\u9650\uff0c\u4ee5\u907f\u514d\u8bbf\u95ee\u95ee\u9898\u3002<\/li>\n<li> <strong>\u5e8f\u5217\u5316\u65b9\u6cd5<\/strong>\uff1alithe cache \u540c\u65f6\u652f\u6301\u5e8f\u5217\u5316\u548c json\uff0c\u7528\u4e8e\u5728\u5b58\u50a8\u6570\u636e\u4e4b\u524d\u5e8f\u5217\u5316\u6570\u636e\u3002\u9009\u62e9\u6700\u9002\u5408\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u6c42\u7684\u65b9\u6cd5\u3002<\/li>\n<li> <strong>\u76ee\u5f55\u7ed3\u6784<\/strong>\uff1alithe cache \u5c06\u7f13\u5b58\u6587\u4ef6\u7ec4\u7ec7\u5230\u5b50\u76ee\u5f55\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u5728\u5927\u578b\u76ee\u5f55\u4e2d\u8fdb\u884c\u641c\u7d22\u5e76\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528 <strong>lithe cache<\/strong>\uff0c\u60a8\u5c06\u62e5\u6709\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u7f13\u5b58\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u96c6\u6210\u5230\u5404\u79cd php \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ece\u800c\u63d0\u4f9b\u6539\u8fdb\u7684\u6027\u80fd\u548c\u66f4\u6d41\u7545\u7684\u7528\u6237\u4f53\u9a8c\u3002\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u770b\u770b\u7f13\u5b58\u53ef\u4ee5\u5728\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e26\u6765\u4ec0\u4e48\u4e0d\u540c\uff01<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Lithe \u7f13\u5b58\u63d0\u9ad8 PHP \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\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>\u4f60\u597d\uff0c\u793e\u533a\uff01\u4eca\u5929\uff0c\u6211\u60f3\u548c\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528lithe cache\uff0c\u4e00\u4e2a\u7b80\u5355\u9ad8\u6548\u7684\u5229\u7528\u6587\u4ef6\u7cfb\u7edf\u7684\u7f13\u5b58\u6a21\u5757\u3002\u5bf9\u4e8e\u4efb\u4f55\u5e0c\u671b\u63d0\u9ad8 php \u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u4eba\u6765\u8bf4\uff0clithe cache \u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u8ba9\u60a8\u53ef\u4ee5\u5feb\u901f\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u3002\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u8bbe\u7f6e\u5e76\u5728\u60a8\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u5b83\u3002 \u4ec0\u4e48\u662f lithe \u7f13\u5b58\uff1f lithe cache\u662f\u4e00\u4e2a\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u7684\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u907f\u514d\u91cd\u590d\u7684\u6570\u636e\u5e93\u67e5\u8be2\u6216\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u6765\u5e2e\u52a9\u51cf\u5c11\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65f6\u95f4\u3002\u5b83\u5c06\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e0a\u7684\u6587\u4ef6\u4e2d\uff0c\u4f7f\u5176\u6613\u4e8e\u4f7f\u7528\u548c\u5b9e\u73b0\u3002 \u5b89\u88c5 lithe \u7f13\u5b58 \u8981\u5b89\u88c5lithemod\/cache\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer require lithemod\/cache \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u7075\u6d3b\u7684\u7f13\u5b58 \u5b89\u88c5\u540e\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6e\u548c\u4f7f\u7528lithe cache\uff1a 1.\u914d\u7f6e\u7f13\u5b58\u76ee\u5f55 \u5728\u4f7f\u7528\u7f13\u5b58\u4e4b\u524d\uff0c\u9700\u8981\u5b9a\u4e49\u7f13\u5b58\u6570\u636e\u7684\u5b58\u50a8\u76ee\u5f55\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 cache \u7c7b\u7684 dir \u65b9\u6cd5\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\uff1a use lithesupportcache; \/\/ define the cache directory cache::dir(__dir__ . &#8216;\/cache&#8217;); \u767b\u5f55\u540e\u590d\u5236 2. \u5728\u7f13\u5b58\u4e2d\u5b58\u50a8\u6570\u636e \u8981\u5b58\u50a8\u6570\u636e\uff0c\u8bf7\u4f7f\u7528 add \u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u6307\u5b9a\u5bc6\u94a5\u3001\u8981\u5b58\u50a8\u7684\u6570\u636e\u3001\u8fc7\u671f\u65f6\u95f4\u4ee5\u53ca\u8981\u4f7f\u7528\u7684\u5e8f\u5217\u5316\u65b9\u6cd5\uff1a \/\/ add data to the cache cache::add(&#8216;my_data&#8217;, [&#8216;foo&#8217; =&gt; &#8216;bar&#8217;], 3600, [&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-26013","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26013","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=26013"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26013\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}