From 751394fc1dcc3289c4ef1c9ea731c8dd1dda6ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20H=C3=A4der?= Date: Sun, 13 May 2018 07:38:53 +0200 Subject: [PATCH] Redis: - added support for redis server: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit // 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 --- src/Core/Cache.php | 6 +++ src/Core/Cache/RedisCacheDriver.php | 77 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/Core/Cache/RedisCacheDriver.php diff --git a/src/Core/Cache.php b/src/Core/Cache.php index f1d1e11a5..4202db325 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -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 index 000000000..fa98842da --- /dev/null +++ b/src/Core/Cache/RedisCacheDriver.php @@ -0,0 +1,77 @@ + + * @author Roland Haeder + */ +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; + } +}