public function clean(array $conditions)
{
if (!$this->pdo) {
$this->open();
}
if (!empty($conditions[Cache::ALL])) {
$this->pdo->exec('
BEGIN;
DELETE FROM tags;
DELETE FROM priorities;
COMMIT;
');
return NULL;
}
$unions = $args = [];
if (!empty($conditions[Cache::TAGS])) {
$tags = (array) $conditions[Cache::TAGS];
$unions[] = 'SELECT DISTINCT key FROM tags WHERE tag IN (?' . str_repeat(', ?', count($tags) - 1) . ')';
$args = $tags;
}
if (!empty($conditions[Cache::PRIORITY])) {
$unions[] = 'SELECT DISTINCT key FROM priorities WHERE priority <= ?';
$args[] = (int) $conditions[Cache::PRIORITY];
}
if (empty($unions)) {
return [];
}
$unionSql = implode(' UNION ', $unions);
$this->pdo->exec('BEGIN IMMEDIATE');
$stmt = $this->pdo->prepare($unionSql);
$stmt->execute($args);
$keys = $stmt->fetchAll(\PDO::FETCH_COLUMN, 0);
if (empty($keys)) {
$this->pdo->exec('COMMIT');
return [];
}
$this->pdo->prepare("DELETE FROM tags WHERE key IN ({$unionSql})")->execute($args);
$this->pdo->prepare("DELETE FROM priorities WHERE key IN ({$unionSql})")->execute($args);
$this->pdo->exec('COMMIT');
return $keys;
}