From b53973157d8edb7254286d17517e63e1a843de2b Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 12 Oct 2021 00:08:55 +0200 Subject: [PATCH] try to fix some caching bugs memcached/redis --- DataStorage/Cache/Connection/FileCache.php | 16 +++++++++ DataStorage/Cache/Connection/MemCached.php | 36 ++++++++++++++------- DataStorage/Cache/Connection/RedisCache.php | 22 ++++++++++++- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/DataStorage/Cache/Connection/FileCache.php b/DataStorage/Cache/Connection/FileCache.php index 8731efca8..90e0f91b9 100644 --- a/DataStorage/Cache/Connection/FileCache.php +++ b/DataStorage/Cache/Connection/FileCache.php @@ -454,6 +454,10 @@ final class FileCache extends ConnectionAbstract */ public function increment(int | string $key, int $value = 1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + $path = $this->getPath($key); if (!File::exists($path)) { return false; @@ -489,6 +493,10 @@ final class FileCache extends ConnectionAbstract */ public function decrement(int | string $key, int $value = 1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + $path = $this->getPath($key); if (!File::exists($path)) { return false; @@ -524,6 +532,10 @@ final class FileCache extends ConnectionAbstract */ public function rename(int | string $old, int | string $new, int $expire = -1) : void { + if ($this->status !== CacheStatus::OK) { + return; + } + $value = $this->get($old); $this->set($new, $value, $expire); $this->delete($old); @@ -628,6 +640,10 @@ final class FileCache extends ConnectionAbstract */ public function updateExpire(int | string $key, int $expire = -1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + $value = $this->get($key, $expire); $this->delete($key); $this->set($key, $value, $expire); diff --git a/DataStorage/Cache/Connection/MemCached.php b/DataStorage/Cache/Connection/MemCached.php index b481d49e2..590f77b7a 100644 --- a/DataStorage/Cache/Connection/MemCached.php +++ b/DataStorage/Cache/Connection/MemCached.php @@ -128,11 +128,7 @@ final class MemCached extends ConnectionAbstract $result = $this->con->get((string) $key); - if ($this->con->getResultCode() !== \Memcached::RES_SUCCESS) { - return null; - } - - return $result; + return $this->con->getResultCode() !== \Memcached::RES_SUCCESS ? null : $result; } /** @@ -144,7 +140,9 @@ final class MemCached extends ConnectionAbstract return false; } - return $this->con->delete((string) $key); + $result = $this->con->delete((string) $key); + + return $this->con->getResultCode() === \Memcached::RES_NOTFOUND ? false : $result; } /** @@ -164,9 +162,11 @@ final class MemCached extends ConnectionAbstract */ public function increment(int | string $key, int $value = 1) : bool { - $this->con->increment((string) $key, $value); + if ($this->status !== CacheStatus::OK) { + return false; + } - return true; + return $this->con->increment((string) $key, $value) !== false; } /** @@ -174,9 +174,11 @@ final class MemCached extends ConnectionAbstract */ public function decrement(int | string $key, int $value = 1) : bool { - $this->con->decrement((string) $key, $value); + if ($this->status !== CacheStatus::OK) { + return false; + } - return true; + return $this->con->decrement((string) $key, $value) !== false; } /** @@ -184,6 +186,10 @@ final class MemCached extends ConnectionAbstract */ public function rename(int | string $old, int | string $new, int $expire = -1) : void { + if ($this->status !== CacheStatus::OK) { + return; + } + $value = $this->get((string) $old); $this->set((string) $new, $value, $expire); $this->delete((string) $old); @@ -296,6 +302,10 @@ final class MemCached extends ConnectionAbstract */ public function updateExpire(int | string $key, int $expire = -1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + if ($expire > 0) { $this->con->touch((string) $key, $expire); } @@ -308,7 +318,11 @@ final class MemCached extends ConnectionAbstract */ public function flush(int $expire = 0) : bool { - return $this->flushAll(); + if ($this->status !== CacheStatus::OK) { + return false; + } + + return $this->flush(); } /** diff --git a/DataStorage/Cache/Connection/RedisCache.php b/DataStorage/Cache/Connection/RedisCache.php index 4576820b0..2710de130 100644 --- a/DataStorage/Cache/Connection/RedisCache.php +++ b/DataStorage/Cache/Connection/RedisCache.php @@ -184,6 +184,10 @@ final class RedisCache extends ConnectionAbstract */ public function increment(int | string $key, int $value = 1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + $this->con->incrBy((string) $key, $value); return true; @@ -194,6 +198,10 @@ final class RedisCache extends ConnectionAbstract */ public function decrement(int | string $key, int $value = 1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + $this->con->decrBy((string) $key, $value); return true; @@ -204,6 +212,10 @@ final class RedisCache extends ConnectionAbstract */ public function rename(int | string $old, int | string $new, int $expire = -1) : void { + if ($this->status !== CacheStatus::OK) { + return; + } + $this->con->rename((string) $old, (string) $new); if ($expire > 0) { @@ -263,6 +275,10 @@ final class RedisCache extends ConnectionAbstract */ public function updateExpire(int | string $key, int $expire = -1) : bool { + if ($this->status !== CacheStatus::OK) { + return false; + } + if ($expire > 0) { $this->con->expire((string) $key, $expire); } @@ -275,7 +291,11 @@ final class RedisCache extends ConnectionAbstract */ public function flush(int $expire = 0) : bool { - return $this->flushAll(); + if ($this->status !== CacheStatus::OK) { + return false; + } + + return $this->flushDb(); } /**