Beispiel #1
0
 /**
  * Adds the desired form elements.
  */
 protected function configuration_definition()
 {
     $form = $this->_form;
     $form->addElement('textarea', 'servers', get_string('servers', 'cachestore_memcached'), array('cols' => 75, 'rows' => 5));
     $form->addHelpButton('servers', 'servers', 'cachestore_memcached');
     $form->addRule('servers', get_string('required'), 'required');
     $form->setType('servers', PARAM_RAW);
     $form->addElement('selectyesno', 'compression', get_string('usecompression', 'cachestore_memcached'));
     $form->addHelpButton('compression', 'usecompression', 'cachestore_memcached');
     $form->setDefault('compression', 1);
     $form->setType('compression', PARAM_BOOL);
     $serialiseroptions = cachestore_memcached::config_get_serialiser_options();
     $form->addElement('select', 'serialiser', get_string('useserialiser', 'cachestore_memcached'), $serialiseroptions);
     $form->addHelpButton('serialiser', 'useserialiser', 'cachestore_memcached');
     $form->setDefault('serialiser', Memcached::SERIALIZER_PHP);
     $form->setType('serialiser', PARAM_NUMBER);
     $form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcached'), array('size' => 16));
     $form->setType('prefix', PARAM_ALPHANUM);
     $form->addHelpButton('prefix', 'prefix', 'cachestore_memcached');
     $hashoptions = cachestore_memcached::config_get_hash_options();
     $form->addElement('select', 'hash', get_string('hash', 'cachestore_memcached'), $hashoptions);
     $form->addHelpButton('hash', 'hash', 'cachestore_memcached');
     $form->setDefault('serialiser', Memcached::HASH_DEFAULT);
     $form->setType('serialiser', PARAM_INT);
     $form->addElement('selectyesno', 'bufferwrites', get_string('bufferwrites', 'cachestore_memcached'));
     $form->addHelpButton('bufferwrites', 'bufferwrites', 'cachestore_memcached');
     $form->setDefault('bufferwrites', 0);
     $form->setType('bufferwrites', PARAM_BOOL);
 }
 /**
  * Adds the desired form elements.
  */
 protected function configuration_definition()
 {
     $form = $this->_form;
     $form->addElement('textarea', 'servers', get_string('servers', 'cachestore_memcached'), array('cols' => 75, 'rows' => 5));
     $form->addHelpButton('servers', 'servers', 'cachestore_memcached');
     $form->addRule('servers', get_string('required'), 'required');
     $form->setType('servers', PARAM_RAW);
     $form->addElement('selectyesno', 'compression', get_string('usecompression', 'cachestore_memcached'));
     $form->addHelpButton('compression', 'usecompression', 'cachestore_memcached');
     $form->setDefault('compression', 1);
     $form->setType('compression', PARAM_BOOL);
     $serialiseroptions = cachestore_memcached::config_get_serialiser_options();
     $form->addElement('select', 'serialiser', get_string('useserialiser', 'cachestore_memcached'), $serialiseroptions);
     $form->addHelpButton('serialiser', 'useserialiser', 'cachestore_memcached');
     $form->setDefault('serialiser', Memcached::SERIALIZER_PHP);
     $form->setType('serialiser', PARAM_INT);
     $form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcached'), array('size' => 16));
     $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);
     $form->addHelpButton('hash', 'hash', 'cachestore_memcached');
     $form->setDefault('serialiser', Memcached::HASH_DEFAULT);
     $form->setType('serialiser', PARAM_INT);
     $form->addElement('selectyesno', 'bufferwrites', get_string('bufferwrites', 'cachestore_memcached'));
     $form->addHelpButton('bufferwrites', 'bufferwrites', 'cachestore_memcached');
     $form->setDefault('bufferwrites', 0);
     $form->setType('bufferwrites', PARAM_BOOL);
 }
 /**
  * Adds the desired form elements.
  */
 protected function configuration_definition()
 {
     global $OUTPUT;
     $form = $this->_form;
     $version = phpversion('memcached');
     $hasrequiredversion = $version || version_compare($version, cachestore_memcached::REQUIRED_VERSION, '>=');
     if (!$hasrequiredversion) {
         $notify = new \core\output\notification(nl2br(get_string('upgrade200recommended', 'cachestore_memcached')), \core\output\notification::NOTIFY_WARNING);
         $form->addElement('html', $OUTPUT->render($notify));
     }
     $form->addElement('textarea', 'servers', get_string('servers', 'cachestore_memcached'), array('cols' => 75, 'rows' => 5));
     $form->addHelpButton('servers', 'servers', 'cachestore_memcached');
     $form->addRule('servers', get_string('required'), 'required');
     $form->setType('servers', PARAM_RAW);
     $form->addElement('selectyesno', 'compression', get_string('usecompression', 'cachestore_memcached'));
     $form->addHelpButton('compression', 'usecompression', 'cachestore_memcached');
     $form->setDefault('compression', 1);
     $form->setType('compression', PARAM_BOOL);
     $serialiseroptions = cachestore_memcached::config_get_serialiser_options();
     $form->addElement('select', 'serialiser', get_string('useserialiser', 'cachestore_memcached'), $serialiseroptions);
     $form->addHelpButton('serialiser', 'useserialiser', 'cachestore_memcached');
     $form->setDefault('serialiser', Memcached::SERIALIZER_PHP);
     $form->setType('serialiser', PARAM_INT);
     $form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcached'), array('size' => 16));
     $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\\-_]+$#');
     $form->setForceLtr('prefix');
     $hashoptions = cachestore_memcached::config_get_hash_options();
     $form->addElement('select', 'hash', get_string('hash', 'cachestore_memcached'), $hashoptions);
     $form->addHelpButton('hash', 'hash', 'cachestore_memcached');
     $form->setDefault('serialiser', Memcached::HASH_DEFAULT);
     $form->setType('serialiser', PARAM_INT);
     $form->addElement('selectyesno', 'bufferwrites', get_string('bufferwrites', 'cachestore_memcached'));
     $form->addHelpButton('bufferwrites', 'bufferwrites', 'cachestore_memcached');
     $form->setDefault('bufferwrites', 0);
     $form->setType('bufferwrites', PARAM_BOOL);
     if ($hasrequiredversion) {
         // Only show this option if we have the required version of memcache extension installed.
         // If it's not installed then this option does nothing, so there is no point in displaying it.
         $form->addElement('selectyesno', 'isshared', get_string('isshared', 'cachestore_memcached'));
         $form->addHelpButton('isshared', 'isshared', 'cachestore_memcached');
         $form->setDefault('isshared', 0);
         $form->setType('isshared', PARAM_BOOL);
     }
     $form->addElement('header', 'clusteredheader', get_string('clustered', 'cachestore_memcached'));
     $form->addElement('checkbox', 'clustered', get_string('clustered', 'cachestore_memcached'));
     $form->setDefault('checkbox', false);
     $form->addHelpButton('clustered', 'clustered', 'cachestore_memcached');
     $form->addElement('textarea', 'setservers', get_string('setservers', 'cachestore_memcached'), array('cols' => 75, 'rows' => 5));
     $form->addHelpButton('setservers', 'setservers', 'cachestore_memcached');
     $form->disabledIf('setservers', 'clustered');
     $form->setType('setservers', PARAM_RAW);
 }
 /**
  * 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);
     if (!$instance) {
         // Something prevented memcached store to be inited (extension, TEST_CACHESTORE_MEMCACHED_TESTSERVERS...).
         $this->markTestSkipped();
     }
     $keys = array('abc', 'ABC', '123', 'aB1', '1aB', 'a-1', '1-a', '-a1', 'a1-', '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);
     }
 }
Beispiel #5
0
 /**
  * Generates an instance of the cache store that can be used for testing.
  *
  * @param cache_definition $definition
  * @return cachestore_memcached|false
  */
 public static function initialise_test_instance(cache_definition $definition)
 {
     if (!self::are_requirements_met()) {
         return false;
     }
     $config = get_config('cachestore_memcached');
     if (empty($config->testservers)) {
         return false;
     }
     $configuration = array();
     $configuration['servers'] = $config->testservers;
     if (!empty($config->testcompression)) {
         $configuration['compression'] = $config->testcompression;
     }
     if (!empty($config->testserialiser)) {
         $configuration['serialiser'] = $config->testserialiser;
     }
     if (!empty($config->testprefix)) {
         $configuration['prefix'] = $config->testprefix;
     }
     if (!empty($config->testhash)) {
         $configuration['hash'] = $config->testhash;
     }
     if (!empty($config->testbufferwrites)) {
         $configuration['bufferwrites'] = $config->testbufferwrites;
     }
     $store = new cachestore_memcached('Test memcached', $configuration);
     $store->initialise($definition);
     return $store;
 }
 /**
  * Tests that memcached cache store flushes entire cache when it is using a dedicated cache.
  */
 public function test_dedicated_cache()
 {
     if (!cachestore_memcached::are_requirements_met() || !defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) {
         $this->markTestSkipped('Could not test cachestore_memcached. Requirements are not met.');
     }
     $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
     $cachestore = $this->create_test_cache_with_config($definition, array('isshared' => false));
     $connection = new Memcached(crc32(__METHOD__));
     $connection->addServers($this->get_servers(TEST_CACHESTORE_MEMCACHED_TESTSERVERS));
     $connection->setOptions(array(Memcached::OPT_COMPRESSION => true, Memcached::OPT_SERIALIZER => Memcached::SERIALIZER_PHP, Memcached::OPT_PREFIX_KEY => 'phpunit_', Memcached::OPT_BUFFER_WRITES => false));
     // We must flush first to make sure nothing is there.
     $connection->flush();
     // Test the cachestore.
     $this->assertFalse($cachestore->get('test'));
     $this->assertTrue($cachestore->set('test', 'cachestore'));
     $this->assertSame('cachestore', $cachestore->get('test'));
     // Test the connection.
     $this->assertFalse($connection->get('test'));
     $this->assertEquals(Memcached::RES_NOTFOUND, $connection->getResultCode());
     $this->assertTrue($connection->set('test', 'connection'));
     $this->assertSame('connection', $connection->get('test'));
     // Test both again and make sure the values are correct.
     $this->assertSame('cachestore', $cachestore->get('test'));
     $this->assertSame('connection', $connection->get('test'));
     // Purge the cachestore and check the connection was also purged.
     $this->assertTrue($cachestore->purge());
     $this->assertFalse($cachestore->get('test'));
     $this->assertFalse($connection->get('test'));
 }
Beispiel #7
0
 /**
  * Tests the clustering feature.
  */
 public function test_clustered()
 {
     $this->resetAfterTest(true);
     if (!defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) {
         $this->markTestSkipped();
     }
     $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHED_TESTSERVERS));
     if (count($testservers) < 2) {
         $this->markTestSkipped();
     }
     // Use the first server as our primary.
     // We need to set a prefix for all, otherwise it uses the name, which will not match between connections.
     set_config('testprefix', 'pre', 'cachestore_memcached');
     // We need to set a name, otherwise we get a reused connection.
     set_config('testname', 'cluster', 'cachestore_memcached');
     set_config('testservers', $testservers[0], 'cachestore_memcached');
     set_config('testsetservers', TEST_CACHESTORE_MEMCACHED_TESTSERVERS, 'cachestore_memcached');
     set_config('testclustered', true, 'cachestore_memcached');
     // First and instance that we can use to test the second server.
     $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
     $instance = cachestore_memcached::initialise_test_instance($definition);
     if (!$instance) {
         $this->markTestSkipped();
     }
     // Now we are going to setup a connection to each independent server.
     set_config('testclustered', false, 'cachestore_memcached');
     set_config('testsetservers', '', 'cachestore_memcached');
     $checkinstances = array();
     foreach ($testservers as $testserver) {
         // We need to set a name, otherwise we get a reused connection.
         set_config('testname', $testserver, 'cachestore_memcached');
         set_config('testservers', $testserver, 'cachestore_memcached');
         $checkinstance = cachestore_memcached::initialise_test_instance($definition);
         if (!$checkinstance) {
             $this->markTestSkipped();
         }
         $checkinstances[] = $checkinstance;
     }
     $keys = array('abc', 'ABC', '123', 'aB1', '1aB', 'a-1', '1-a', '-a1', 'a1-', 'a_1', '1_a', '_a1', 'a1_');
     // Set each key.
     foreach ($keys as $key) {
         $this->assertTrue($instance->set($key, $key), "Failed to set key `{$key}`");
     }
     // Check each key.
     foreach ($keys as $key) {
         $this->assertEquals($key, $instance->get($key), "Failed to get key `{$key}`");
         foreach ($checkinstances as $id => $checkinstance) {
             $this->assertEquals($key, $checkinstance->get($key), "Failed to get key `{$key}` from server {$id}");
         }
     }
     // Reset a key.
     $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `{$key}`");
     $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `{$key}`");
     foreach ($checkinstances as $id => $checkinstance) {
         $this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `{$key}` from server {$id}");
     }
     // Delete and check that we can't retrieve.
     foreach ($keys as $key) {
         $this->assertTrue($instance->delete($key), "Failed to delete key `{$key}`");
         $this->assertFalse($instance->get($key), "Retrieved deleted key `{$key}`");
         foreach ($checkinstances as $id => $checkinstance) {
             $this->assertFalse($checkinstance->get($key), "Retrieved deleted key `{$key}` from server {$id}");
         }
     }
     // Try set many, and check that count is correct.
     $many = array();
     foreach ($keys as $key) {
         $many[] = array('key' => $key, 'value' => $key);
     }
     $returncount = $instance->set_many($many);
     $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
     // Check keys retrieved with get_many.
     $values = $instance->get_many($keys);
     foreach ($keys as $key) {
         $this->assertTrue(isset($values[$key]), "Failed to get_many key `{$key}`");
         $this->assertEquals($key, $values[$key], "Failed to match get_many key `{$key}`");
     }
     foreach ($checkinstances as $id => $checkinstance) {
         $values = $checkinstance->get_many($keys);
         foreach ($keys as $key) {
             $this->assertTrue(isset($values[$key]), "Failed to get_many key `{$key}` from server {$id}");
             $this->assertEquals($key, $values[$key], "Failed to get_many key `{$key}` from server {$id}");
         }
     }
     // Delete many, make sure count matches.
     $returncount = $instance->delete_many($keys);
     $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
     // Check that each key was deleted.
     foreach ($keys as $key) {
         $this->assertFalse($instance->get($key), "Retrieved many deleted key `{$key}`");
         foreach ($checkinstances as $id => $checkinstance) {
             $this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `{$key}` from server {$id}");
         }
     }
     // Set the keys again.
     $returncount = $instance->set_many($many);
     $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
     // Purge.
     $this->assertTrue($instance->purge(), 'Failure to purge');
     // Delete and check that we can't retrieve.
     foreach ($keys as $key) {
         $this->assertFalse($instance->get($key), "Retrieved purged key `{$key}`");
         foreach ($checkinstances as $id => $checkinstance) {
             $this->assertFalse($checkinstance->get($key), "Retrieved purged key `{$key}` from server 2");
         }
     }
 }
Beispiel #8
0
 /**
  * Creates a test instance for unit tests if possible.
  * @param cache_definition $definition
  * @return bool|cachestore_memcached
  */
 public static function initialise_unit_test_instance(cache_definition $definition)
 {
     if (!self::are_requirements_met()) {
         return false;
     }
     if (!defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) {
         return false;
     }
     $configuration = array();
     $configuration['servers'] = explode("\n", TEST_CACHESTORE_MEMCACHED_TESTSERVERS);
     $store = new cachestore_memcached('Test memcached', $configuration);
     $store->initialise($definition);
     return $store;
 }
Beispiel #9
0
 /**
  * Generates an instance of the cache store that can be used for testing.
  *
  * @param cache_definition $definition
  * @return cachestore_memcached|false
  */
 public static function initialise_test_instance(cache_definition $definition)
 {
     if (!self::are_requirements_met()) {
         return false;
     }
     $config = get_config('cachestore_memcached');
     if (empty($config->testservers)) {
         return false;
     }
     $configuration = array();
     $configuration['servers'] = explode("\n", $config->testservers);
     if (!empty($config->testcompression)) {
         $configuration['compression'] = $config->testcompression;
     }
     if (!empty($config->testserialiser)) {
         $configuration['serialiser'] = $config->testserialiser;
     }
     if (!empty($config->testprefix)) {
         $configuration['prefix'] = $config->testprefix;
     }
     if (!empty($config->testhash)) {
         $configuration['hash'] = $config->testhash;
     }
     if (!empty($config->testbufferwrites)) {
         $configuration['bufferwrites'] = $config->testbufferwrites;
     }
     if (!empty($config->testclustered)) {
         $configuration['clustered'] = $config->testclustered;
     }
     if (!empty($config->testsetservers)) {
         $configuration['setservers'] = explode("\n", $config->testsetservers);
     }
     if (!empty($config->testname)) {
         $name = $config->testname;
     } else {
         $name = 'Test memcached';
     }
     $store = new cachestore_memcached($name, $configuration);
     // If store is ready then only initialise.
     if ($store->is_ready()) {
         $store->initialise($definition);
     }
     return $store;
 }