Skip to content
Browse files

MDL-38205 cache: implemented key prefix in memcache plugin

  • Loading branch information...
1 parent 19075a2 commit 5a9b867cecb6155fc1040994fdcc0aa6ef36910f Sam Hemelryk committed Feb 26, 2013
Showing with 31 additions and 10 deletions.
  1. +31 −10 cache/stores/memcache/lib.php
View
41 cache/stores/memcache/lib.php
@@ -117,7 +117,7 @@ public function __construct($name, array $configuration = array()) {
$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);
}
/**
@@ -192,13 +192,27 @@ public static function get_supported_modes(array $configuration = array()) {
}
/**
+ * 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.
*
* @param string $key The key to retrieve
* @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 @@ public function get($key) {
* 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 @@ public function get_many($keys) {
* @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 @@ public function set($key, $data) {
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 @@ public function set_many(array $keyvaluearray) {
* @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));
}
/**

0 comments on commit 5a9b867

Please sign in to comment.
Something went wrong with that request. Please try again.