{"id":111,"date":"2009-06-02T23:32:51","date_gmt":"2009-06-02T16:32:51","guid":{"rendered":"http:\/\/www.icez.net\/blog\/?p=111"},"modified":"2009-08-26T01:09:47","modified_gmt":"2009-08-25T18:09:47","slug":"php-simple-object-cache","status":"publish","type":"post","link":"https:\/\/www.icez.net\/blog\/111\/php-simple-object-cache","title":{"rendered":"0061 | PHP simple object cache"},"content":{"rendered":"<p>\u0e23\u0e30\u0e1a\u0e1a cache \u0e21\u0e32\u0e2d\u0e35\u0e01\u0e41\u0e25\u0e49\u0e27 \u0e40\u0e2d\u0e4a\u0e2d\u0e01\u0e46<br \/>\n\u0e04\u0e23\u0e32\u0e27\u0e19\u0e35\u0e49\u0e40\u0e1b\u0e47\u0e19 object cache \u0e04\u0e23\u0e31\u0e1a \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48 cache \u0e17\u0e31\u0e49\u0e07\u0e2b\u0e19\u0e49\u0e32\u0e41\u0e1a\u0e1a\u0e04\u0e23\u0e32\u0e27\u0e01\u0e48\u0e2d\u0e19<\/p>\n<pre lang=\"php\">\r\n<?php\r\n$_cachetime = 3600;\r\n$_cacheroot = '\/path\/to\/cache\/';\r\nfunction cacheget($key) {\r\n\tglobal $_cachetime, $_cacheroot;\r\n\t$_cachename = sprintf('%x', crc32($key));\r\n\t$_cachefolder = $_cacheroot.substr($_cachename,0,2).'\/';\r\n\t$_cachefile = $_cachefolder.$_cachename;\r\n\tif (file_exists($_cachefile) &#038;&#038; (filemtime($_cachefile) > time() - $_cachetime || \r\n\t\t(file_exists($_cachefile.'.lock') && time() - filemtime($_cachefile.'.lock') < 120))) {\r\n\t\treturn unserialize(file_get_contents($_cachefile));\r\n\t}\r\n\treturn false;\r\n}\r\nfunction cachelock($key) {\r\n\tglobal $_cacheroot;\r\n\t$_cachename = sprintf(\"%x\", crc32($key));\r\n\t$_cachefolder = $_cacheroot.substr($_cachename,0,2).'\/';\r\n\t$_cachefile = $_cachefolder.$_cachename;\r\n\tumask(0);\r\n\tif (!file_exists($_cachefolder)) {\r\n\t\tmkdir($_cachefolder,0777);\r\n\t}\r\n\ttouch($_cachefile.'.lock');\r\n}\r\nfunction cacheset($key, $value) {\r\n\tglobal $_cacheroot;\r\n\t$_cachename = sprintf('%x', crc32($key));\r\n\t$_cachefolder = $_cacheroot.substr($_cachename,0,2).'\/';\r\n\t$_cachefile = $_cachefolder.$_cachename;\r\n\tumask(0);\r\n\tif (!file_exists($_cachefolder)) {\r\n\t\tmkdir($_cachefolder,0777);\r\n\t}\r\n\tif ($_h = fopen($_cachefile, 'w')) {\r\n\t\tfwrite($_h, serialize($value));\r\n\t\tfclose($_h);\r\n\t\tunlink($_cachefile.'.lock');\r\n\t}\r\n}\r\n?>\r\n<\/pre>\n<p>\u0e27\u0e34\u0e18\u0e35\u0e15\u0e34\u0e14\u0e15\u0e31\u0e49\u0e07: <\/p>\n<p>include \u0e44\u0e1f\u0e25\u0e4c\u0e44\u0e1b\u0e1a\u0e19\u0e2a\u0e38\u0e14\u0e40\u0e25\u0e22 \u0e41\u0e01\u0e49\u0e15\u0e31\u0e27\u0e41\u0e1b\u0e23 $_cacheroot \u0e01\u0e31\u0e1a $_cachetime \u0e14\u0e49\u0e27\u0e22\u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a<br \/>\nchmod 777 folder \u0e15\u0e32\u0e21\u0e17\u0e35\u0e48\u0e15\u0e31\u0e49\u0e07\u0e43\u0e19 $_cacheroot \u0e44\u0e27\u0e49\u0e14\u0e49\u0e27\u0e22<\/p>\n<p>\u0e27\u0e34\u0e18\u0e35\u0e40\u0e23\u0e35\u0e22\u0e01\u0e43\u0e0a\u0e49:<\/p>\n<p>\u0e2a\u0e21\u0e21\u0e15\u0e34\u0e27\u0e48\u0e32 cache \u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07 sql \u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a<br \/>\n\u0e16\u0e49\u0e32\u0e1b\u0e01\u0e15\u0e34\u0e43\u0e0a\u0e49\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e19\u0e35\u0e49<\/p>\n<pre lang=\"php\">\r\n<?php\r\n$query = 'SELECT * FROM table';\r\n$sql = mysql_query($query);\r\nwhile ($row = mysql_fetch_assoc($sql)) {\r\n        var_dump($row);\r\n}\r\n?>\r\n<\/pre>\n<p>\u0e01\u0e47\u0e41\u0e01\u0e49\u0e40\u0e1b\u0e47\u0e19\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13\u0e19\u0e35\u0e49<\/p>\n<pre lang=\"php\">\r\n<?php\r\n$query = 'SELECT * FROM table';\r\nif (!$rows = cacheget($query)) {\r\n        cachelock($query);\r\n        $sql = mysql_query($query);\r\n        $rows = array();\r\n        while ($row = mysql_fetch_assoc($sql))\r\n                $rows[] = $row;\r\n        cacheset($query, $rows);\r\n}\r\nforeach($rows as $row) {\r\n        var_dump($row);\r\n}\r\n?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0e23\u0e30\u0e1a\u0e1a cache \u0e21\u0e32\u0e2d\u0e35\u0e01\u0e41\u0e25\u0e49\u0e27 \u0e40\u0e2d\u0e4a\u0e2d\u0e01\u0e46 \u0e04\u0e23\u0e32\u0e27\u0e19\u0e35\u0e49\u0e40\u0e1b\u0e47\u0e19 object cache \u0e04\u0e23\u0e31\u0e1a \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48 cache \u0e17\u0e31\u0e49\u0e07\u0e2b\u0e19\u0e49\u0e32\u0e41\u0e1a\u0e1a\u0e04\u0e23\u0e32\u0e27\u0e01\u0e48\u0e2d\u0e19 \u0e27\u0e34\u0e18\u0e35\u0e15\u0e34\u0e14\u0e15\u0e31\u0e49\u0e07: include \u0e44\u0e1f\u0e25\u0e4c\u0e44\u0e1b\u0e1a\u0e19\u0e2a\u0e38\u0e14\u0e40\u0e25\u0e22 \u0e41\u0e01\u0e49\u0e15\u0e31\u0e27\u0e41\u0e1b\u0e23 $_cacheroot \u0e01\u0e31\u0e1a $_cachetime \u0e14\u0e49\u0e27\u0e22\u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a chmod 777 folder \u0e15\u0e32\u0e21\u0e17\u0e35\u0e48\u0e15\u0e31\u0e49\u0e07\u0e43\u0e19 $_cacheroot \u0e44\u0e27\u0e49\u0e14\u0e49\u0e27\u0e22 \u0e27\u0e34\u0e18\u0e35\u0e40\u0e23\u0e35\u0e22\u0e01\u0e43\u0e0a\u0e49: \u0e2a\u0e21\u0e21\u0e15\u0e34\u0e27\u0e48\u0e32 cache \u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07 sql \u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a \u0e16\u0e49\u0e32\u0e1b\u0e01\u0e15\u0e34\u0e43\u0e0a\u0e49\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e19\u0e35\u0e49 \u0e01\u0e47\u0e41\u0e01\u0e49\u0e40\u0e1b\u0e47\u0e19\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13\u0e19\u0e35\u0e49<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5,8,3],"tags":[87,5903,16],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-database","category-php","category-programming","category-webserver","tag-cache","tag-mysql","tag-optimize"],"_links":{"self":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":2,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}