]> git.mxchange.org Git - friendica.git/commitdiff
Redis:
authorRoland Häder <roland@mxchange.org>
Sun, 13 May 2018 05:38:53 +0000 (07:38 +0200)
committerRoland Häder <roland@mxchange.org>
Mon, 14 May 2018 20:50:05 +0000 (22:50 +0200)
- added support for redis server:

// Required to actually have this cache driver active:
$a->config['system']['cache_driver'] = 'redis';

// ---- OPTIONAL/DEFAULT: ----
$a->config['system']['redis_host'] = '127.0.0.1';
$a->config['system']['redis_port'] = 6379;

Signed-off-by: Roland Häder <roland@mxchange.org>
src/Core/Cache.php
src/Core/Cache/RedisCacheDriver.php [new file with mode: 0644]

index f1d1e11a542a26835b0700e25221fd36493ef044..4202db325c411c38b7852321af769bfb0f09d33c 100644 (file)
@@ -40,6 +40,12 @@ class Cache extends \Friendica\BaseObject
 
                                self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
                                break;
+                       case 'redis':
+                               $redis_host = Config::get('system', 'redis_host', '127.0.0.1');
+                               $redis_port = Config::get('system', 'redis_port', 6379);
+
+                               self::$driver = new Cache\RedisCacheDriver($redis_host, $redis_port);
+                               break;
                        default:
                                self::$driver = new Cache\DatabaseCacheDriver();
                }
diff --git a/src/Core/Cache/RedisCacheDriver.php b/src/Core/Cache/RedisCacheDriver.php
new file mode 100644 (file)
index 0000000..fa98842
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+
+namespace Friendica\Core\Cache;
+
+use Friendica\BaseObject;
+use Friendica\Core\Cache;
+
+/**
+ * Redis Cache Driver. This driver is based on Memcache driver
+ *
+ * @author Hypolite Petovan <mrpetovan@gmail.com>
+ * @author Roland Haeder <roland@mxchange.org>
+ */
+class RedisCacheDriver extends BaseObject implements ICacheDriver
+{
+       /**
+        * @var Redis
+        */
+       private $redis;
+
+       public function __construct($redis_host, $redis_port)
+       {
+               if (!class_exists('Redis', false)) {
+                       throw new \Exception('Redis class isn\'t available');
+               }
+
+               $this->redis = new \Redis();
+
+               if (!$this->redis->connect($redis_host, $redis_port)) {
+                       throw new \Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
+               }
+       }
+
+       public function get($key)
+       {
+               $return = null;
+
+               // We fetch with the hostname as key to avoid problems with other applications
+               $cached = $this->redis->get(self::getApp()->get_hostname() . ':' . $key);
+
+               // @see http://php.net/manual/en/redis.get.php#84275
+               if (is_bool($cached) || is_double($cached) || is_long($cached)) {
+                       return $return;
+               }
+
+               $value = @unserialize($cached);
+
+               // Only return a value if the serialized value is valid.
+               // We also check if the db entry is a serialized
+               // boolean 'false' value (which we want to return).
+               if ($cached === serialize(false) || $value !== false) {
+                       $return = $value;
+               }
+
+               return $return;
+       }
+
+       public function set($key, $value, $duration = Cache::MONTH)
+       {
+               // We store with the hostname as key to avoid problems with other applications
+               return $this->redis->set(
+                       self::getApp()->get_hostname() . ":" . $key,
+                       serialize($value),
+                       time() + $duration
+               );
+       }
+
+       public function delete($key)
+       {
+               return $this->redis->delete($key);
+       }
+
+       public function clear()
+       {
+               return true;
+       }
+}