Skip to content

Commit 4356dc6

Browse files
authored
fix(memcached): produce legal cache keys (#4892)
1 parent 49cb906 commit 4356dc6

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

caches/MemcachedCache.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222

2323
public function get(string $key, $default = null)
2424
{
25-
$value = $this->conn->get($key);
25+
$value = $this->conn->get($this->createCacheKey($key));
2626
if ($value === false) {
2727
return $default;
2828
}
@@ -36,22 +36,23 @@ public function set(string $key, $value, $ttl = null): void
3636
}
3737

3838
$expiration = $ttl === null ? 0 : time() + $ttl; // if ttl not provided, store forever
39-
$result = $this->conn->set($key, $value, $expiration);
39+
$result = $this->conn->set($this->createCacheKey($key), $value, $expiration);
4040
if ($result === false) {
4141
$this->logger->warning('Failed to store an item in memcached', [
42-
'key' => $key,
43-
'resultCode' => $this->conn->getResultCode(),
44-
'code' => $this->conn->getLastErrorCode(),
45-
'message' => $this->conn->getLastErrorMessage(),
46-
'number' => $this->conn->getLastErrorErrno(),
42+
'key' => $this->createCacheKey($key),
43+
'resultCode' => $this->conn->getResultCode(),
44+
'resultMessage' => $this->conn->getResultMessage(),
45+
'errorCode' => $this->conn->getLastErrorCode(),
46+
'errorMessage' => $this->conn->getLastErrorMessage(),
47+
'errorNumber' => $this->conn->getLastErrorErrno(),
4748
]);
4849
// Intentionally not throwing an exception
4950
}
5051
}
5152

5253
public function delete(string $key): void
5354
{
54-
$this->conn->delete($key);
55+
$this->conn->delete($this->createCacheKey($key));
5556
}
5657

5758
public function clear(): void
@@ -63,4 +64,9 @@ public function prune(): void
6364
{
6465
// memcached manages pruning on its own
6566
}
67+
68+
private function createCacheKey(string $key): string
69+
{
70+
return hash('sha1', $key);
71+
}
6672
}

0 commit comments

Comments
 (0)