Skip to content

Commit f56c5e0

Browse files
hubipedg
authored andcommitted
Cache: TAGS in dependencies are converted to array list [Closes #46]
1 parent a710238 commit f56c5e0

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

src/Caching/Cache.php

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

155+
// make list from TAGS
156+
if (isset($dp[self::TAGS])) {
157+
$dp[self::TAGS] = array_values((array) $dp[self::TAGS]);
158+
}
159+
155160
// convert FILES into CALLBACKS
156161
if (isset($dp[self::FILES])) {
157162
foreach (array_unique((array) $dp[self::FILES]) as $item) {
@@ -202,7 +207,11 @@ public function remove($key)
202207
public function clean(array $conditions = NULL)
203208
{
204209
$this->key = $this->data = NULL;
205-
$this->storage->clean((array) $conditions);
210+
$conditions = (array) $conditions;
211+
if (isset($conditions[self::TAGS])) {
212+
$conditions[self::TAGS] = array_values((array) $conditions[self::TAGS]);
213+
}
214+
$this->storage->clean($conditions);
206215
}
207216

208217

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)