diff options
Diffstat (limited to 'engine/lib')
-rw-r--r-- | engine/lib/cache.php | 65 | ||||
-rw-r--r-- | engine/lib/memcache.php | 19 |
2 files changed, 54 insertions, 30 deletions
diff --git a/engine/lib/cache.php b/engine/lib/cache.php index e8b996e59..0363f501b 100644 --- a/engine/lib/cache.php +++ b/engine/lib/cache.php @@ -169,6 +169,36 @@ } /** + * Shared memory cache description. + * Extends ElggCache with functions useful to shared memory style caches (static variables, memcache etc) + */ + abstract class ElggSharedMemoryCache extends ElggCache + { + /** + * Namespace variable used to keep various bits of the cache + * separate. + * + * @var string + */ + private $namespace; + + /** + * Set the namespace of this cache. + * This is useful for cache types (like memcache or static variables) where there is one large + * flat area of memory shared across all instances of the cache. + * + * @param string $namespace + */ + public function setNamespace($namespace = "default") { $this->namespace = $namespace; } + /** + * Get the namespace currently defined. + * + * @return string + */ + public function getNamespace() { return $this->namespace; } + } + + /** * ElggStaticVariableCache * Dummy cache which stores values in a static array. Using this makes future replacements to other caching back * ends (eg memcache) much easier. @@ -177,7 +207,7 @@ * @package Elgg * @subpackage API */ - class ElggStaticVariableCache extends ElggCache + class ElggStaticVariableCache extends ElggSharedMemoryCache { /** * The cache. @@ -187,54 +217,55 @@ private static $__cache; /** - * ID of a cache to use. - * - * @var unknown_type - */ - private $cache_id; - - /** * Create the variable cache. * * This function creates a variable cache in a static variable in memory, optionally with a given namespace (to avoid overlap). * - * @param string $cache_id The namespace for this cache to write to - note, namespaces of the same name are shared! + * @param string $namespace The namespace for this cache to write to - note, namespaces of the same name are shared! */ - function __construct($cache_id = 'default') + function __construct($namespace = 'default') { - $this->cache_id = $cache_id; + $this->setNamespace($namespace); $this->clear(); } public function save($key, $data) { - ElggStaticVariableCache::$__cache[$this->cache_id][$key] = $data; + $namespace = $this->getNamespace(); + + ElggStaticVariableCache::$__cache[$namespace][$key] = $data; return true; } public function load($key, $offset = 0, $limit = null) { - if (isset(ElggStaticVariableCache::$__cache[$this->cache_id][$key])) - return ElggStaticVariableCache::$__cache[$this->cache_id][$key]; + $namespace = $this->getNamespace(); + + if (isset(ElggStaticVariableCache::$__cache[$namespace][$key])) + return ElggStaticVariableCache::$__cache[$namespace][$key]; return false; } public function delete($key) { - unset(ElggStaticVariableCache::$__cache[$this->cache_id][$key]); + $namespace = $this->getNamespace(); + + unset(ElggStaticVariableCache::$__cache[$namespace][$key]); return true; } public function clear() { + $namespace = $this->getNamespace(); + if (!isset(ElggStaticVariableCache::$__cache)) ElggStaticVariableCache::$__cache = array(); - if (!isset(ElggStaticVariableCache::$__cache[$this->cache_id])) - ElggStaticVariableCache::$__cache[$this->cache_id] = array(); + if (!isset(ElggStaticVariableCache::$__cache[$namespace])) + ElggStaticVariableCache::$__cache[$namespace] = array(); } } diff --git a/engine/lib/memcache.php b/engine/lib/memcache.php index 27d8ed015..8afef6302 100644 --- a/engine/lib/memcache.php +++ b/engine/lib/memcache.php @@ -16,7 +16,7 @@ * Memcache wrapper class. * @author Curverider Ltd <info@elgg.com> */ - class ElggMemcache extends ElggCache + class ElggMemcache extends ElggSharedMemoryCache { /** * Minimum version of memcached needed to run @@ -25,13 +25,6 @@ private static $MINSERVERVERSION = '1.1.12'; /** - * Namespace variable used for key - * - * @var string - */ - private $namespace; - - /** * Memcache object */ private $memcache; @@ -62,7 +55,7 @@ { global $CONFIG; - $this->namespace = $namespace; + $this->setNamespace($namespace); // Do we have memcache? if (!class_exists('Memcache')) @@ -140,7 +133,7 @@ */ private function make_memcache_key($key) { - $prefix = $this->namespace . ":"; + $prefix = $this->getNamespace() . ":"; if (strlen($prefix.$key)> 250) $key = md5($key); @@ -200,19 +193,19 @@ /*private function load_persistent_keylist() { - return $this->memcache->get($this->namespace.':keys_so_far'); + return $this->memcache->get($this->getNamespace().':keys_so_far'); } private function save_persistent_keylist() { $stored = $this->load_persistent_keylist(); if ($stored) $this->keys_so_far = array_merge($this->keys_so_far, $stored); - return $this->memcache->set($this->namespace.':keys_so_far', $this->keys_so_far, null, 0); + return $this->memcache->set($this->getNamespace().':keys_so_far', $this->keys_so_far, null, 0); } private function clear_persistent_keylist() { - return $this->memcache->delete($this->namespace.':keys_so_far', 0); + return $this->memcache->delete($this->getNamespace().':keys_so_far', 0); } public function __destruct() |