1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-02 12:57:35 +01:00

Merge pull request #831 from Zankaria/redis-cache-patch

Redis cache improvements
This commit is contained in:
Lorenzo Yario 2024-10-29 19:03:28 -07:00 committed by GitHub
commit 4ccb5862e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,6 +14,9 @@ class RedisCacheDriver implements CacheDriver {
if ($password) { if ($password) {
$this->inner->auth($password); $this->inner->auth($password);
} }
if (!$this->inner->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_JSON)) {
throw new \RuntimeException('Unable to configure Redis serializer');
}
if (!$this->inner->select($database)) { if (!$this->inner->select($database)) {
throw new \RuntimeException('Unable to connect to Redis!'); throw new \RuntimeException('Unable to connect to Redis!');
} }
@ -26,15 +29,18 @@ class RedisCacheDriver implements CacheDriver {
if ($ret === false) { if ($ret === false) {
return null; return null;
} }
return \json_decode($ret, true); if ($ret === null) {
return false;
}
return $ret;
} }
public function set(string $key, mixed $value, mixed $expires = false): void { public function set(string $key, mixed $value, mixed $expires = false): void {
$value = $value === false ? null : $value;
if ($expires === false) { if ($expires === false) {
$this->inner->set($this->prefix . $key, \json_encode($value)); $this->inner->set($this->prefix . $key, $value);
} else { } else {
$expires = $expires * 1000; // Seconds to milliseconds. $this->inner->setEx($this->prefix . $key, $expires, $value);
$this->inner->setex($this->prefix . $key, $expires, \json_encode($value));
} }
} }
@ -43,6 +49,10 @@ class RedisCacheDriver implements CacheDriver {
} }
public function flush(): void { public function flush(): void {
$this->inner->flushDB(); if (empty($this->prefix)) {
$this->inner->flushDB();
} else {
$this->inner->unlink($this->inner->keys("{$this->prefix}*"));
}
} }
} }