{"id":264,"date":"2013-12-29T00:43:37","date_gmt":"2013-12-28T16:43:37","guid":{"rendered":"http:\/\/web.jofar.cn\/?p=264"},"modified":"2013-12-29T00:44:45","modified_gmt":"2013-12-28T16:44:45","slug":"opencart%e5%a2%9e%e5%8a%a0memcache%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/web.jofar.cn\/?p=264","title":{"rendered":"opencart\u589e\u52a0memcache\u7f13\u5b58"},"content":{"rendered":"<p>\u4ece https:\/\/github.com\/shaman\/opencart-memcached \u4e0b\u8f7dcache.php<\/p>\n<div>\u4f7f\u7528\u8fd9\u4e2a cache.php\u66ff\u6362\u7cfb\u7edf\u4e2d\u7684 .\/system\/library\/cache.php<\/div>\n<div>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d.\/config.php \u548c.\/admin\/config.php\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801<\/div>\n<div>\n<div><\/div>\n<ol start=\"1\">\n<li>\/\/?CACHE<\/li>\n<li>define(&#8216;CACHE_DRIVER&#8217;,?&#8217;memcached&#8217;);<\/li>\n<li>define(&#8216;MEMCACHE_HOSTNAME&#8217;,?&#8217;localhost&#8217;);<\/li>\n<li>define(&#8216;MEMCACHE_PORT&#8217;,?&#8217;11211&#8242;);<\/li>\n<li>define(&#8216;MEMCACHE_NAMESPACE&#8217;,?&#8217;opencart_test&#8217;);<\/li>\n<\/ol>\n<\/div>\n<div>\u4e0b\u9762\u662fcache.php\u4ee3\u7801\uff1a<\/div>\n<div><\/div>\n<div>\n<div id=\"LC1\">&lt;?php<\/div>\n<div id=\"LC2\"><\/div>\n<div id=\"LC3\">\/**<\/div>\n<div id=\"LC4\">* OpenCart Ukrainian Community<\/div>\n<div id=\"LC5\">*<\/div>\n<div id=\"LC6\">* LICENSE<\/div>\n<div id=\"LC7\">*<\/div>\n<div id=\"LC8\">* This source file is subject to the GNU General Public License, Version 3<\/div>\n<div id=\"LC9\">* It is also available through the world-wide-web at this URL:<\/div>\n<div id=\"LC10\">* http:\/\/www.gnu.org\/copyleft\/gpl.html<\/div>\n<div id=\"LC11\">* If you did not receive a copy of the license and are unable to<\/div>\n<div id=\"LC12\">* obtain it through the world-wide-web, please send an email<\/div>\n<div id=\"LC13\">* to license@opencart.ua so we can send you a copy immediately.<\/div>\n<div id=\"LC14\">*<\/div>\n<div id=\"LC15\">* @category OpenCart<\/div>\n<div id=\"LC16\">* @package OCU Memcached<\/div>\n<div id=\"LC17\">* @copyright Copyright (c) 2011 created by UncleAndy, maintained by Eugene Kuligin for OpenCart Ukrainian Community (http:\/\/www.opencart.ua)<\/div>\n<div id=\"LC18\">* @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU General Public License, Version 3<\/div>\n<div id=\"LC19\">*\/<\/div>\n<div id=\"LC20\"><\/div>\n<div id=\"LC21\"><\/div>\n<div id=\"LC22\">final class Cache {<\/div>\n<div id=\"LC23\"><\/div>\n<div id=\"LC24\">????private $expire;<\/div>\n<div id=\"LC25\">????private $memcache;<\/div>\n<div id=\"LC26\">????private $ismemcache = false;<\/div>\n<div id=\"LC27\"><\/div>\n<div id=\"LC28\">????public function __construct($exp = 3600) {<\/div>\n<div id=\"LC29\">????????$this-&gt;expire = $exp;<\/div>\n<div id=\"LC30\"><\/div>\n<div id=\"LC31\">????????if (CACHE_DRIVER == &#8216;memcached&#8217;)<\/div>\n<div id=\"LC32\">????????{<\/div>\n<div id=\"LC33\">????????????$mc = new Memcache;<\/div>\n<div id=\"LC34\">????????if ($mc-&gt;pconnect(MEMCACHE_HOSTNAME, MEMCACHE_PORT))<\/div>\n<div id=\"LC35\">????????{<\/div>\n<div id=\"LC36\">????????????$this-&gt;memcache = $mc;<\/div>\n<div id=\"LC37\">????????????$this-&gt;ismemcache = true;<\/div>\n<div id=\"LC38\">????????};<\/div>\n<div id=\"LC39\">????????};<\/div>\n<div id=\"LC40\"><\/div>\n<div id=\"LC41\">????????$files = glob(DIR_CACHE . &#8216;cache.*&#8217;);<\/div>\n<div id=\"LC42\"><\/div>\n<div id=\"LC43\">????????if ($files) {<\/div>\n<div id=\"LC44\">????????????foreach ($files as $file) {<\/div>\n<div id=\"LC45\">????????????????$time = substr(strrchr($file, &#8216;.&#8217;), 1);<\/div>\n<div id=\"LC46\"><\/div>\n<div id=\"LC47\">??????????????????if ($time &lt; time()) {<\/div>\n<div id=\"LC48\">????????????????????if (file_exists($file)) {<\/div>\n<div id=\"LC49\">????????????????????????@unlink($file);<\/div>\n<div id=\"LC50\">????????????????????}<\/div>\n<div id=\"LC51\">??????????????????}<\/div>\n<div id=\"LC52\">????????????}<\/div>\n<div id=\"LC53\">????????}<\/div>\n<div id=\"LC54\">??????}<\/div>\n<div id=\"LC55\"><\/div>\n<div id=\"LC56\">????public function get($key) {<\/div>\n<div id=\"LC57\">????????if ((CACHE_DRIVER == &#8216;memcached&#8217;) &amp;&amp; $this-&gt;ismemcache)<\/div>\n<div id=\"LC58\">????????{<\/div>\n<div id=\"LC59\">????????return($this-&gt;memcache-&gt;get(MEMCACHE_NAMESPACE . $key, 0));<\/div>\n<div id=\"LC60\">????????}<\/div>\n<div id=\"LC61\">????????else<\/div>\n<div id=\"LC62\">????????{<\/div>\n<div id=\"LC63\">????????$files = glob(DIR_CACHE . &#8216;cache.&#8217; . $key . &#8216;.*&#8217;);<\/div>\n<div id=\"LC64\"><\/div>\n<div id=\"LC65\">????????if ($files) {<\/div>\n<div id=\"LC66\">????????????foreach ($files as $file) {<\/div>\n<div id=\"LC67\">??????????????????$cache = &#8221;;<\/div>\n<div id=\"LC68\"><\/div>\n<div id=\"LC69\">????????????????$handle = fopen($file, &#8216;r&#8217;);<\/div>\n<div id=\"LC70\"><\/div>\n<div id=\"LC71\">????????????????if ($handle) {<\/div>\n<div id=\"LC72\">????????????????????$cache = fread($handle, filesize($file));<\/div>\n<div id=\"LC73\"><\/div>\n<div id=\"LC74\">????????????????????fclose($handle);<\/div>\n<div id=\"LC75\">????????????????}<\/div>\n<div id=\"LC76\"><\/div>\n<div id=\"LC77\">??????????????????return unserialize($cache);<\/div>\n<div id=\"LC78\">????????????????}<\/div>\n<div id=\"LC79\">????????}<\/div>\n<div id=\"LC80\">????????}<\/div>\n<div id=\"LC81\">??????}<\/div>\n<div id=\"LC82\"><\/div>\n<div id=\"LC83\">??????public function set($key, $value) {<\/div>\n<div id=\"LC84\">????????if ((CACHE_DRIVER == &#8216;memcached&#8217;) &amp;&amp; $this-&gt;ismemcache)<\/div>\n<div id=\"LC85\">????????{<\/div>\n<div id=\"LC86\">????????$this-&gt;memcache-&gt;set(MEMCACHE_NAMESPACE . $key, $value, MEMCACHE_COMPRESSED, $this-&gt;expire);<\/div>\n<div id=\"LC87\">????????}<\/div>\n<div id=\"LC88\">????????else<\/div>\n<div id=\"LC89\">????????{<\/div>\n<div id=\"LC90\"><\/div>\n<div id=\"LC91\">????????????????$this-&gt;delete($key);<\/div>\n<div id=\"LC92\"><\/div>\n<div id=\"LC93\">????????????$file = DIR_CACHE . &#8216;cache.&#8217; . $key . &#8216;.&#8217; . (time() + $this-&gt;expire);<\/div>\n<div id=\"LC94\"><\/div>\n<div id=\"LC95\">????????????$handle = fopen($file, &#8216;w&#8217;);<\/div>\n<div id=\"LC96\"><\/div>\n<div id=\"LC97\">????????????????fwrite($handle, serialize($value));<\/div>\n<div id=\"LC98\"><\/div>\n<div id=\"LC99\">????????????????fclose($handle);<\/div>\n<div id=\"LC100\">????????????};<\/div>\n<div id=\"LC101\">??????}<\/div>\n<div id=\"LC102\"><\/div>\n<div id=\"LC103\">??????public function delete($key) {<\/div>\n<div id=\"LC104\">????????if ((CACHE_DRIVER == &#8216;memcached&#8217;) &amp;&amp; $this-&gt;ismemcache)<\/div>\n<div id=\"LC105\">????????{<\/div>\n<div id=\"LC106\">????????$this-&gt;memcache-&gt;delete(MEMCACHE_NAMESPACE . $key, 0);<\/div>\n<div id=\"LC107\">????????}<\/div>\n<div id=\"LC108\">????????else<\/div>\n<div id=\"LC109\">????????{<\/div>\n<div id=\"LC110\">????????$files = glob(DIR_CACHE . &#8216;cache.&#8217; . $key . &#8216;.*&#8217;);<\/div>\n<div id=\"LC111\"><\/div>\n<div id=\"LC112\">????????if ($files) {<\/div>\n<div id=\"LC113\">????????????foreach ($files as $file) {<\/div>\n<div id=\"LC114\">??????????????????if (file_exists($file)) {<\/div>\n<div id=\"LC115\">????????????????????@unlink($file);<\/div>\n<div id=\"LC116\">????????????????????clearstatcache();<\/div>\n<div id=\"LC117\">????????????????}<\/div>\n<div id=\"LC118\">????????????}<\/div>\n<div id=\"LC119\">????????}<\/div>\n<div id=\"LC120\">????????}<\/div>\n<div id=\"LC121\">??????}<\/div>\n<div id=\"LC122\">}<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece https:\/\/github.com\/shaman\/opencart-memcached \u4e0b\u8f7dcache.\u2026 <span class=\"read-more\"><a href=\"https:\/\/web.jofar.cn\/?p=264\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12,5],"tags":[],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-cloud","category-server","category-practice-readme"],"_links":{"self":[{"href":"https:\/\/web.jofar.cn\/index.php?rest_route=\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/web.jofar.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web.jofar.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web.jofar.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/web.jofar.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264"}],"version-history":[{"count":0,"href":"https:\/\/web.jofar.cn\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions"}],"wp:attachment":[{"href":"https:\/\/web.jofar.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.jofar.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.jofar.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}