try to fix some caching bugs memcached/redis

This commit is contained in:
Dennis Eichhorn 2021-10-12 00:08:55 +02:00
parent 72b23f2928
commit b53973157d
3 changed files with 62 additions and 12 deletions

View File

@ -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);

View File

@ -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();
}
/**

View File

@ -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();
}
/**