diff options
Diffstat (limited to 'engine/classes/ElggMemcache.php')
| -rw-r--r-- | engine/classes/ElggMemcache.php | 79 |
1 files changed, 64 insertions, 15 deletions
diff --git a/engine/classes/ElggMemcache.php b/engine/classes/ElggMemcache.php index b8be999cd..91d50ab89 100644 --- a/engine/classes/ElggMemcache.php +++ b/engine/classes/ElggMemcache.php @@ -1,7 +1,9 @@ <?php /** * Memcache wrapper class. - * @author Curverider Ltd <info@elgg.com> + * + * @package Elgg.Core + * @subpackage Memcache */ class ElggMemcache extends ElggSharedMemoryCache { /** @@ -28,7 +30,10 @@ class ElggMemcache extends ElggSharedMemoryCache { /** * Connect to memcache. * - * @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! + * + * @throws ConfigurationException */ function __construct($namespace = 'default') { global $CONFIG; @@ -37,7 +42,7 @@ class ElggMemcache extends ElggSharedMemoryCache { // Do we have memcache? if (!class_exists('Memcache')) { - throw new ConfigurationException(elgg_echo('memcache:notinstalled')); + throw new ConfigurationException('PHP memcache module not installed, you must install php5-memcache'); } // Create memcache object @@ -45,7 +50,7 @@ class ElggMemcache extends ElggSharedMemoryCache { // Now add servers if (!$CONFIG->memcache_servers) { - throw new ConfigurationException(elgg_echo('memcache:noservers')); + throw new ConfigurationException('No memcache servers defined, please populate the $CONFIG->memcache_servers variable'); } if (is_callable(array($this->memcache, 'addServer'))) { @@ -82,7 +87,12 @@ class ElggMemcache extends ElggSharedMemoryCache { // Get version $this->version = $this->memcache->getVersion(); if (version_compare($this->version, ElggMemcache::$MINSERVERVERSION, '<')) { - throw new ConfigurationException(sprintf(elgg_echo('memcache:versiontoolow'), ElggMemcache::$MINSERVERVERSION, $this->version)); + $msg = vsprintf('Memcache needs at least version %s to run, you are running %s', + array(ElggMemcache::$MINSERVERVERSION, + $this->version + )); + + throw new ConfigurationException($msg); } // Set some defaults @@ -95,6 +105,8 @@ class ElggMemcache extends ElggSharedMemoryCache { * Set the default expiry. * * @param int $expires The lifetime as a unix timestamp or time from now. Defaults forever. + * + * @return void */ public function setDefaultExpiry($expires = 0) { $this->expires = $expires; @@ -105,46 +117,83 @@ class ElggMemcache extends ElggSharedMemoryCache { * 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) { + private function makeMemcacheKey($key) { $prefix = $this->getNamespace() . ":"; - if (strlen($prefix.$key)> 250) { + if (strlen($prefix . $key) > 250) { $key = md5($key); } - return $prefix.$key; + return $prefix . $key; } - public function save($key, $data) { - $key = $this->make_memcache_key($key); + /** + * Saves a name and value to the cache + * + * @param string $key Name + * @param string $data Value + * @param integer $expires Expires (in seconds) + * + * @return bool + */ + public function save($key, $data, $expires = null) { + $key = $this->makeMemcacheKey($key); - $result = $this->memcache->set($key, $data, null, $this->expires); - if (!$result) { + if ($expires === null) { + $expires = $this->expires; + } + + $result = $this->memcache->set($key, $data, null, $expires); + if ($result === false) { elgg_log("MEMCACHE: FAILED TO SAVE $key", 'ERROR'); } return $result; } + /** + * Retrieves data. + * + * @param string $key Name of data to retrieve + * @param int $offset Offset + * @param int $limit Limit + * + * @return mixed + */ public function load($key, $offset = 0, $limit = null) { - $key = $this->make_memcache_key($key); + $key = $this->makeMemcacheKey($key); $result = $this->memcache->get($key); - if (!$result) { + if ($result === false) { elgg_log("MEMCACHE: FAILED TO LOAD $key", 'ERROR'); } return $result; } + /** + * Delete data + * + * @param string $key Name of data + * + * @return bool + */ public function delete($key) { - $key = $this->make_memcache_key($key); + $key = $this->makeMemcacheKey($key); return $this->memcache->delete($key, 0); } + /** + * Clears the entire cache? + * + * @todo write or remove. + * + * @return true + */ public function clear() { // DISABLE clearing for now - you must use delete on a specific key. return true; |
