From f07cc09e4f7800eb5f57b6ab2277eebeedf53bc4 Mon Sep 17 00:00:00 2001 From: ngleader Date: Wed, 12 May 2010 08:34:23 +0000 Subject: [PATCH] #18885380 add Cache Handler git-svn-id: http://xe-core.googlecode.com/svn/sandbox@7450 201d5d3c-b55e-5fd7-737f-ddc643e51545 --- classes/cache/CacheApc.class.php | 70 ++++++++++++++++++++ classes/cache/CacheHandler.class.php | 94 +++++++++++++++++++++++++++ classes/cache/CacheMemcache.class.php | 91 ++++++++++++++++++++++++++ 3 files changed, 255 insertions(+) create mode 100644 classes/cache/CacheApc.class.php create mode 100644 classes/cache/CacheHandler.class.php create mode 100644 classes/cache/CacheMemcache.class.php diff --git a/classes/cache/CacheApc.class.php b/classes/cache/CacheApc.class.php new file mode 100644 index 000000000..bd77c66cf --- /dev/null +++ b/classes/cache/CacheApc.class.php @@ -0,0 +1,70 @@ +valid_time; + return apc_store(md5(_XE_PATH_.$key), array(time(), $buff), $valid_time); + } + + function isValid($key, $modified_time = 0) + { + $key = md5(_XE_PATH_.$key); + $obj = apc_fetch($key, $success); + if(!$success || !is_array($obj)) return false; + unset($obj[1]); + + if($modified_time > 0 && $modified_time > $obj[0]) + { + $this->delete($_key); + return false; + } + + return true; + } + + function get($key, $modified_time = 0) + { + $key = md5(_XE_PATH_.$key); + $obj = apc_fetch($key, $success); + if(!$success || !is_array($obj)) return false; + + if($modified_time > 0 && $modified_time > $obj[0]) + { + $this->_delete($key); + return false; + } + + return $obj[1]; + } + + function _delete($_key) + { + $this->put($_key,null,1); + } + + function delete($key) + { + $this->_delete(md5(_XE_PATH_.$key)); + } + + function truncate() + { + apc_clear_cache('user'); + } + } +?> diff --git a/classes/cache/CacheHandler.class.php b/classes/cache/CacheHandler.class.php new file mode 100644 index 000000000..03dcc66aa --- /dev/null +++ b/classes/cache/CacheHandler.class.php @@ -0,0 +1,94 @@ +use_template_cache =='apc') $type = 'apc'; + else if(substr($info->use_template_cache,0,8)=='memcache'){ + $type = 'memcache'; + $url = $info->use_template_cache; + } + }else if($target == 'template'){ + if($info->use_template_cache =='apc') $type = 'apc'; + else if(substr($info->use_template_cache,0,8)=='memcache'){ + $type = 'memcache'; + $url = $info->use_template_cache; + } + } + + if($type){ + $class = 'Cache' . ucfirst($type); + include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class); + $this->handler = call_user_func(array($class,'getInstance'), $url); + } + } + + function isSupport(){ + if($this->handler && $this->handler->isSupport()) return true; + return false; + } + + function get($key, $modified_time = 0){ + if(!$this->handler) return false; + return $this->handler->get($key, $modified_time); + } + + function put($key, $obj, $valid_time = 0){ + if(!$this->handler) return false; + return $this->handler->put($key, $obj, $valid_time); + } + + function delete($key){ + if(!$this->handler) return false; + return $this->handler->delete($key); + } + + function isValid($key, $modified_time){ + if(!$this->handler) return false; + return $this->handler->isValid($key, $modified_time); + } + + function truncate(){ + if(!$this->handler) return false; + return $this->handler->truncate(); + } + } + + class CacheBase{ + function get($key, $modified_time = 0){ + return false; + } + + function put($key, $obj, $valid_time = 0){ + return false; + } + + function isValid($key, $modified_time = 0){ + return false; + } + + function isSupport(){ + return false; + } + + function truncate(){ + return false; + } + } +?> diff --git a/classes/cache/CacheMemcache.class.php b/classes/cache/CacheMemcache.class.php new file mode 100644 index 000000000..cd45c0eca --- /dev/null +++ b/classes/cache/CacheMemcache.class.php @@ -0,0 +1,91 @@ +Memcache = new Memcache; + + foreach($config['url'] as $url) { + $info = parse_url($url); + $this->Memcache->addServer($info['host'], $info['port']); + } + } + + function isSupport(){ + return $this->Memcache->set('xe', 'xe', MEMCACHE_COMPRESSED, 1); + } + + function getKey($key){ + return md5(_XE_PATH_.$key); + } + + function put($key, $buff, $valid_time = 0){ + if($valid_time == 0) $valid_time = $this->valid_time; + + return $this->Memcache->set($this->getKey($key), array(time(), $buff), MEMCACHE_COMPRESSED, $valid_time); + } + + function isValid($key, $modified_time = 0) { + $_key = $this->getKey($key); + + $obj = $this->Memcache->get($_key); + if(!$obj || !is_array($obj)) return false; + unset($obj[1]); + + if($modified_time > 0 && $modified_time > $obj[0]) { + $this->_delete($_key); + return false; + } + + return true; + } + + function get($key, $modified_time = 0) { + $_key = $this->getKey($key); + $obj = $this->Memcache->get($_key); + if(!$obj || !is_array($obj)) return false; + + if($modified_time > 0 && $modified_time > $obj[0]) { + $this->_delete($_key); + return false; + } + + unset($obj[0]); + + return $obj[1]; + } + + function delete($key) { + $_key = $this->getKey($key); + $this->_delete($_key); + } + + function _delete($_key) { + $this->Memcache->delete($_key); + } + + function truncate() { + // not support memcached + return false; + } + } +?>