Skip to content

Commit

Permalink
SQLiteStorage: added support for cleaning namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Jirka F authored and dg committed Aug 18, 2017
1 parent dc543c2 commit 8455405
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Caching/Storages/SQLiteStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function __construct($path)
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->exec('
PRAGMA foreign_keys = ON;
PRAGMA case_sensitive_like = ON;
CREATE TABLE IF NOT EXISTS cache (
key BLOB NOT NULL PRIMARY KEY,
data BLOB NOT NULL,
Expand Down Expand Up @@ -145,19 +147,26 @@ public function clean(array $conditions): void
{
if (!empty($conditions[Cache::ALL])) {
$this->pdo->prepare('DELETE FROM cache')->execute();
return;
}

} else {
$sql = 'DELETE FROM cache WHERE expire < ?';
$args = [time()];
$sql = 'DELETE FROM cache WHERE expire < ?';
$args = [time()];

if (!empty($conditions[Cache::TAGS])) {
$tags = $conditions[Cache::TAGS];
$sql .= ' OR key IN (SELECT key FROM tags WHERE tag IN (?' . str_repeat(',?', count($tags) - 1) . '))';
$args = array_merge($args, $tags);
}
if (!empty($conditions[Cache::TAGS])) {
$tags = $conditions[Cache::TAGS];
$sql .= ' OR key IN (SELECT key FROM tags WHERE tag IN (?' . str_repeat(',?', count($tags) - 1) . '))';
$args = array_merge($args, $tags);
}

$this->pdo->prepare($sql)->execute($args);
if (!empty($conditions[Cache::NAMESPACES])) {
foreach ($conditions[Cache::NAMESPACES] as $namespace) {
$sql .= ' OR key LIKE ?';
$args[] = self::sanitize($namespace . Cache::NAMESPACE_SEPARATOR . '%');
}
}

$this->pdo->prepare($sql)->execute($args);
}


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

/**
* Test: Nette\Caching\Storages\SQLiteStorage clean with Cache::NAMESPACE
* @phpExtension pdo_sqlite
*/

declare(strict_types=1);

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


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

$storage = new SQLiteStorage(':memory:');

/*
* Create SQLiteStorage cache without namespace and some with namespaces
*/
$cacheA = new Cache($storage);
$cacheB = new Cache($storage, 'C' . Cache::NAMESPACE_SEPARATOR . 'A');
$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 => [$cacheB->getNamespace()]]);

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 => [$cacheC->getNamespace(), $cacheD->getNamespace()]]);

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 8455405

Please sign in to comment.