aboutsummaryrefslogtreecommitdiff
path: root/engine/lib
diff options
context:
space:
mode:
authormarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-04 13:45:24 +0000
committermarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-04 13:45:24 +0000
commit253505d11d7602a07527e575076d0185e82110ec (patch)
tree385e0b957766da48fe51a783142fb99c3927a9ac /engine/lib
parentb9313967bc61dfd208498045e707016b81b627cb (diff)
downloadelgg-253505d11d7602a07527e575076d0185e82110ec.tar.gz
elgg-253505d11d7602a07527e575076d0185e82110ec.tar.bz2
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
Diffstat (limited to 'engine/lib')
-rw-r--r--engine/lib/memcache.php147
1 files changed, 147 insertions, 0 deletions
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 @@
+<?php
+ /**
+ * Elgg memcache support.
+ *
+ * Requires php5-memcache to work.
+ *
+ * @package Elgg
+ * @subpackage API
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd <info@elgg.com>
+ * @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