From ff2ac15a56ce99a1880dc2fec12228f5282ff1c2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 18 Aug 2017 16:55:02 +0200 Subject: [PATCH] SQLiteStorage: changed key [WIP] --- src/Caching/Storages/SQLiteStorage.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Caching/Storages/SQLiteStorage.php b/src/Caching/Storages/SQLiteStorage.php index 55a558f1..eb409fce 100644 --- a/src/Caching/Storages/SQLiteStorage.php +++ b/src/Caching/Storages/SQLiteStorage.php @@ -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)) { @@ -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 = []; @@ -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; @@ -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)]); } @@ -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); + } }