]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Cache/RedisCache.php
Merge pull request #8996 from MrPetovan/bug/8995-profile-contacts-is-owner
[friendica.git] / src / Core / Cache / RedisCache.php
index 40cb56d35cdaa9939c7cc998f006b0059125ede4..5dbb963882917d3be47bed7d0383380cb3b9993d 100644 (file)
@@ -1,19 +1,35 @@
 <?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Core\Cache;
 
 use Exception;
-use Friendica\Core\Cache;
-use Friendica\Core\Config\Configuration;
+use Friendica\Core\BaseCache;
+use Friendica\Core\Config\IConfig;
 use Redis;
 
 /**
  * Redis Cache. This driver is based on Memcache driver
- *
- * @author Hypolite Petovan <hypolite@mrpetovan.com>
- * @author Roland Haeder <roland@mxchange.org>
  */
-class RedisCache extends AbstractCache implements IMemoryCache
+class RedisCache extends BaseCache implements IMemoryCache
 {
        /**
         * @var Redis
@@ -23,7 +39,7 @@ class RedisCache extends AbstractCache implements IMemoryCache
        /**
         * @throws Exception
         */
-       public function __construct(string $hostname, Configuration $config)
+       public function __construct(string $hostname, IConfig $config)
        {
                if (!class_exists('Redis', false)) {
                        throw new Exception('Redis class isn\'t available');
@@ -38,8 +54,10 @@ class RedisCache extends AbstractCache implements IMemoryCache
                $redis_pw   = $config->get('system', 'redis_password');
                $redis_db   = $config->get('system', 'redis_db', 0);
 
-               if (!$this->redis->connect($redis_host, $redis_port)) {
+               if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
                        throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
+               } elseif (!@$this->redis->connect($redis_host)) {
+                       throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
                }
 
                if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
@@ -95,7 +113,7 @@ class RedisCache extends AbstractCache implements IMemoryCache
        /**
         * (@inheritdoc)
         */
-       public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
+       public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
        {
                $cachekey = $this->getCacheKey($key);
 
@@ -121,7 +139,9 @@ class RedisCache extends AbstractCache implements IMemoryCache
        public function delete($key)
        {
                $cachekey = $this->getCacheKey($key);
-               return ($this->redis->delete($cachekey) > 0);
+               $this->redis->del($cachekey);
+               // Redis doesn't have an error state for del()
+               return true;
        }
 
        /**
@@ -139,7 +159,7 @@ class RedisCache extends AbstractCache implements IMemoryCache
        /**
         * (@inheritdoc)
         */
-       public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
+       public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
        {
                $cachekey = $this->getCacheKey($key);
                $cached = serialize($value);
@@ -150,7 +170,7 @@ class RedisCache extends AbstractCache implements IMemoryCache
        /**
         * (@inheritdoc)
         */
-       public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
+       public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
        {
                $cachekey = $this->getCacheKey($key);
 
@@ -193,8 +213,11 @@ class RedisCache extends AbstractCache implements IMemoryCache
                return false;
        }
 
-       public function __toString()
+       /**
+        * {@inheritDoc}
+        */
+       public function getName()
        {
-               return self::TYPE_REDIS;
+               return Type::REDIS;
        }
 }