aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/cache.php
diff options
context:
space:
mode:
authormarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-06 10:49:21 +0000
committermarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-06 10:49:21 +0000
commit4acff6b9c106ef73362dbb289fb2fade01c5f24d (patch)
treefdee504fbcb844ffe4cd45ace20951493cebb802 /engine/lib/cache.php
parente2282f9fb0a1acb521bf8ff2d6e486dd98f56172 (diff)
downloadelgg-4acff6b9c106ef73362dbb289fb2fade01c5f24d.tar.gz
elgg-4acff6b9c106ef73362dbb289fb2fade01c5f24d.tar.bz2
Class hierachy changed to separate shared memory caching
git-svn-id: https://code.elgg.org/elgg/trunk@2410 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/lib/cache.php')
-rw-r--r--engine/lib/cache.php65
1 files changed, 48 insertions, 17 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();
}
}