From 4af3d7e7a3402000b80df149de39e07473b89015 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 26 Feb 2013 16:35:45 +1300 Subject: [PATCH] MDL-38205 cache: implemented key prefix in memcache plugin --- cache/stores/memcache/lib.php | 41 ++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/cache/stores/memcache/lib.php b/cache/stores/memcache/lib.php index c457cc06e5c..906b3556e49 100644 --- a/cache/stores/memcache/lib.php +++ b/cache/stores/memcache/lib.php @@ -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)); } /**