MDL-40700 cache: memcache and memcached stores now allow alphanumext prefixes

This commit is contained in:
Sam Hemelryk 2013-07-19 12:47:23 +12:00
parent a423acd95c
commit 7dfa10b02e
9 changed files with 62 additions and 6 deletions

View File

@ -49,8 +49,8 @@ class cachestore_memcache_addinstance_form extends cachestore_addinstance_form {
$form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcache'),
array('maxlength' => 5, 'size' => 5));
$form->addHelpButton('prefix', 'prefix', 'cachestore_memcache');
$form->addRule('prefix', get_string('storeprefixinvalid', 'cache'), 'regex', '#^[a-zA-Z0-9\-_ ]+$#');
$form->setType('prefix', PARAM_TEXT);
$form->setType('prefix', PARAM_TEXT); // We set to text but we have a rule to limit to alphanumext.
$form->setDefault('prefix', 'mdl_');
$form->addRule('prefix', get_string('prefixinvalid', 'cachestore_memcache'), 'regex', '#^[a-zA-Z0-9\-_]+$#');
}
}

View File

@ -31,6 +31,7 @@ $string['prefix'] = 'Key prefix';
$string['prefix_help'] = 'This prefix is used for all key names on the memcache server.
* If you only have one Moodle instance using this server, you can leave this value default.
* Due to key length restrictions, a maximum of 5 characters is permitted.';
$string['prefixinvalid'] = 'Invalid prefix. You can only use a-z A-Z 0-9-_.';
$string['servers'] = 'Servers';
$string['servers_help'] = 'This sets the servers that should be utilised by this memcache adapter.
Servers should be defined one per line and consist of a server address and optionally a port and weight.

View File

@ -404,7 +404,7 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
* Generates an instance of the cache store that can be used for testing.
*
* @param cache_definition $definition
* @return false
* @return cachestore_memcache|false
*/
public static function initialise_test_instance(cache_definition $definition) {
if (!self::are_requirements_met()) {

View File

@ -59,4 +59,31 @@ class cachestore_memcache_test extends cachestore_tests {
protected function get_class_name() {
return 'cachestore_memcache';
}
/**
* Tests the valid keys to ensure they work.
*/
public function test_valid_keys() {
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test');
$instance = cachestore_memcache::initialise_test_instance($definition);
$keys = array(
// Alphanumeric.
'abc', 'ABC', '123', 'aB1', '1aB',
// Hyphens.
'a-1', '1-a', '-a1', 'a1-',
// Underscores.
'a_1', '1_a', '_a1', 'a1_'
);
foreach ($keys as $key) {
$this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
}
foreach ($keys as $key) {
$this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
}
$values = $instance->get_many($keys);
foreach ($values as $key => $value) {
$this->assertEquals($key, $value);
}
}
}

View File

@ -60,8 +60,9 @@ class cachestore_memcached_addinstance_form extends cachestore_addinstance_form
$form->setType('serialiser', PARAM_NUMBER);
$form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcached'), array('size' => 16));
$form->setType('prefix', PARAM_ALPHANUM);
$form->setType('prefix', PARAM_TEXT); // We set to text but we have a rule to limit to alphanumext.
$form->addHelpButton('prefix', 'prefix', 'cachestore_memcached');
$form->addRule('prefix', get_string('prefixinvalid', 'cachestore_memcached'), 'regex', '#^[a-zA-Z0-9\-_]+$#');
$hashoptions = cachestore_memcached::config_get_hash_options();
$form->addElement('select', 'hash', get_string('hash', 'cachestore_memcached'), $hashoptions);

View File

@ -42,6 +42,7 @@ $string['hash_murmur'] = 'Murmur';
$string['pluginname'] = 'Memcached';
$string['prefix'] = 'Prefix key';
$string['prefix_help'] = 'This can be used to create a "domain" for your item keys allowing you to create multiple memcached stores on a single memcached installation. It cannot be longer than 16 characters in order to ensure key length issues are not encountered.';
$string['prefixinvalid'] = 'Invalid prefix. You can only use a-z A-Z 0-9-_.';
$string['serialiser_igbinary'] = 'The igbinary serializer.';
$string['serialiser_json'] = 'The JSON serializer.';
$string['serialiser_php'] = 'The default PHP serializer.';

View File

@ -444,7 +444,7 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
* Generates an instance of the cache store that can be used for testing.
*
* @param cache_definition $definition
* @return false
* @return cachestore_memcached|false
*/
public static function initialise_test_instance(cache_definition $definition) {

View File

@ -59,4 +59,31 @@ class cachestore_memcached_test extends cachestore_tests {
protected function get_class_name() {
return 'cachestore_memcached';
}
/**
* Tests the valid keys to ensure they work.
*/
public function test_valid_keys() {
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
$instance = cachestore_memcached::initialise_test_instance($definition);
$keys = array(
// Alphanumeric.
'abc', 'ABC', '123', 'aB1', '1aB',
// Hyphens.
'a-1', '1-a', '-a1', 'a1-',
// Underscores.
'a_1', '1_a', '_a1', 'a1_'
);
foreach ($keys as $key) {
$this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
}
foreach ($keys as $key) {
$this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
}
$values = $instance->get_many($keys);
foreach ($values as $key => $value) {
$this->assertEquals($key, $value);
}
}
}

View File

@ -141,7 +141,6 @@ $string['storename_help'] = 'This sets the store name. It is used to identify th
$string['storenamealreadyused'] = 'You must choose a unique name for this store.';
$string['storenameinvalid'] = 'Invalid store name. You can only use a-z A-Z 0-9 -_ and spaces.';
$string['storeperformance'] = 'Cache store performance reporting - {$a} unique requests per operation.';
$string['storeprefixinvalid'] = 'Invalid store prefix. You can only use a-z A-Z 0-9 -_ and spaces.';
$string['storeready'] = 'Ready';
$string['storenotready'] = 'Store not ready';
$string['storerequiresattention'] = 'Requires attention.';