{"id":382,"date":"2014-03-22T11:08:58","date_gmt":"2014-03-22T03:08:58","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=382"},"modified":"2015-02-15T20:46:33","modified_gmt":"2015-02-15T12:46:33","slug":"php-bing-fa-cao-zuo-shi-de-jia-suo-gong-neng","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=382","title":{"rendered":"PHP\u5e76\u53d1\u64cd\u4f5c\u65f6\u7684\u52a0\u9501\u529f\u80fd"},"content":{"rendered":"<p>\u6709\u8fd9\u4e48\u4e00\u4e2a\u9700\u6c42\uff1a\u751f\u6210\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u591a\u7528\u6237\u90fd\u6709\u6743\u9650\u8fdb\u884c\u751f\u6210\uff0c\u9632\u6b62\u5e76\u53d1\u4e0b\uff0c\u5bfc\u81f4\u751f\u6210\u7684\u7ed3\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u9700\u8981\u5bf9\u751f\u6210\u7684\u8fc7\u7a0b\u8fdb\u884c\u52a0\u9501\uff0c\u53ea\u5bb9\u8bb8\u4e00\u4e2a\u7528\u6237\u5728 \u4e00\u4e2a\u65f6\u95f4\u5185\u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981\u7528\u5230\u9501\u4e86\uff0c\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u8fc7\u7a0b\u9501\u8d77\u6765\u3002\u5728\u7528\u4e86cache\u7684\u65f6\u5019\uff0ccache\u5931\u6548\u53ef\u80fd\u5bfc\u81f4\u77ac\u95f4\u7684\u591a\u6570\u5e76\u53d1\u8bf7\u6c42\u7a7f\u900f\u5230\u6570\u636e\u5e93\u6b64 \u65f6\u4e5f\u53ef\u4ee5\u5f97\u9700\u8981\u7528\u9501\u5728\u540c\u4e00\u5e76\u53d1\u7684\u8fc7\u7a0b\u4e2d\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u9501\u5b9a\u3002<\/p>\n<p>\u9488\u5bf9\u4ee5\u4e0a\u76842\u79cd\u60c5\u51b5\uff0c\u73b0\u5728\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u5bf9\u5904\u7406\u8fc7\u7a0b\u8fdb\u884c\u9501\u673a\u5236\uff0c\u901a\u8fc7PHP\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<p>\u7528\u5230\u4e86Eaccelerator\u7684\u5185\u5b58\u9501\u548c\u6587\u4ef6\u9501\uff0c\u539f\u7406\uff1a\u5224\u65ad\u7cfb\u7edf\u4e2d\u662f\u5426\u5b89\u4e86EAccelerator\u5982\u679c\u6709\u5219\u4f7f\u7528\u5185\u5b58\u9501\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fdb\u884c\u6587\u4ef6\u9501\u3002\u6839\u636e\u5e26\u5165\u7684key\u7684\u4e0d\u540c\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u9501\u76f4\u63a5\u7684\u5e76\u884c\u5904\u7406\uff0c\u7c7b\u4f3cInnodb\u7684\u884c\u7ea7\u9501\u3002<\/p>\n<p>\u5177\u4f53\u7c7b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\/**\r\n * CacheLock \u8fdb\u7a0b\u9501,\u4e3b\u8981\u7528\u6765\u8fdb\u884ccache\u5931\u6548\u65f6\u7684\u5355\u8fdb\u7a0bcache\u83b7\u53d6\uff0c\u9632\u6b62\u8fc7\u591a\u7684SQL\u8bf7\u6c42\u7a7f\u900f\u5230\u6570\u636e\u5e93\r\n * \u7528\u4e8e\u89e3\u51b3PHP\u5728\u5e76\u53d1\u65f6\u5019\u7684\u9501\u63a7\u5236,\u901a\u8fc7\u6587\u4ef6\/eaccelerator\u8fdb\u884c\u8fdb\u7a0b\u95f4\u9501\u5b9a\r\n * \u5982\u679c\u6ca1\u6709\u4f7f\u7528eaccelerator\u5219\u8fdb\u884c\u8fdb\u884c\u6587\u4ef6\u9501\u5904\u7406\uff0c\u4f1a\u505a\u5bf9\u5e94\u76ee\u5f55\u4e0b\u4ea7\u751f\u5bf9\u5e94\u7c92\u5ea6\u7684\u9501\r\n * \u4f7f\u7528\u4e86eaccelerator\u5219\u5728\u5185\u5b58\u4e2d\u5904\u7406\uff0c\u6027\u80fd\u76f8\u5bf9\u8f83\u9ad8\r\n * \u4e0d\u540c\u7684\u9501\u4e4b\u95f4\u5e76\u884c\u6267\u884c\uff0c\u7c7b\u4f3cmysql innodb\u7684\u884c\u7ea7\u9501\r\n * \u672c\u7c7b\u5728sunli\u7684phplock\u7684\u57fa\u7840\u4e0a\u505a\u4e86\u5c11\u8bb8\u4fee\u6539  http:\/\/code.google.com\/p\/phplock \r\n * @author yangxinqi\r\n *\r\n *\/\r\nclass CacheLock\r\n{\r\n\t\/\/\u6587\u4ef6\u9501\u5b58\u653e\u8def\u5f84\r\n\tprivate $path = null;\r\n\t\/\/\u6587\u4ef6\u53e5\u67c4\r\n\tprivate $fp = null;\r\n\t\/\/\u9501\u7c92\u5ea6,\u8bbe\u7f6e\u8d8a\u5927\u7c92\u5ea6\u8d8a\u5c0f\r\n\tprivate $hashNum = 100;\r\n\t\/\/cache key \r\n\tprivate $name;\r\n\t\/\/\u662f\u5426\u5b58\u5728eaccelerator\u6807\u5fd7\r\n\tprivate  $eAccelerator = false;\r\n\r\n\t\/**\r\n\t * \u6784\u9020\u51fd\u6570\r\n\t * \u4f20\u5165\u9501\u7684\u5b58\u653e\u8def\u5f84\uff0c\u53cacache key\u7684\u540d\u79f0\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fdb\u884c\u5e76\u53d1\r\n\t * @param string $path \u9501\u7684\u5b58\u653e\u76ee\u5f55\uff0c\u4ee5\"\/\"\u7ed3\u5c3e\r\n\t * @param string $name cache key\r\n\t *\/\r\n\tpublic function __construct($name,$path='lock\\\\')\r\n\t{\r\n\t\t\/\/\u5224\u65ad\u662f\u5426\u5b58\u5728eAccelerator,\u8fd9\u91cc\u542f\u7528\u4e86eAccelerator\u4e4b\u540e\u53ef\u4ee5\u8fdb\u884c\u5185\u5b58\u9501\u63d0\u9ad8\u6548\u7387\r\n\t\t$this-&gt;eAccelerator = function_exists(\"eaccelerator_lock\");\r\n\t\tif(!$this-&gt;eAccelerator)\r\n\t\t{\r\n\t\t\t$this-&gt;path = $path.($this-&gt;_mycrc32($name) % $this-&gt;hashNum).'.txt';\r\n\t\t}\r\n\t\t$this-&gt;name = $name;\r\n\t}\r\n\r\n\t\/**\r\n\t * crc32\r\n\t * crc32\u5c01\u88c5\r\n\t * @param int $string\r\n\t * @return int\r\n\t *\/\r\n\tprivate function _mycrc32($string)\r\n\t{\r\n\t\t$crc = abs (crc32($string));\r\n\t\tif ($crc &amp; 0x80000000) {\r\n\t\t\t$crc ^= 0xffffffff;\r\n\t\t\t$crc += 1;\r\n\t\t}\r\n\t\treturn $crc;\r\n\t}\r\n\t\/**\r\n\t * \u52a0\u9501\r\n\t * Enter description here ...\r\n\t *\/\r\n\tpublic function lock()\r\n\t{\r\n\t\t\/\/\u5982\u679c\u65e0\u6cd5\u5f00\u542fea\u5185\u5b58\u9501\uff0c\u5219\u5f00\u542f\u6587\u4ef6\u9501\r\n\t\tif(!$this-&gt;eAccelerator)\r\n\t\t{\r\n\t\t\t\/\/\u914d\u7f6e\u76ee\u5f55\u6743\u9650\u53ef\u5199\r\n\t\t\t$this-&gt;fp = fopen($this-&gt;path, 'w+');\r\n\t\t\tif($this-&gt;fp === false)\r\n\t\t\t{\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\treturn flock($this-&gt;fp, LOCK_EX);\r\n\t\t}else{\r\n\t\t\treturn eaccelerator_lock($this-&gt;name);\r\n\t\t}\r\n\t}\r\n\r\n\t\/**\r\n\t * \u89e3\u9501\r\n\t * Enter description here ...\r\n\t *\/\r\n\tpublic function unlock()\r\n\t{\r\n\t\tif(!$this-&gt;eAccelerator)\r\n\t\t{\r\n\t\t\tif($this-&gt;fp !== false)\r\n\t\t\t{\r\n\t\t\t\tflock($this-&gt;fp, LOCK_UN);\r\n\t\t\t\tclearstatcache();\r\n\t\t\t}\r\n\t\t\t\/\/\u8fdb\u884c\u5173\u95ed\r\n\t\t\tfclose($this-&gt;fp);\r\n\t\t}else{\r\n\t\t\treturn eaccelerator_unlock($this-&gt;name);\r\n\t\t}\r\n\t}\r\n}\r\n?&gt;<\/pre>\n<p>\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">$lock = new CacheLock('key_name');\r\n$lock-&gt;lock();\r\n\/\/logic here\r\n$lock-&gt;unlock();\r\n\/\/\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u4e0b\u6587\u4ef6\u9501\u6240\u5728\u8def\u5f84\u9700\u8981\u6709\u5199\u6743\u9650.<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u8fd9\u4e48\u4e00\u4e2a\u9700\u6c42\uff1a\u751f\u6210\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u7531\u4e8e\u591a\u7528\u6237\u90fd\u6709\u6743\u9650\u8fdb\u884c\u751f\u6210\uff0c\u9632\u6b62\u5e76\u53d1\u4e0b\uff0c\u5bfc\u81f4\u751f\u6210\u7684\u7ed3\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u9700\u8981\u5bf9\u751f\u6210\u7684\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=382"}],"version-history":[{"count":2,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}