MDL-38205 cache: implemented key prefix in memcache plugin

This commit is contained in:
Sam Hemelryk
2013-03-01 10:11:45 +13:00
parent cce0d9a100
commit 4af3d7e7a3
+31 -10
View File
@@ -117,7 +117,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
$this->connection->addServer($server[0], $server[1], true, $server[2]);
// Test the connection to this server.
}
$this->isready = @$this->connection->set("ping", 'ping', MEMCACHE_COMPRESSED, 1);
$this->isready = @$this->connection->set($this->parse_key('ping'), 'ping', MEMCACHE_COMPRESSED, 1);
}
/**
@@ -191,6 +191,20 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
return self::MODE_APPLICATION + self::MODE_SESSION;
}
/**
* Parses the given key to make it work for this memcache backend.
*
* @param string $key The raw key.
* @return string The resulting key.
*/
protected function parse_key($key) {
if (strlen($key) > 245) {
$key = '_sha1_'.sha1($key);
}
$key = 'mdl_'.$key;
return $key;
}
/**
* Retrieves an item from the cache store given its key.
*
@@ -198,7 +212,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
* @return mixed The data that was associated with the key, or false if the key did not exist.
*/
public function get($key) {
return $this->connection->get($key);
return $this->connection->get($this->parse_key($key));
}
/**
@@ -211,16 +225,23 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
* be set to false.
*/
public function get_many($keys) {
$result = $this->connection->get($keys);
$mkeys = array();
foreach ($keys as $key) {
$mkeys[$key] = $this->parse_key($key);
}
$result = $this->connection->get($mkeys);
if (!is_array($result)) {
$result = array();
}
foreach ($keys as $key) {
if (!array_key_exists($key, $result)) {
$result[$key] = false;
$return = array();
foreach ($mkeys as $key => $mkey) {
if (!array_key_exists($mkey, $result)) {
$return[$key] = false;
} else {
$return[$key] = $result[$mkey];
}
}
return $result;
return $return;
}
/**
@@ -231,7 +252,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
* @return bool True if the operation was a success false otherwise.
*/
public function set($key, $data) {
return $this->connection->set($key, $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
return $this->connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
}
/**
@@ -245,7 +266,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
public function set_many(array $keyvaluearray) {
$count = 0;
foreach ($keyvaluearray as $pair) {
if ($this->connection->set($pair['key'], $pair['value'], MEMCACHE_COMPRESSED, $this->definition->get_ttl())) {
if ($this->connection->set($this->parse_key($pair['key']), $pair['value'], MEMCACHE_COMPRESSED, $this->definition->get_ttl())) {
$count++;
}
}
@@ -259,7 +280,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
* @return bool Returns true if the operation was a success, false otherwise.
*/
public function delete($key) {
return $this->connection->delete($key);
return $this->connection->delete($this->parse_key($key));
}
/**