{"id":41,"date":"2024-08-14T15:42:04","date_gmt":"2024-08-14T07:42:04","guid":{"rendered":"https:\/\/www.linhw.cn\/?p=41"},"modified":"2024-08-14T15:42:04","modified_gmt":"2024-08-14T07:42:04","slug":"%e7%ae%80%e5%8d%95-php-%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e6%b1%a0%e7%b1%bb%e8%ae%be%e8%ae%a1%ef%bc%9a","status":"publish","type":"post","link":"https:\/\/www.linhw.cn\/index.php\/2024\/08\/14\/%e7%ae%80%e5%8d%95-php-%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e6%b1%a0%e7%b1%bb%e8%ae%be%e8%ae%a1%ef%bc%9a\/","title":{"rendered":"\u7b80\u5355 PHP \u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7c7b\u8bbe\u8ba1\uff1a"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">      <code>class DatabaseConnectionPool {\n\n    private $host;\n    private $username;\n    private $password;\n    private $database;\n    private $maxConnections;\n    private $connectionPool = [];\n    private $usedConnections = [];\n\n    public function __construct($host, $username, $password, $database, $maxConnections = 10) {\n        $this-&gt;host = $host;\n        $this-&gt;username = $username;\n        $this-&gt;password = $password;\n        $this-&gt;database = $database;\n        $this-&gt;maxConnections = $maxConnections;\n\n        $this-&gt;initialize(); \/\/ \u521d\u59cb\u5316\u8fde\u63a5\u6c60\n    }\n\n    \/\/ \u521d\u59cb\u5316\u8fde\u63a5\u6c60\n    private function initialize() {\n        for ($i = 0; $i &lt; $this-&gt;maxConnections; $i++) {\n            $this-&gt;connectionPool[] = $this-&gt;createConnection();\n        }\n    }\n\n    \/\/ \u521b\u5efa\u65b0\u7684\u6570\u636e\u5e93\u8fde\u63a5\n    private function createConnection() {\n        $connection = new mysqli($this-&gt;host, $this-&gt;username, $this-&gt;password, $this-&gt;database);\n        if ($connection-&gt;connect_error) {\n            throw new Exception(\"Connection failed: \" . $connection-&gt;connect_error);\n        }\n        return $connection;\n    }\n\n    \/\/ \u4ece\u8fde\u63a5\u6c60\u83b7\u53d6\u8fde\u63a5\n    public function getConnection() {\n        if (!empty($this-&gt;connectionPool)) {\n            $connection = array_pop($this-&gt;connectionPool);\n            $this-&gt;usedConnections[] = $connection;\n            return $connection;\n        } else {\n            \/\/ \u8fde\u63a5\u6c60\u5df2\u6ee1\uff0c\u53ef\u4ee5\u7b49\u5f85\u6216\u629b\u51fa\u5f02\u5e38\n            throw new Exception(\"Connection pool is full.\"); \n        }\n    }\n\n    \/\/ \u91ca\u653e\u8fde\u63a5\uff0c\u5c06\u5176\u653e\u56de\u8fde\u63a5\u6c60\n    public function releaseConnection($connection) {\n        $key = array_search($connection, $this-&gt;usedConnections);\n        if ($key !== false) {\n            unset($this-&gt;usedConnections[$key]);\n            $this-&gt;connectionPool[] = $connection;\n        }\n    }\n\n    \/\/ \u9500\u6bc1\u8fde\u63a5\u6c60\uff0c\u5173\u95ed\u6240\u6709\u8fde\u63a5\n    public function __destruct() {\n        foreach (array_merge($this-&gt;connectionPool, $this-&gt;usedConnections) as $connection) {\n            $connection-&gt;close();\n        }\n    }\n}<\/code>\n    <\/pre>\n\n\n\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      <code>\/\/ \u521b\u5efa\u8fde\u63a5\u6c60\u5bf9\u8c61\n$pool = new DatabaseConnectionPool(\"localhost\", \"user\", \"password\", \"database\", 10);\n\n\/\/ \u83b7\u53d6\u8fde\u63a5\n$connection = $pool-&gt;getConnection();\n\n\/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c...\n\n\/\/ \u91ca\u653e\u8fde\u63a5\n$pool-&gt;releaseConnection($connection);<\/code>\n    <\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u8fde\u63a5\u6c60\u7c7b\u7ef4\u62a4\u4e24\u4e2a\u6570\u7ec4\uff1a\n<ul class=\"wp-block-list\">\n<li>$connectionPool\uff1a\u5b58\u50a8\u7a7a\u95f2\u7684\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n\n\n\n<li>$usedConnections\uff1a\u5b58\u50a8\u6b63\u5728\u4f7f\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>getConnection() \u65b9\u6cd5\u4f1a\u4ece $connectionPool \u4e2d\u83b7\u53d6\u4e00\u4e2a\u8fde\u63a5\uff0c\u5982\u679c $connectionPool \u4e3a\u7a7a\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n\n\n\n<li>releaseConnection() \u65b9\u6cd5\u5c06\u8fde\u63a5\u653e\u56de $connectionPool \u4e2d\uff0c\u4ee5\u4fbf\u5176\u4ed6\u8bf7\u6c42\u53ef\u4ee5\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>__destruct() \u65b9\u6cd5\u5728\u5bf9\u8c61\u9500\u6bc1\u65f6\u5173\u95ed\u6240\u6709\u8fde\u63a5\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u4f18\u5316\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u8fde\u63a5\u6c60\u76d1\u63a7\u3001\u8fde\u63a5\u8d85\u65f6\u5904\u7406\u3001\u8fde\u63a5\u9519\u8bef\u5904\u7406\u7b49\u529f\u80fd\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>class DatabaseConnectionPool { private $host; private $ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.linhw.cn\/index.php\/2024\/08\/14\/%e7%ae%80%e5%8d%95-php-%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e6%b1%a0%e7%b1%bb%e8%ae%be%e8%ae%a1%ef%bc%9a\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u7b80\u5355 PHP \u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7c7b\u8bbe\u8ba1\uff1a\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-9"],"_links":{"self":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions\/42"}],"wp:attachment":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}