Skip to content

Commit

Permalink
SQLiteStorage: changed key [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 18, 2017
1 parent 5d5f14f commit ff2ac15
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Caching/Storages/SQLiteStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function __construct($path)
*/
public function read(string $key)
{
$key = self::sanitize($key);
$stmt = $this->pdo->prepare('SELECT data, slide FROM cache WHERE key=? AND (expire IS NULL OR expire >= ?)');
$stmt->execute([$key, time()]);
if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
Expand All @@ -77,6 +78,7 @@ public function read(string $key)
*/
public function bulkRead(array $keys): array
{
$keys = array_map([self::class, 'sanitize'], $keys);
$stmt = $this->pdo->prepare('SELECT key, data, slide FROM cache WHERE key IN (?' . str_repeat(',?', count($keys) - 1) . ') AND (expire IS NULL OR expire >= ?)');
$stmt->execute(array_merge($keys, [time()]));
$result = [];
Expand Down Expand Up @@ -108,6 +110,7 @@ public function lock(string $key): void
*/
public function write(string $key, $data, array $dependencies): void
{
$key = self::sanitize($key);
$expire = isset($dependencies[Cache::EXPIRATION]) ? $dependencies[Cache::EXPIRATION] + time() : null;
$slide = isset($dependencies[Cache::SLIDING]) ? $dependencies[Cache::EXPIRATION] : null;

Expand All @@ -133,7 +136,7 @@ public function write(string $key, $data, array $dependencies): void
public function remove(string $key): void
{
$this->pdo->prepare('DELETE FROM cache WHERE key=?')
->execute([$key]);
->execute([self::sanitize($key)]);
}


Expand All @@ -159,10 +162,16 @@ public function clean(array $conditions): void
if (!empty($conditions[Cache::NAMESPACES])) {
foreach ($conditions[Cache::NAMESPACES] as $namespace) {
$sql .= ' OR key LIKE ?';
$args[] = $namespace . Cache::NAMESPACE_SEPARATOR . '%';
$args[] = self::sanitize($namespace . Cache::NAMESPACE_SEPARATOR . '%');
}
}

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


private function sanitize($key)
{
return str_replace(Cache::NAMESPACE_SEPARATOR, "\x01", $key);
}
}

0 comments on commit ff2ac15

Please sign in to comment.