MDL-46151 cachestore_memcache: now checks extension version for support

This commit is contained in:
Sam Hemelryk
2014-10-14 08:27:33 +13:00
parent 6a10ec4260
commit 4ad4c19b58
3 changed files with 94 additions and 12 deletions
+37 -1
View File
@@ -87,6 +87,12 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
*/
protected $definition;
/**
* If true data going in and out will be encoded.
* @var bool
*/
protected $encode = true;
/**
* Default prefix for key names.
* @var string
@@ -150,6 +156,24 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
}
$this->definition = $definition;
$this->isinitialised = true;
$this->encode = self::require_encoding();
}
/**
* Tests if encoding is going to be required.
*
* Prior to memcache 3.0.3 scalar data types were not preserved.
* For earlier versions of the memcache extension we need to encode and decode scalar types
* to ensure that it is preserved.
*
* @param string $version The version to check, if null it is fetched from PHP.
* @return bool
*/
public static function require_encoding($version = null) {
if (!$version) {
$version = phpversion('memcache');
}
return (version_compare($version, '3.0.3', '<'));
}
/**
@@ -240,7 +264,11 @@ 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($this->parse_key($key));
$result = $this->connection->get($this->parse_key($key));
if ($this->encode && $result !== false) {
return @unserialize($result);
}
return $result;
}
/**
@@ -267,6 +295,9 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
$return[$key] = false;
} else {
$return[$key] = $result[$mkey];
if ($this->encode && $return[$key] !== false) {
$return[$key] = @unserialize($return[$key]);
}
}
}
return $return;
@@ -280,6 +311,11 @@ 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) {
if ($this->encode) {
// We must serialise this data.
$data = serialize($data);
}
return $this->connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
}