Skip to content

Commit ff2ac15

Browse files
committed
SQLiteStorage: changed key [WIP]
1 parent 5d5f14f commit ff2ac15

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Caching/Storages/SQLiteStorage.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function __construct($path)
6060
*/
6161
public function read(string $key)
6262
{
63+
$key = self::sanitize($key);
6364
$stmt = $this->pdo->prepare('SELECT data, slide FROM cache WHERE key=? AND (expire IS NULL OR expire >= ?)');
6465
$stmt->execute([$key, time()]);
6566
if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
@@ -77,6 +78,7 @@ public function read(string $key)
7778
*/
7879
public function bulkRead(array $keys): array
7980
{
81+
$keys = array_map([self::class, 'sanitize'], $keys);
8082
$stmt = $this->pdo->prepare('SELECT key, data, slide FROM cache WHERE key IN (?' . str_repeat(',?', count($keys) - 1) . ') AND (expire IS NULL OR expire >= ?)');
8183
$stmt->execute(array_merge($keys, [time()]));
8284
$result = [];
@@ -108,6 +110,7 @@ public function lock(string $key): void
108110
*/
109111
public function write(string $key, $data, array $dependencies): void
110112
{
113+
$key = self::sanitize($key);
111114
$expire = isset($dependencies[Cache::EXPIRATION]) ? $dependencies[Cache::EXPIRATION] + time() : null;
112115
$slide = isset($dependencies[Cache::SLIDING]) ? $dependencies[Cache::EXPIRATION] : null;
113116

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

139142

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

166169
$this->pdo->prepare($sql)->execute($args);
167170
}
171+
172+
173+
private function sanitize($key)
174+
{
175+
return str_replace(Cache::NAMESPACE_SEPARATOR, "\x01", $key);
176+
}
168177
}

0 commit comments

Comments
 (0)