aboutsummaryrefslogtreecommitdiff
path: root/engine/classes/ElggMemcache.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/classes/ElggMemcache.php')
-rw-r--r--engine/classes/ElggMemcache.php94
1 files changed, 73 insertions, 21 deletions
diff --git a/engine/classes/ElggMemcache.php b/engine/classes/ElggMemcache.php
index 9f12fb73b..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,20 +50,20 @@ 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($this->memcache, 'addServer')) {
+ if (is_callable(array($this->memcache, 'addServer'))) {
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[2]) ? $server[2] : FALSE,
+ isset($server[3]) ? $server[3] : 1,
isset($server[4]) ? $server[4] : 1,
isset($server[5]) ? $server[5] : 15,
- isset($server[6]) ? $server[6] : true
+ isset($server[6]) ? $server[6] : TRUE
);
} else {
@@ -66,7 +71,10 @@ class ElggMemcache extends ElggSharedMemoryCache {
}
}
} else {
- elgg_log(elgg_echo('memcache:noaddserver'), 'ERROR');
+ // don't use elgg_echo() here because most of the config hasn't been loaded yet
+ // and it caches the language, which is hard coded in $CONFIG->language as en.
+ // overriding it with real values later has no effect because it's already cached.
+ elgg_log("This version of the PHP memcache API doesn't support multiple servers.", 'ERROR');
$server = $CONFIG->memcache_servers[0];
if (is_array($server)) {
@@ -77,9 +85,14 @@ class ElggMemcache extends ElggSharedMemoryCache {
}
// Get version
- $this->version = $this->memcache->getversion();
+ $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
@@ -92,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;
@@ -102,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;