From 023b80e6173044d87fd1c928b06e740810c61144 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Julius=20H=C3=A4rtl?= Date: Thu, 17 Nov 2022 13:20:50 +0100 Subject: [PATCH] Only setup redis connection if needed MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/private/Memcache/Redis.php | 46 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/private/Memcache/Redis.php b/lib/private/Memcache/Redis.php index de38033ca32..f4094e0bef6 100644 --- a/lib/private/Memcache/Redis.php +++ b/lib/private/Memcache/Redis.php @@ -33,20 +33,28 @@ use OCP\IMemcacheTTL; class Redis extends Cache implements IMemcacheTTL { /** - * @var \Redis $cache + * @var \Redis|\RedisCluster $cache */ private static $cache = null; public function __construct($prefix = '', string $logFile = '') { parent::__construct($prefix); + } + + /** + * @return \Redis|\RedisCluster|null + * @throws \Exception + */ + public function getCache() { if (is_null(self::$cache)) { self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); } + return self::$cache; } public function get($key) { - $result = self::$cache->get($this->getPrefix() . $key); - if ($result === false && !self::$cache->exists($this->getPrefix() . $key)) { + $result = $this->getCache()->get($this->getPrefix() . $key); + if ($result === false && !$this->getCache()->exists($this->getPrefix() . $key)) { return null; } else { return json_decode($result, true); @@ -55,18 +63,18 @@ class Redis extends Cache implements IMemcacheTTL { public function set($key, $value, $ttl = 0) { if ($ttl > 0) { - return self::$cache->setex($this->getPrefix() . $key, $ttl, json_encode($value)); + return $this->getCache()->setex($this->getPrefix() . $key, $ttl, json_encode($value)); } else { - return self::$cache->set($this->getPrefix() . $key, json_encode($value)); + return $this->getCache()->set($this->getPrefix() . $key, json_encode($value)); } } public function hasKey($key) { - return (bool)self::$cache->exists($this->getPrefix() . $key); + return (bool)$this->getCache()->exists($this->getPrefix() . $key); } public function remove($key) { - if (self::$cache->del($this->getPrefix() . $key)) { + if ($this->getCache()->del($this->getPrefix() . $key)) { return true; } else { return false; @@ -75,8 +83,8 @@ class Redis extends Cache implements IMemcacheTTL { public function clear($prefix = '') { $prefix = $this->getPrefix() . $prefix . '*'; - $keys = self::$cache->keys($prefix); - $deleted = self::$cache->del($keys); + $keys = $this->getCache()->keys($prefix); + $deleted = $this->getCache()->del($keys); return (is_array($keys) && (count($keys) === $deleted)); } @@ -100,7 +108,7 @@ class Redis extends Cache implements IMemcacheTTL { $args['ex'] = $ttl; } - return self::$cache->set($this->getPrefix() . $key, $value, $args); + return $this->getCache()->set($this->getPrefix() . $key, (string)$value, $args); } /** @@ -111,7 +119,7 @@ class Redis extends Cache implements IMemcacheTTL { * @return int | bool */ public function inc($key, $step = 1) { - return self::$cache->incrBy($this->getPrefix() . $key, $step); + return $this->getCache()->incrBy($this->getPrefix() . $key, $step); } /** @@ -125,7 +133,7 @@ class Redis extends Cache implements IMemcacheTTL { if (!$this->hasKey($key)) { return false; } - return self::$cache->decrBy($this->getPrefix() . $key, $step); + return $this->getCache()->decrBy($this->getPrefix() . $key, $step); } /** @@ -140,14 +148,14 @@ class Redis extends Cache implements IMemcacheTTL { if (!is_int($new)) { $new = json_encode($new); } - self::$cache->watch($this->getPrefix() . $key); + $this->getCache()->watch($this->getPrefix() . $key); if ($this->get($key) === $old) { - $result = self::$cache->multi() + $result = $this->getCache()->multi() ->set($this->getPrefix() . $key, $new) ->exec(); return $result !== false; } - self::$cache->unwatch(); + $this->getCache()->unwatch(); return false; } @@ -159,19 +167,19 @@ class Redis extends Cache implements IMemcacheTTL { * @return bool */ public function cad($key, $old) { - self::$cache->watch($this->getPrefix() . $key); + $this->getCache()->watch($this->getPrefix() . $key); if ($this->get($key) === $old) { - $result = self::$cache->multi() + $result = $this->getCache()->multi() ->del($this->getPrefix() . $key) ->exec(); return $result !== false; } - self::$cache->unwatch(); + $this->getCache()->unwatch(); return false; } public function setTTL($key, $ttl) { - self::$cache->expire($this->getPrefix() . $key, $ttl); + $this->getCache()->expire($this->getPrefix() . $key, $ttl); } public static function isAvailable(): bool { -- 2.39.5