Skip to content

Commit

Permalink
MDL-57572 cache: Adds igbinary serializer to Redis cache store
Browse files Browse the repository at this point in the history
  • Loading branch information
mwehr committed Jan 6, 2017
1 parent 8ed0851 commit 9c860ce
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
6 changes: 6 additions & 0 deletions cache/stores/redis/addinstanceform.php
Expand Up @@ -48,5 +48,11 @@ protected function configuration_definition() {
$form->setType('prefix', PARAM_TEXT); // We set to text but we have a rule to limit to alphanumext.
$form->addHelpButton('prefix', 'prefix', 'cachestore_redis');
$form->addRule('prefix', get_string('prefixinvalid', 'cachestore_redis'), 'regex', '#^[a-zA-Z0-9\-_]+$#');

$serializeroptions = cachestore_redis::config_get_serializer_options();
$form->addElement('select', 'serializer', get_string('useserializer', 'cachestore_redis'), $serializeroptions);
$form->addHelpButton('serializer', 'useserializer', 'cachestore_redis');
$form->setDefault('serializer', Redis::SERIALIZER_PHP);
$form->setType('serializer', PARAM_INT);
}
}
12 changes: 10 additions & 2 deletions cache/stores/redis/lang/en/cachestore_redis.php
Expand Up @@ -30,7 +30,15 @@
* 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['serializer_igbinary'] = 'The igbinary serializer.';
$string['serializer_php'] = 'The default PHP serializer.';
$string['server'] = 'Server';
$string['server_help'] = 'This sets the hostname or IP address of the Redis server to use.';
$string['test_server'] = 'Test server';
$string['test_server_desc'] = 'Redis server to use for testing.';
$string['server'] = 'Server';
$string['server_help'] = 'This sets the hostname or IP address of the Redis server to use.';
$string['test_serializer'] = 'Serializer';
$string['test_serializer_desc'] = 'Serializer to use for testing.';
$string['useserializer'] = 'Use serializer';
$string['useserializer_help'] = 'Specifies the serializer to use for serializing.
The valid serializers are Redis::SERIALIZER_PHP or Redis::SERIALIZER_IGBINARY.
The latter is supported only when phpredis is configured with --enable-redis-igbinary option and the igbinary extension is loaded.';
42 changes: 39 additions & 3 deletions cache/stores/redis/lib.php
Expand Up @@ -73,6 +73,13 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
*/
protected $redis;

/**
* Serializer for this store.
*
* @var int
*/
protected $serializer = Redis::SERIALIZER_PHP;

/**
* Determines if the requirements for this type of store are met.
*
Expand Down Expand Up @@ -124,6 +131,9 @@ public function __construct($name, array $configuration = array()) {
if (!array_key_exists('server', $configuration) || empty($configuration['server'])) {
return;
}
if (array_key_exists('serializer', $configuration)) {
$this->serializer = (int)$configuration['serializer'];
}
$prefix = !empty($configuration['prefix']) ? $configuration['prefix'] : '';
$this->redis = $this->new_redis($configuration['server'], $prefix);
}
Expand All @@ -145,7 +155,7 @@ protected function new_redis($server, $prefix = '') {
$port = $serverconf[1];
}
if ($redis->connect($server, $port)) {
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$redis->setOption(Redis::OPT_SERIALIZER, $this->serializer);
if (!empty($prefix)) {
$redis->setOption(Redis::OPT_PREFIX, $prefix);
}
Expand Down Expand Up @@ -426,7 +436,11 @@ public function release_lock($key, $ownerid) {
* @return array
*/
public static function config_get_configuration_array($data) {
return array('server' => $data->server, 'prefix' => $data->prefix);
return array(
'server' => $data->server,
'prefix' => $data->prefix,
'serializer' => $data->serializer
);
}

/**
Expand All @@ -440,6 +454,9 @@ public static function config_set_edit_form_data(moodleform $editform, array $co
$data = array();
$data['server'] = $config['server'];
$data['prefix'] = !empty($config['prefix']) ? $config['prefix'] : '';
if (!empty($config['serializer'])) {
$data['serializer'] = $config['serializer'];
}
$editform->set_data($data);
}

Expand All @@ -458,7 +475,11 @@ public static function initialise_test_instance(cache_definition $definition) {
if (empty($config->test_server)) {
return false;
}
$cache = new cachestore_redis('Redis test', ['server' => $config->test_server]);
$configuration = array('server' => $config->test_server);
if (!empty($config->test_serializer)) {
$configuration['serializer'] = $config->test_serializer;
}
$cache = new cachestore_redis('Redis test', $configuration);
$cache->initialise($definition);

return $cache;
Expand Down Expand Up @@ -491,4 +512,19 @@ public static function unit_test_configuration() {
public static function ready_to_be_used_for_testing() {
return defined('TEST_CACHESTORE_REDIS_TESTSERVERS');
}

/**
* Gets an array of options to use as the serialiser.
* @return array
*/
public static function config_get_serializer_options() {
$options = array(
Redis::SERIALIZER_PHP => get_string('serializer_php', 'cachestore_redis')
);

if (defined('Redis::SERIALIZER_IGBINARY')) {
$options[Redis::SERIALIZER_IGBINARY] = get_string('serializer_igbinary', 'cachestore_redis');
}
return $options;
}
}
31 changes: 23 additions & 8 deletions cache/stores/redis/settings.php
Expand Up @@ -25,12 +25,27 @@
defined('MOODLE_INTERNAL') || die();

$settings->add(
new admin_setting_configtext(
'cachestore_redis/test_server',
get_string('test_server', 'cachestore_redis'),
get_string('test_server_desc', 'cachestore_redis'),
'',
PARAM_TEXT,
16
)
new admin_setting_configtext(
'cachestore_redis/test_server',
get_string('test_server', 'cachestore_redis'),
get_string('test_server_desc', 'cachestore_redis'),
'',
PARAM_TEXT,
16
)
);

$options = array(Redis::SERIALIZER_PHP => get_string('serializer_php', 'cachestore_redis'));

if (defined('Redis::SERIALIZER_IGBINARY')) {
$options[Redis::SERIALIZER_IGBINARY] = get_string('serializer_igbinary', 'cachestore_redis');
}

$settings->add(new admin_setting_configselect(
'cachestore_redis/test_serializer',
get_string('test_serializer', 'cachestore_redis'),
get_string('test_serializer_desc', 'cachestore_redis'),
0,
$options
)
);

0 comments on commit 9c860ce

Please sign in to comment.