Skip to content

Commit

Permalink
Cache: TAGS in dependencies are converted to array list [Closes #46]
Browse files Browse the repository at this point in the history
  • Loading branch information
hubipe authored and dg committed Oct 6, 2016
1 parent 19d65f7 commit 7da6cc6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion tests/Caching/Cache.bulkLoad.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Caching/Cache.load.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
4 changes: 2 additions & 2 deletions tests/Caching/Cache.save.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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';
Expand Down
33 changes: 29 additions & 4 deletions tests/Storages/FileStorage.tags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
2 changes: 1 addition & 1 deletion tests/Storages/IJournalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]));
}


Expand Down

0 comments on commit 7da6cc6

Please sign in to comment.