From 253505d11d7602a07527e575076d0185e82110ec Mon Sep 17 00:00:00 2001 From: marcus Date: Tue, 4 Nov 2008 13:45:24 +0000 Subject: First draft memcache object. No testing yet carried out, submitted 'cos I'm moving computers. git-svn-id: https://code.elgg.org/elgg/trunk@2392 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/memcache.php | 147 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 engine/lib/memcache.php (limited to 'engine/lib') diff --git a/engine/lib/memcache.php b/engine/lib/memcache.php new file mode 100644 index 000000000..2df93cbaf --- /dev/null +++ b/engine/lib/memcache.php @@ -0,0 +1,147 @@ + + * @copyright Curverider Ltd 2008 + * @link http://elgg.org/ + */ + + + class ElggMemcache extends ElggCache + { + /** + * Namespace variable used for key + * + * @var string + */ + private $namespace; + + /** + * Memcache object + */ + private $memcache; + + /** + * Expiry of saved items (defaults forever) + */ + private $expires = 0; + + /** + * The version of memcache running + */ + private $version = 0; + + /** + * Keys so far. + * This variable holds a list of keys we have seen so that when we call ->clear() we invalidate only those keys. + * TODO: Could this be done better? + */ + private $keys_so_far = array(); + + /** + * Connect to memcache. + * + * @param string $cache_id The namespace for this cache to write to - note, namespaces of the same name are shared! + */ + function __construct($namespace = 'default') + { + global $CONFIG; + + $this->namespace = $namespace; + + // Do we have memcache? + if (!class_exists('Memcache')) + throw new ConfigurationException(elgg_echo('memcache:notinstalled')); + + // Create memcache object + $this->memcache = new Memcache; + + // Now add servers + if (!$CONFIG->memcache_servers) + throw new ConfigurationException(elgg_echo('memcache:noservers')); + + foreach ($CONFIG->memcache_servers as $server) + { + if (is_array($server)) + { + $this->memcache->addServer( + $server[0], + isset($server[1]) ? $server[1] : 11211, + isset($server[2]) ? $server[2] : true, + isset($server[3]) ? $server[3] : null, + isset($server[4]) ? $server[4] : 1, + isset($server[5]) ? $server[5] : 15, + isset($server[6]) ? $server[6] : true + ); + + } + else + $this->memcache->addServer($server, 11211); + } + + // Get version + $this->version = $this->memcache->getversion(); + if (version_compare($this->version, '2.0.0', '<')) + throw new ConfigurationException(sprintf(elgg_echo('memcache:versiontoolow'), $this->version)); + + // Set some defaults + if (isset($CONFIG->memcache_expires)) + $this->expires = $CONFIG->memcache_expires; + + } + + /** + * Combine a key with the namespace. + * Memcache can only accept <250 char key. If the given key is too long it is shortened. + * + * @param string $key The key + * @return string The new key. + */ + private function make_memcache_key($key) + { + $prefix = $this->namespace . ":"; + + if (strlen($prefix.$key)> 250) + $key = md5($key); + + return $prefix.$key; + } + + public function save($key, $data) + { + $key = $this->make_memcache_key($key); + + return $this->memcache->add($key, $data, null, $this->expires); + } + + public function load($key, $offset = 0, $limit = null) + { + $key = $this->make_memcache_key($key); + + return $this->memcache->get($key); + } + + public function delete($key) + { + $key = $this->make_memcache_key($key); + + return $this->memcache->delete($key, 0); + } + + public function clear() + { + foreach ($this->keys_so_far as $key) + $this->memcache->delete($key, 0); + + $this->keys_so_far = array(); + + return true; + } + } +?> \ No newline at end of file -- cgit v1.2.3