{"id":48352,"date":"2024-12-02T08:47:14","date_gmt":"2024-12-02T00:47:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48352\/"},"modified":"2024-12-02T08:47:14","modified_gmt":"2024-12-02T00:47:14","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e5%87%bd%e6%95%b0%e7%9a%84%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48352\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>\u6027\u80fd\u4f18\u5316\u65b9\u6cd5\uff1a\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u6765\u7f13\u5b58\u5916\u90e8\u51fd\u6570\u7684\u7ed3\u679c\u3002\u4f7f\u7528\u7f13\u5b58\u6765\u907f\u514d\u91cd\u590d\u7684\u5916\u90e8\u51fd\u6570\u8c03\u7528\u3002\u4f7f\u7528\u5907\u5fd8\u5f55\u6a21\u5f0f\u6765\u5b58\u50a8\u5df2\u8c03\u7528\u7684\u51fd\u6570\u53c2\u6570\u548c\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730016345671df459be45a.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u8c03\u7528\u5916\u90e8\u51fd\u6570\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\u3002\u5982\u679c\u60a8\u9700\u8981\u5728 PHP \u51fd\u6570\u4e2d\u53cd\u590d\u8c03\u7528\u5916\u90e8\u51fd\u6570\uff0c\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u65b9\u6cd5\u6765\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9759\u6001\u53d8\u91cf<\/strong><\/p>\n<p>\u9759\u6001\u53d8\u91cf\u5728\u51fd\u6570\u6267\u884c\u671f\u95f4\u4fdd\u7559\u5176\u503c\uff0c\u907f\u514d\u4e86\u6bcf\u6b21\u8c03\u7528\u65f6\u91cd\u65b0\u8ba1\u7b97\u8be5\u503c\u3002\u5982\u679c\u60a8\u5728\u51fd\u6570\u4e2d\u591a\u6b21\u8c03\u7528\u76f8\u540c\u7684\u5916\u90e8\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u6765\u5b58\u50a8\u7ed3\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre>function get_external_data() {\n    static $data;\n    if (!$data) {\n        $data = external_function();\n    }\n    return $data;\n}<\/pre>\n<p><strong>\u4f7f\u7528\u7f13\u5b58<\/strong><\/p>\n<p>\u7f13\u5b58\u53ef\u4ee5\u901a\u8fc7\u5b58\u50a8\u5148\u524d\u6267\u884c\u7684\u7ed3\u679c\u6765\u907f\u514d\u91cd\u590d\u7684\u5916\u90e8\u51fd\u6570\u8c03\u7528\u3002\u4f7f\u7528\u7f13\u5b58\u53ef\u4ee5\u663e\u7740\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u522b\u662f\u5f53\u5916\u90e8\u51fd\u6570\u6d89\u53ca\u7e41\u91cd\u7684\u8ba1\u7b97\u6216\u6570\u636e\u5e93\u67e5\u8be2\u65f6\u3002<\/p>\n<pre>function get_cached_data() {\n    $cache_key = 'external_data';\n    $data = wp_cache_get($cache_key);\n    if ($data === false) {\n        $data = external_function();\n        wp_cache_set($cache_key, $data, '', 600);\n    }\n    return $data;\n}<\/pre>\n<p><strong>\u4f7f\u7528\u5907\u5fd8\u5f55\u6a21\u5f0f<\/strong><\/p>\n<p>\u5907\u5fd8\u5f55\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u65e8\u5728\u63d0\u9ad8\u51fd\u6570\u8c03\u7528\u6027\u80fd\u3002\u5b83\u4f7f\u7528\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u5df2\u8c03\u7528\u7684\u51fd\u6570\u53c2\u6570\u548c\u7ed3\u679c\u3002\u5f53\u51fd\u6570\u518d\u6b21\u4f7f\u7528\u76f8\u540c\u7684\u53c2\u6570\u8c03\u7528\u65f6\uff0c\u5907\u5fd8\u5f55\u4f1a\u8fd4\u56de\u5b58\u50a8\u7684\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u3002<\/p>\n<pre>class Memoize {\n    private $cache = [];\n\n    public function __call($name, $args) {\n        $key = md5(serialize($args));\n        if (!isset($this-&gt;cache[$key])) {\n            $this-&gt;cache[$key] = call_user_func_array($name, $args);\n        }\n        return $this-&gt;cache[$key];\n    }\n}\n\n$memo = new Memoize();\n$data = $memo-&gt;external_function($params);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u7f13\u5b58\u6765\u4f18\u5316\u5916\u90e8\u51fd\u6570\u8c03\u7528\u7684\u6027\u80fd\uff1a<\/p>\n<pre>\/\/ \u83b7\u53d6 1000 \u7bc7\u6587\u7ae0\u7684\u6807\u9898\n$posts = get_posts(array(\n    'numberposts' =&gt; 1000,\n));\n\n\/\/ \u4f7f\u7528\u7f13\u5b58\u907f\u514d\u91cd\u590d\u8c03\u7528 get_the_title()\nforeach ($posts as $post) {\n    $title = wp_cache_get('post_title_' . $post-&gt;ID);\n    if ($title === false) {\n        $title = get_the_title($post-&gt;ID);\n        wp_cache_set('post_title_' . $post-&gt;ID, $title, '', 600);\n    }\n    \/\/ \u4f7f\u7528 $title\n}<\/pre>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u4f18\u5316\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u63d0\u9ad8 PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\uff0c\u4ece\u800c\u6539\u5584\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4f53\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5 \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300aPHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 \u6027\u80fd\u4f18\u5316\u65b9\u6cd5\uff1a\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u6765\u7f13\u5b58\u5916\u90e8\u51fd\u6570\u7684\u7ed3\u679c\u3002\u4f7f\u7528\u7f13\u5b58\u6765\u907f\u514d\u91cd\u590d\u7684\u5916\u90e8\u51fd\u6570\u8c03\u7528\u3002\u4f7f\u7528\u5907\u5fd8\u5f55\u6a21\u5f0f\u6765\u5b58\u50a8\u5df2\u8c03\u7528\u7684\u51fd\u6570\u53c2\u6570\u548c\u7ed3\u679c\u3002 PHP \u51fd\u6570\u4e2d\u8c03\u7528\u5916\u90e8\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6cd5 \u5728 PHP \u4e2d\uff0c\u8c03\u7528\u5916\u90e8\u51fd\u6570\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\u3002\u5982\u679c\u60a8\u9700\u8981\u5728 PHP \u51fd\u6570\u4e2d\u53cd\u590d\u8c03\u7528\u5916\u90e8\u51fd\u6570\uff0c\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u65b9\u6cd5\u6765\u4f18\u5316\u6027\u80fd\u3002 \u4f7f\u7528\u9759\u6001\u53d8\u91cf \u9759\u6001\u53d8\u91cf\u5728\u51fd\u6570\u6267\u884c\u671f\u95f4\u4fdd\u7559\u5176\u503c\uff0c\u907f\u514d\u4e86\u6bcf\u6b21\u8c03\u7528\u65f6\u91cd\u65b0\u8ba1\u7b97\u8be5\u503c\u3002\u5982\u679c\u60a8\u5728\u51fd\u6570\u4e2d\u591a\u6b21\u8c03\u7528\u76f8\u540c\u7684\u5916\u90e8\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u6765\u5b58\u50a8\u7ed3\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002 function get_external_data() { static $data; if (!$data) { $data = external_function(); } return $data; } \u4f7f\u7528\u7f13\u5b58 \u7f13\u5b58\u53ef\u4ee5\u901a\u8fc7\u5b58\u50a8\u5148\u524d\u6267\u884c\u7684\u7ed3\u679c\u6765\u907f\u514d\u91cd\u590d\u7684\u5916\u90e8\u51fd\u6570\u8c03\u7528\u3002\u4f7f\u7528\u7f13\u5b58\u53ef\u4ee5\u663e\u7740\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u522b\u662f\u5f53\u5916\u90e8\u51fd\u6570\u6d89\u53ca\u7e41\u91cd\u7684\u8ba1\u7b97\u6216\u6570\u636e\u5e93\u67e5\u8be2\u65f6\u3002 function get_cached_data() { $cache_key = &#8216;external_data&#8217;; $data = wp_cache_get($cache_key); if ($data === false) { $data = external_function(); wp_cache_set($cache_key, $data, &#8221;, 600); } return $data; [&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-48352","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48352","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=48352"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48352\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}