Skip to content

Commit

Permalink
added Cache::NAMESPACES and support for cleaning namespace in FileSto…
Browse files Browse the repository at this point in the history
…rage [Closes #52]
  • Loading branch information
Jirka F authored and dg committed Aug 18, 2017
1 parent fb5bc4e commit 997d3b9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Cache
ITEMS = 'items',
CONSTS = 'consts',
CALLBACKS = 'callbacks',
NAMESPACES = 'namespaces',
ALL = 'all';

/** @internal */
Expand Down Expand Up @@ -208,6 +209,11 @@ private function completeDependencies($dp)
$dp[self::TAGS] = array_values((array) $dp[self::TAGS]);
}

// make list from NAMESPACES
if (isset($dp[self::NAMESPACES])) {
$dp[self::NAMESPACES] = array_values((array) $dp[self::NAMESPACES]);
}

// convert FILES into CALLBACKS
if (isset($dp[self::FILES])) {
foreach (array_unique((array) $dp[self::FILES]) as $item) {
Expand Down
12 changes: 12 additions & 0 deletions src/Caching/Storages/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public function clean(array $conditions)
{
$all = !empty($conditions[Cache::ALL]);
$collector = empty($conditions);
$namespaces = isset($conditions[Cache::NAMESPACES]) ? $conditions[Cache::NAMESPACES] : null;

// cleaning using file iterator
if ($all || $collector) {
Expand Down Expand Up @@ -296,6 +297,17 @@ public function clean(array $conditions)
$this->journal->clean($conditions);
}
return;

} elseif ($namespaces) {
foreach ($namespaces as $namespace) {
$dir = $this->dir . '/_' . urlencode($namespace);
if (is_dir($dir)) {
foreach (Nette\Utils\Finder::findFiles('_*')->in($dir) as $entry) {
$this->delete((string) $entry);
}
@rmdir($dir); // may already contain new files
}
}
}

// cleaning using journal
Expand Down
93 changes: 93 additions & 0 deletions tests/Storages/FileStorage.clean-namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Test: Nette\Caching\Storages\FileStorage clean with Cache::NAMESPACE
*/

declare(strict_types=1);

use Nette\Caching\Cache;
use Nette\Caching\Storages\FileStorage;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

$storage = new FileStorage(TEMP_DIR);

/*
* Create filestorage cache without namespace and some with namespaces
*/
$cacheA = new Cache($storage);
$cacheB = new Cache($storage, 'B');
$cacheC = new Cache($storage, 'C');
$cacheD = new Cache($storage, 'D');

/*
* Fill with data
*/
$cacheA->save('test1', 'David');
$cacheA->save('test2', 'Grudl');

$cacheB->save('test1', 'Barry');
$cacheB->save('test2', 'Allen');

$cacheC->save('test1', 'Oliver');
$cacheC->save('test2', 'Queen');

$cacheD->save('test1', 'Bruce');
$cacheD->save('test2', 'Wayne');


/*
* Check if fill wass successfull
*/
Assert::same('David', $cacheA->load('test1'));
Assert::same('Grudl', $cacheA->load('test2'));

Assert::same('Barry', $cacheB->load('test1'));
Assert::same('Allen', $cacheB->load('test2'));

Assert::same('Oliver', $cacheC->load('test1'));
Assert::same('Queen', $cacheC->load('test2'));

Assert::same('Bruce', $cacheD->load('test1'));
Assert::same('Wayne', $cacheD->load('test2'));


/*
* Clean one namespace
*/
$storage->clean([Cache::NAMESPACES => ['B']]);

Assert::same('David', $cacheA->load('test1'));
Assert::same('Grudl', $cacheA->load('test2'));

// Only these should be null now
Assert::null($cacheB->load('test1'));
Assert::null($cacheB->load('test2'));

Assert::same('Oliver', $cacheC->load('test1'));
Assert::same('Queen', $cacheC->load('test2'));

Assert::same('Bruce', $cacheD->load('test1'));
Assert::same('Wayne', $cacheD->load('test2'));


/*
* Test cleaning multiple namespaces
*/
$storage->clean([Cache::NAMESPACES => ['C', 'D']]);

Assert::same('David', $cacheA->load('test1'));
Assert::same('Grudl', $cacheA->load('test2'));

// All other should be null
Assert::null($cacheB->load('test1'));
Assert::null($cacheB->load('test2'));

Assert::null($cacheC->load('test1'));
Assert::null($cacheC->load('test2'));

Assert::null($cacheD->load('test1'));
Assert::null($cacheD->load('test2'));

0 comments on commit 997d3b9

Please sign in to comment.