Commit 1d4cd8b1 authored by Jeremy's avatar Jeremy

Issue 1613622 by Jeremy, markpavlitski, tzakrajs: properly hash keys >250 characters

parent fcd303ac
......@@ -225,6 +225,17 @@ array of settings.php:
$conf['memcache_key_prefix'] = 'something_unique';
Note: if the length of your prefix + key + bin combine to be more than 250
characters, they will be automatically hashed. Memcache only supports key
lengths up to 250 bytes. You can optionally configure the hashing algorithm
used, however sha1 was selected as the default because it performs quickly with
minimal collisions.
$conf['memcache_key_hash_algorithm'] = 'sha1';
Visit http://www.php.net/manual/en/function.hash-algos.php to learn more about
which hash algorithms are available.
## MULTIPLE SERVERS ##
To use this module with multiple memcached servers, it is important that you set
......
......@@ -452,10 +452,11 @@ function dmemcache_key($key, $bin = 'cache') {
$full_key = urlencode($prefix . $bin . '-' . $key);
// Memcache only supports key lengths up to 250 bytes. If we have generated
// a longer key, hash it with sha1 which will shrink the key down to 40 bytes
// while still keeping it unique.
// a longer key, we shrink it to an acceptible length with a configurable
// hashing algorithm. Sha1 was selected as the default as it performs
// quickly with minimal collisions.
if (strlen($full_key) > 250) {
$full_key = $prefix . $bin . '-' . sha1($key);
$full_key = urlencode(hash(variable_get('memcache_key_hash_algorithm', 'sha1'), $prefix . $bin . '-' . $key));
}
return $full_key;
......
......@@ -158,13 +158,27 @@ class MemCacheSavingCase extends MemcacheTestCase {
$this->assertTrue(isset($cache->data) && $cache->data == $test_object, t('Object is saved and restored properly.'));
}
/**
* Test save and restoring a string with a long key.
*/
function testStringLongKey() {
$this->checkVariable($this->randomName(100), 'ThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydogThequickbrownfoxjumpsoverthelazydog');
}
/**
* Test save and restoring a string using a key with special characters.
*/
function testStringSpecialKey() {
$this->checkVariable($this->randomName(100), 'Qwerty!@#$%^&*()_+-=[]\;\',./<>?:"{}|£¢');
}
/*
* Check or a variable is stored and restored properly.
**/
function checkVariable($var) {
cache_set('test_var', $var, 'cache');
$cache = cache_get('test_var', 'cache');
$this->assertTrue(isset($cache->data) && $cache->data === $var, t('@type is saved and restored properly.', array('@type' => ucfirst(gettype($var)))));
function checkVariable($var, $key = 'test_var') {
cache_set($key, $var, 'cache');
$cache = cache_get($key, 'cache');
$this->assertTrue(isset($cache->data) && $cache->data === $var, t('@type is saved and restored properly!key.', array('@type' => ucfirst(gettype($var)), '!key' => ($key != 'test_var') ? t(' with key @key', array('@key' => $key)) : '')));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment