/**
  * Test the maxsize option.
  */
 public function test_maxsize()
 {
     $defid = 'phpunit/testmaxsize';
     $config = cache_config_phpunittest::instance();
     $config->phpunit_add_definition($defid, array('mode' => cache_store::MODE_REQUEST, 'component' => 'phpunit', 'area' => 'testmaxsize', 'maxsize' => 3));
     $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
     $instance = cachestore_static::initialise_test_instance($definition);
     $this->assertTrue($instance->set('key1', 'value1'));
     $this->assertTrue($instance->set('key2', 'value2'));
     $this->assertTrue($instance->set('key3', 'value3'));
     $this->assertTrue($instance->has('key1'));
     $this->assertTrue($instance->has('key2'));
     $this->assertTrue($instance->has('key3'));
     $this->assertTrue($instance->set('key4', 'value4'));
     $this->assertTrue($instance->set('key5', 'value5'));
     $this->assertFalse($instance->has('key1'));
     $this->assertFalse($instance->has('key2'));
     $this->assertTrue($instance->has('key3'));
     $this->assertTrue($instance->has('key4'));
     $this->assertTrue($instance->has('key5'));
     $this->assertFalse($instance->get('key1'));
     $this->assertFalse($instance->get('key2'));
     $this->assertEquals('value3', $instance->get('key3'));
     $this->assertEquals('value4', $instance->get('key4'));
     $this->assertEquals('value5', $instance->get('key5'));
     // Test adding one more.
     $this->assertTrue($instance->set('key6', 'value6'));
     $this->assertFalse($instance->get('key3'));
     // Test reducing and then adding to make sure we don't lost one.
     $this->assertTrue($instance->delete('key6'));
     $this->assertTrue($instance->set('key7', 'value7'));
     $this->assertEquals('value4', $instance->get('key4'));
     // Set the same key three times to make sure it doesn't count overrides.
     for ($i = 0; $i < 3; $i++) {
         $this->assertTrue($instance->set('key8', 'value8'));
     }
     $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
     // Test adding many.
     $this->assertEquals(3, $instance->set_many(array(array('key' => 'keyA', 'value' => 'valueA'), array('key' => 'keyB', 'value' => 'valueB'), array('key' => 'keyC', 'value' => 'valueC'))));
     $this->assertEquals(array('key4' => false, 'key5' => false, 'key6' => false, 'key7' => false, 'keyA' => 'valueA', 'keyB' => 'valueB', 'keyC' => 'valueC'), $instance->get_many(array('key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC')));
 }
 /**
  * Returns an array of default stores for use.
  *
  * @return array
  */
 protected static function get_default_stores()
 {
     global $CFG;
     require_once $CFG->dirroot . '/cache/stores/file/lib.php';
     require_once $CFG->dirroot . '/cache/stores/session/lib.php';
     require_once $CFG->dirroot . '/cache/stores/static/lib.php';
     return array('default_application' => array('name' => 'default_application', 'plugin' => 'file', 'configuration' => array(), 'features' => cachestore_file::get_supported_features(), 'modes' => cachestore_file::get_supported_modes(), 'default' => true), 'default_session' => array('name' => 'default_session', 'plugin' => 'session', 'configuration' => array(), 'features' => cachestore_session::get_supported_features(), 'modes' => cachestore_session::get_supported_modes(), 'default' => true), 'default_request' => array('name' => 'default_request', 'plugin' => 'static', 'configuration' => array(), 'features' => cachestore_static::get_supported_features(), 'modes' => cachestore_static::get_supported_modes(), 'default' => true));
 }
