From 4acff6b9c106ef73362dbb289fb2fade01c5f24d Mon Sep 17 00:00:00 2001 From: marcus Date: Thu, 6 Nov 2008 10:49:21 +0000 Subject: Class hierachy changed to separate shared memory caching git-svn-id: https://code.elgg.org/elgg/trunk@2410 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/cache.php | 65 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 17 deletions(-) (limited to 'engine/lib/cache.php') 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 @@ -168,6 +168,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 @@ -177,7 +207,7 @@ * @package Elgg * @subpackage API */ - class ElggStaticVariableCache extends ElggCache + class ElggStaticVariableCache extends ElggSharedMemoryCache { /** * The cache. @@ -186,55 +216,56 @@ */ 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(); } } -- cgit v1.2.3