From 7da6cc65f301e220acba4fd8a11d3afdc642be9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Hub=C3=ADk?= Date: Wed, 5 Oct 2016 20:08:21 +0200 Subject: [PATCH] Cache: TAGS in dependencies are converted to array list [Closes #46] --- src/Caching/Cache.php | 11 +++++++++- tests/Caching/Cache.bulkLoad.phpt | 2 +- tests/Caching/Cache.load.phpt | 2 +- tests/Caching/Cache.save.phpt | 4 ++-- tests/Storages/FileStorage.tags.phpt | 33 ++++++++++++++++++++++++---- tests/Storages/IJournalTestCase.php | 2 +- 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php index b30c4708..48b263c6 100644 --- a/src/Caching/Cache.php +++ b/src/Caching/Cache.php @@ -199,6 +199,11 @@ private function completeDependencies($dp) $dp[self::EXPIRATION] = Nette\Utils\DateTime::from($dp[self::EXPIRATION])->format('U') - time(); } + // make list from TAGS + if (isset($dp[self::TAGS])) { + $dp[self::TAGS] = array_values((array) $dp[self::TAGS]); + } + // convert FILES into CALLBACKS if (isset($dp[self::FILES])) { foreach (array_unique((array) $dp[self::FILES]) as $item) { @@ -248,7 +253,11 @@ public function remove($key) */ public function clean(array $conditions = NULL) { - $this->storage->clean((array) $conditions); + $conditions = (array) $conditions; + if (isset($conditions[self::TAGS])) { + $conditions[self::TAGS] = array_values((array) $conditions[self::TAGS]); + } + $this->storage->clean($conditions); } diff --git a/tests/Caching/Cache.bulkLoad.phpt b/tests/Caching/Cache.bulkLoad.phpt index 10fe2b74..00d695e7 100644 --- a/tests/Caching/Cache.bulkLoad.phpt +++ b/tests/Caching/Cache.bulkLoad.phpt @@ -46,7 +46,7 @@ test(function () { test(function () { $storage = new BulkReadTestStorage; $cache = new Cache($storage, 'ns'); - $dependencies = [Cache::TAGS => 'tag']; + $dependencies = [Cache::TAGS => ['tag']]; $cache->bulkLoad([1], function ($key, & $deps) use ($dependencies) { $deps = $dependencies; return $key; diff --git a/tests/Caching/Cache.load.phpt b/tests/Caching/Cache.load.phpt index bcf52c5b..a76a5d26 100644 --- a/tests/Caching/Cache.load.phpt +++ b/tests/Caching/Cache.load.phpt @@ -29,7 +29,7 @@ Assert::equal('value', $data['data']); // load twice with closure fallback, pass dependencies -$dependencies = [Cache::TAGS => 'tag']; +$dependencies = [Cache::TAGS => ['tag']]; $storage = new TestStorage(); $cache = new Cache($storage, 'ns'); diff --git a/tests/Caching/Cache.save.phpt b/tests/Caching/Cache.save.phpt index c7630d01..15dc01fe 100644 --- a/tests/Caching/Cache.save.phpt +++ b/tests/Caching/Cache.save.phpt @@ -16,7 +16,7 @@ require __DIR__ . '/Cache.php'; // save value with dependencies $storage = new testStorage(); $cache = new Cache($storage, 'ns'); -$dependencies = [Cache::TAGS => 'tag']; +$dependencies = [Cache::TAGS => ['tag']]; $cache->save('key', 'value', $dependencies); @@ -41,7 +41,7 @@ Assert::equal([], $res['dependencies']); // save callback return value with dependencies $storage = new testStorage(); $cache = new Cache($storage, 'ns'); -$dependencies = [Cache::TAGS => 'tag']; +$dependencies = [Cache::TAGS => ['tag']]; $cache->save('key', function () { return 'value'; diff --git a/tests/Storages/FileStorage.tags.phpt b/tests/Storages/FileStorage.tags.phpt index 08065ca5..616077a5 100644 --- a/tests/Storages/FileStorage.tags.phpt +++ b/tests/Storages/FileStorage.tags.phpt @@ -27,18 +27,43 @@ $cache->save('key2', 'value2', [ ]); $cache->save('key3', 'value3', [ + Cache::TAGS => ['foo' => 'one', 'bar' => 'two'], +]); + +$cache->save('key4', 'value4', [ + Cache::TAGS => 'one', +]); + +$cache->save('key5', 'value5', [ Cache::TAGS => ['two', 'three'], ]); -$cache->save('key4', 'value4'); +$cache->save('key6', 'value6', [ + Cache::TAGS => ['foo' => 'two', 'bar' => 'three'], +]); + +$cache->save('key7', 'value7', [ + Cache::TAGS => 'two', +]); + +$cache->save('key8', 'value8'); // Cleaning by tags... $cache->clean([ - Cache::TAGS => 'one', + Cache::TAGS => [ + 0 => 'non-existent1', + 1 => 'non-existent2', + 3 => 'one', + 5 => 'non-existent3' + ] ]); Assert::null($cache->load('key1')); Assert::null($cache->load('key2')); -Assert::truthy($cache->load('key3')); -Assert::truthy($cache->load('key4')); +Assert::null($cache->load('key3')); +Assert::null($cache->load('key4')); +Assert::truthy($cache->load('key5')); +Assert::truthy($cache->load('key6')); +Assert::truthy($cache->load('key7')); +Assert::truthy($cache->load('key8')); diff --git a/tests/Storages/IJournalTestCase.php b/tests/Storages/IJournalTestCase.php index 3171f4c4..aa6fb0f7 100644 --- a/tests/Storages/IJournalTestCase.php +++ b/tests/Storages/IJournalTestCase.php @@ -270,7 +270,7 @@ final public function testCleanAll() Assert::null($this->journal->clean([Cache::ALL => TRUE])); Assert::same([ - ], $this->journal->clean([Cache::TAGS => 'test:all'])); + ], $this->journal->clean([Cache::TAGS => ['test:all']])); }