示例#3
0
 /**
  * Creates the default configuration and saves it.
  *
  * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
  * be called when there is no configuration file already.
  *
  * @return true|array Returns true if the default configuration was successfully created.
  *     Returns a configuration array if it could not be saved. This is a bad situation. Check your error logs.
  */
 public static function create_default_configuration()
 {
     global $CFG;
     // HACK ALERT.
     // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
     // default store plugins are protected from deletion.
     require_once $CFG->dirroot . '/cache/stores/file/lib.php';
     require_once $CFG->dirroot . '/cache/stores/session/lib.php';
     require_once $CFG->dirroot . '/cache/stores/static/lib.php';
     $writer = new self();
     $writer->configstores = array('default_application' => array('name' => 'default_application', 'plugin' => 'file', 'configuration' => array(), 'features' => cachestore_file::get_supported_features(), 'modes' => cache_store::MODE_APPLICATION, 'default' => true), 'default_session' => array('name' => 'default_session', 'plugin' => 'session', 'configuration' => array(), 'features' => cachestore_session::get_supported_features(), 'modes' => cache_store::MODE_SESSION, 'default' => true), 'default_request' => array('name' => 'default_request', 'plugin' => 'static', 'configuration' => array(), 'features' => cachestore_static::get_supported_features(), 'modes' => cache_store::MODE_REQUEST, 'default' => true));
     $writer->configdefinitions = self::locate_definitions();
     $writer->configmodemappings = array(array('mode' => cache_store::MODE_APPLICATION, 'store' => 'default_application', 'sort' => -1), array('mode' => cache_store::MODE_SESSION, 'store' => 'default_session', 'sort' => -1), array('mode' => cache_store::MODE_REQUEST, 'store' => 'default_request', 'sort' => -1));
     $writer->configlocks = array('default_file_lock' => array('name' => 'cachelock_file_default', 'type' => 'cachelock_file', 'dir' => 'filelocks', 'default' => true));
     $factory = cache_factory::instance();
     // We expect the cache to be initialising presently. If its not then something has gone wrong and likely
     // we are now in a loop.
     if ($factory->get_state() !== cache_factory::STATE_INITIALISING) {
         return $writer->generate_configuration_array();
     }
     $factory->set_state(cache_factory::STATE_SAVING);
     $writer->config_save();
     return true;
 }
示例#4
0
文件: lib.php 项目: lucaboesch/moodle
 /**
  * Generates an instance of the cache store that can be used for testing.
  *
  * @param cache_definition $definition
  * @return cachestore_static
  */
 public static function initialise_test_instance(cache_definition $definition)
 {
     // Do something here perhaps.
     $cache = new cachestore_static('Static store');
     $cache->initialise($definition);
     return $cache;
 }
示例#5
0
 /**
  * Creates the default configuration and saves it.
  *
  * @param bool $forcesave Ignored because we are disabled!
  * @return array
  */
 public static function create_default_configuration($forcesave = false)
 {
     global $CFG;
     // HACK ALERT.
     // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
     // default store plugins are protected from deletion.
     require_once $CFG->dirroot . '/cache/stores/file/lib.php';
     require_once $CFG->dirroot . '/cache/stores/session/lib.php';
     require_once $CFG->dirroot . '/cache/stores/static/lib.php';
     $writer = new self();
     $writer->configstores = array('default_application' => array('name' => 'default_application', 'plugin' => 'file', 'configuration' => array(), 'features' => cachestore_file::get_supported_features(), 'modes' => cache_store::MODE_APPLICATION, 'default' => true), 'default_session' => array('name' => 'default_session', 'plugin' => 'session', 'configuration' => array(), 'features' => cachestore_session::get_supported_features(), 'modes' => cache_store::MODE_SESSION, 'default' => true), 'default_request' => array('name' => 'default_request', 'plugin' => 'static', 'configuration' => array(), 'features' => cachestore_static::get_supported_features(), 'modes' => cache_store::MODE_REQUEST, 'default' => true));
     $writer->configdefinitions = array();
     $writer->configmodemappings = array(array('mode' => cache_store::MODE_APPLICATION, 'store' => 'default_application', 'sort' => -1), array('mode' => cache_store::MODE_SESSION, 'store' => 'default_session', 'sort' => -1), array('mode' => cache_store::MODE_REQUEST, 'store' => 'default_request', 'sort' => -1));
     $writer->configlocks = array('default_file_lock' => array('name' => 'cachelock_file_default', 'type' => 'cachelock_file', 'dir' => 'filelocks', 'default' => true));
     return $writer->generate_configuration_array();
 }