Skip to content

Commit 7da6cc6

Browse files
hubipedg
authored andcommitted
Cache: TAGS in dependencies are converted to array list [Closes #46]
1 parent 19d65f7 commit 7da6cc6

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/Caching/Cache.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ private function completeDependencies($dp)
199199
$dp[self::EXPIRATION] = Nette\Utils\DateTime::from($dp[self::EXPIRATION])->format('U') - time();
200200
}
201201

202+
// make list from TAGS
203+
if (isset($dp[self::TAGS])) {
204+
$dp[self::TAGS] = array_values((array) $dp[self::TAGS]);
205+
}
206+
202207
// convert FILES into CALLBACKS
203208
if (isset($dp[self::FILES])) {
204209
foreach (array_unique((array) $dp[self::FILES]) as $item) {
@@ -248,7 +253,11 @@ public function remove($key)
248253
*/
249254
public function clean(array $conditions = NULL)
250255
{
251-
$this->storage->clean((array) $conditions);
256+
$conditions = (array) $conditions;
257+
if (isset($conditions[self::TAGS])) {
258+
$conditions[self::TAGS] = array_values((array) $conditions[self::TAGS]);
259+
}
260+
$this->storage->clean($conditions);
252261
}
253262

254263

tests/Caching/Cache.bulkLoad.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test(function () {
4646
test(function () {
4747
$storage = new BulkReadTestStorage;
4848
$cache = new Cache($storage, 'ns');
49-
$dependencies = [Cache::TAGS => 'tag'];
49+
$dependencies = [Cache::TAGS => ['tag']];
5050
$cache->bulkLoad([1], function ($key, & $deps) use ($dependencies) {
5151
$deps = $dependencies;
5252
return $key;

tests/Caching/Cache.load.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Assert::equal('value', $data['data']);
2929

3030

3131
// load twice with closure fallback, pass dependencies
32-
$dependencies = [Cache::TAGS => 'tag'];
32+
$dependencies = [Cache::TAGS => ['tag']];
3333
$storage = new TestStorage();
3434
$cache = new Cache($storage, 'ns');
3535

tests/Caching/Cache.save.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require __DIR__ . '/Cache.php';
1616
// save value with dependencies
1717
$storage = new testStorage();
1818
$cache = new Cache($storage, 'ns');
19-
$dependencies = [Cache::TAGS => 'tag'];
19+
$dependencies = [Cache::TAGS => ['tag']];
2020

2121
$cache->save('key', 'value', $dependencies);
2222

@@ -41,7 +41,7 @@ Assert::equal([], $res['dependencies']);
4141
// save callback return value with dependencies
4242
$storage = new testStorage();
4343
$cache = new Cache($storage, 'ns');
44-
$dependencies = [Cache::TAGS => 'tag'];
44+
$dependencies = [Cache::TAGS => ['tag']];
4545

4646
$cache->save('key', function () {
4747
return 'value';

tests/Storages/FileStorage.tags.phpt

+29-4
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,43 @@ $cache->save('key2', 'value2', [
2727
]);
2828

2929
$cache->save('key3', 'value3', [
30+
Cache::TAGS => ['foo' => 'one', 'bar' => 'two'],
31+
]);
32+
33+
$cache->save('key4', 'value4', [
34+
Cache::TAGS => 'one',
35+
]);
36+
37+
$cache->save('key5', 'value5', [
3038
Cache::TAGS => ['two', 'three'],
3139
]);
3240

33-
$cache->save('key4', 'value4');
41+
$cache->save('key6', 'value6', [
42+
Cache::TAGS => ['foo' => 'two', 'bar' => 'three'],
43+
]);
44+
45+
$cache->save('key7', 'value7', [
46+
Cache::TAGS => 'two',
47+
]);
48+
49+
$cache->save('key8', 'value8');
3450

3551

3652
// Cleaning by tags...
3753
$cache->clean([
38-
Cache::TAGS => 'one',
54+
Cache::TAGS => [
55+
0 => 'non-existent1',
56+
1 => 'non-existent2',
57+
3 => 'one',
58+
5 => 'non-existent3'
59+
]
3960
]);
4061

4162
Assert::null($cache->load('key1'));
4263
Assert::null($cache->load('key2'));
43-
Assert::truthy($cache->load('key3'));
44-
Assert::truthy($cache->load('key4'));
64+
Assert::null($cache->load('key3'));
65+
Assert::null($cache->load('key4'));
66+
Assert::truthy($cache->load('key5'));
67+
Assert::truthy($cache->load('key6'));
68+
Assert::truthy($cache->load('key7'));
69+
Assert::truthy($cache->load('key8'));

tests/Storages/IJournalTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ final public function testCleanAll()
270270

271271
Assert::null($this->journal->clean([Cache::ALL => TRUE]));
272272
Assert::same([
273-
], $this->journal->clean([Cache::TAGS => 'test:all']));
273+
], $this->journal->clean([Cache::TAGS => ['test:all']]));
274274
}
275275

276276

0 commit comments

Comments
 (0)