Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-40903 cache: Unittests and small fixes for persistent application…

… cache
  • Loading branch information...
commit 9e72a16e142e347322dd70edaab834aa53835af7 1 parent 90317c2
@marinaglancy marinaglancy authored samhemelryk committed
View
11 cache/classes/loaders.php
@@ -404,6 +404,9 @@ public function get_many(array $keys, $strictness = IGNORE_MISSING) {
if ($value instanceof cache_cached_object) {
$value = $value->restore_object();
}
+ if ($value !== false && $this->is_using_persist_cache()) {
+ $this->set_in_persist_cache($key, $value);
+ }
$resultstore[$key] = $value;
}
}
@@ -800,6 +803,10 @@ public function delete_many(array $keys, $recurse = true) {
public function purge() {
// 1. Purge the persist cache.
$this->persistcache = array();
+ if ($this->persistmaxsize !== false) {
+ $this->persistkeys = array();
+ $this->persistcount = 0;
+ }
// 2. Purge the store.
$this->store->purge();
// 3. Optionally pruge any stacked loaders.
@@ -1006,6 +1013,10 @@ protected function set_in_persist_cache($key, $data) {
if ($key === (array)$key) {
$key = $key['key'];
}
+ if ($this->persistmaxsize !== false && isset($this->persistkeys[$key])) {
+ $this->persistcount--;
+ unset($this->persistkeys[$key]);
+ }
$this->persistcache[$key] = $data;
if ($this->persistmaxsize !== false) {
$this->persistcount++;
View
158 cache/tests/cache_test.php
@@ -1449,4 +1449,162 @@ public function test_defaults_support_searching() {
$this->assertInstanceOf('cache_request', $cache);
$this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
}
+
+ public function test_persistent_cache() {
+ $instance = cache_config_phpunittest::instance();
+ $instance->phpunit_add_definition('phpunit/persistentapp', array(
+ 'mode' => cache_store::MODE_APPLICATION,
+ 'component' => 'phpunit',
+ 'area' => 'persistentapp',
+ 'persistentdata' => true,
+ 'persistentmaxsize' => 3,
+ ));
+ $instance->phpunit_add_definition('phpunit/persistentapp2', array(
+ 'mode' => cache_store::MODE_APPLICATION,
+ 'component' => 'phpunit',
+ 'area' => 'persistentapp2',
+ 'persistentdata' => true,
+ 'persistentmaxsize' => 3,
+ ));
+ $instance->phpunit_add_definition('phpunit/persistentapp3', array(
+ 'mode' => cache_store::MODE_APPLICATION,
+ 'component' => 'phpunit',
+ 'area' => 'persistentapp3',
+ 'persistentdata' => true,
+ 'persistentmaxsize' => 3,
+ ));
+ $instance->phpunit_add_definition('phpunit/persistentapp4', array(
+ 'mode' => cache_store::MODE_APPLICATION,
+ 'component' => 'phpunit',
+ 'area' => 'persistentapp3',
+ 'persistentdata' => true,
+ 'persistentmaxsize' => 4,
+ ));
+ $cache = cache::make('phpunit', 'persistentapp');
+ $this->assertInstanceOf('cache_phpunit_application', $cache);
+
+ // Set and get three elements.
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('b', 'B'));
+ $this->assertTrue($cache->set('c', 'C'));
+ $this->assertEquals('A', $cache->get('a'));
+ $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
+
+ // Make sure all items are in persistent cache.
+ $this->assertEquals('A', $cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertEquals('B', $cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertEquals('C', $cache->phpunit_get_directly_from_persistcache('c'));
+
+ // Add new value and make sure it is in cache and it is in persistcache.
+ $this->assertTrue($cache->set('d', 'D'));
+ $this->assertEquals('D', $cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('D', $cache->get('d'));
+
+ // Now the least recent accessed item (a) is no longer in persistent cache.
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertEquals('B', $cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertEquals('C', $cache->phpunit_get_directly_from_persistcache('c'));
+
+ // Adding and deleting element.
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->delete('a'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertFalse($cache->has('a'));
+
+ // Make sure "purge" deletes from persist as well.
+ $cache->purge();
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('e'));
+
+ // Check that persistent cache holds the last accessed items by get/set.
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('b', 'B'));
+ $this->assertTrue($cache->set('c', 'C'));
+ $this->assertTrue($cache->set('d', 'D'));
+ $this->assertTrue($cache->set('e', 'E'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertEquals('C', $cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertEquals('D', $cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+
+ /** @var cache_phpunit_application $cache */
+ $cache = cache::make('phpunit', 'persistentapp2');
+ $this->assertInstanceOf('cache_phpunit_application', $cache);
+
+ // Check that persistent cache holds the last accessed items by get/set.
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('b', 'B'));
+ $this->assertTrue($cache->set('c', 'C'));
+ $this->assertTrue($cache->set('d', 'D'));
+ $this->assertTrue($cache->set('e', 'E'));
+ // Current keys in persist cache: c, d, e.
+ $this->assertEquals('C', $cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertEquals('D', $cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('b'));
+
+ $this->assertEquals('A', $cache->get('a'));
+ // Current keys in persist cache: d, e, a.
+ $this->assertEquals('D', $cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+ $this->assertEquals('A', $cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('c'));
+
+ // Current keys in persist cache: d, e, a.
+ $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
+ // Current keys in persist cache: e, a, c.
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+ $this->assertEquals('A', $cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertEquals('C', $cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('d'));
+
+
+ $cache = cache::make('phpunit', 'persistentapp3');
+ $this->assertInstanceOf('cache_phpunit_application', $cache);
+
+ // Check that persistent cache holds the last accessed items by get/set.
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('b', 'B'));
+ $this->assertTrue($cache->set('c', 'C'));
+ $this->assertTrue($cache->set('d', 'D'));
+ $this->assertTrue($cache->set('e', 'E'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertEquals('C', $cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertEquals('D', $cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+
+ $this->assertTrue($cache->set('b', 'B2'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertEquals('B2', $cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertEquals('D', $cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+
+ $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertEquals('B3', $cache->phpunit_get_directly_from_persistcache('b'));
+ $this->assertEquals('C3', $cache->phpunit_get_directly_from_persistcache('c'));
+ $this->assertFalse($cache->phpunit_get_directly_from_persistcache('d'));
+ $this->assertEquals('E', $cache->phpunit_get_directly_from_persistcache('e'));
+
+ $cache = cache::make('phpunit', 'persistentapp4');
+ $this->assertInstanceOf('cache_phpunit_application', $cache);
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertTrue($cache->set('a', 'A'));
+ $this->assertEquals('A', $cache->phpunit_get_directly_from_persistcache('a'));
+ $this->assertEquals('A', $cache->get('a'));
+ }
}
View
11 cache/tests/fixtures/lib.php
@@ -243,6 +243,17 @@ public function phpunit_get_store_class() {
public function phpunit_get_store_implements() {
return class_implements($this->get_store());
}
+
+ /**
+ * Returns the given key directly from the persistdata cache.
+ *
+ * @param string $key
+ * @return false|mixed
+ */
+ public function phpunit_get_directly_from_persistcache($key) {
+ $key = $this->parse_key($key);
+ return $this->get_from_persist_cache($key);
+ }
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.