aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/memcache.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/lib/memcache.php')
-rw-r--r--engine/lib/memcache.php80
1 files changed, 62 insertions, 18 deletions
diff --git a/engine/lib/memcache.php b/engine/lib/memcache.php
index 2df93cbaf..b50c3c820 100644
--- a/engine/lib/memcache.php
+++ b/engine/lib/memcache.php
@@ -16,6 +16,12 @@
class ElggMemcache extends ElggCache
{
/**
+ * Minimum version of memcached needed to run
+ *
+ */
+ private static $MINSERVERVERSION = '1.1.12';
+
+ /**
* Namespace variable used for key
*
* @var string
@@ -66,20 +72,36 @@
if (!$CONFIG->memcache_servers)
throw new ConfigurationException(elgg_echo('memcache:noservers'));
- foreach ($CONFIG->memcache_servers as $server)
+ if (is_callable($this->memcache, 'addServer'))
{
- if (is_array($server))
+ foreach ($CONFIG->memcache_servers as $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
- );
+ 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);
+ }
+ }
+ else
+ {
+ if ((isset($CONFIG->debug)) && ($CONFIG->debug == true))
+ error_log(elgg_echo('memcache:noaddserver'));
+ $server = $CONFIG->memcache_servers[0];
+ if (is_array($server))
+ {
+ $this->memcache->connect($server[0], $server[1]);
}
else
$this->memcache->addServer($server, 11211);
@@ -87,13 +109,23 @@
// 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));
-
+ if (version_compare($this->version, ElggMemcache::$MINSERVERVERSION, '<'))
+ throw new ConfigurationException(sprintf(elgg_echo('memcache:versiontoolow'), ElggMemcache::$MINSERVERVERSION, $this->version));
+
// Set some defaults
if (isset($CONFIG->memcache_expires))
$this->expires = $CONFIG->memcache_expires;
-
+
+ }
+
+ /**
+ * Set the default expiry.
+ *
+ * @param int $expires The lifetime as a unix timestamp or time from now. Defaults forever.
+ */
+ public function setDefaultExpiry($expires = 0)
+ {
+ $this->expires = $expires;
}
/**
@@ -117,14 +149,26 @@
{
$key = $this->make_memcache_key($key);
- return $this->memcache->add($key, $data, null, $this->expires);
+ $this->keys_so_far[$key] = time();
+
+ $result = $this->memcache->set($key, $data, null, $this->expires);
+ if ((isset($CONFIG->debug)) && ($CONFIG->debug == true) && (!$result))
+ error_log("MEMCACHE: FAILED TO SAVE $key");
+
+ return $result;
}
public function load($key, $offset = 0, $limit = null)
{
$key = $this->make_memcache_key($key);
- return $this->memcache->get($key);
+ $this->keys_so_far[$key] = time();
+
+ $result = $this->memcache->get($key);
+ if ((isset($CONFIG->debug)) && ($CONFIG->debug == true) && (!$result))
+ error_log("MEMCACHE: FAILED TO LOAD $key");
+
+ return $result;
}
public function delete($key)
@@ -136,7 +180,7 @@
public function clear()
{
- foreach ($this->keys_so_far as $key)
+ foreach ($this->keys_so_far as $key => $ts)
$this->memcache->delete($key, 0);
$this->keys_so_far = array();