3 namespace Friendica\Core\Cache;
6 use Friendica\Core\BaseCache;
7 use Friendica\Core\Config\IConfig;
11 * Redis Cache. This driver is based on Memcache driver
13 * @author Hypolite Petovan <hypolite@mrpetovan.com>
14 * @author Roland Haeder <roland@mxchange.org>
16 class RedisCache extends BaseCache implements IMemoryCache
26 public function __construct(string $hostname, IConfig $config)
28 if (!class_exists('Redis', false)) {
29 throw new Exception('Redis class isn\'t available');
32 parent::__construct($hostname);
34 $this->redis = new Redis();
36 $redis_host = $config->get('system', 'redis_host');
37 $redis_port = $config->get('system', 'redis_port');
38 $redis_pw = $config->get('system', 'redis_password');
39 $redis_db = $config->get('system', 'redis_db', 0);
41 if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
42 throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
43 } elseif (!@$this->redis->connect($redis_host)) {
44 throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
47 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
48 throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
51 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
52 throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
59 public function getAllKeys($prefix = null)
64 $search = $prefix . '*';
67 $list = $this->redis->keys($this->getCacheKey($search));
69 return $this->getOriginalKeys($list);
75 public function get($key)
78 $cachekey = $this->getCacheKey($key);
80 $cached = $this->redis->get($cachekey);
81 if ($cached === false && !$this->redis->exists($cachekey)) {
85 $value = unserialize($cached);
87 // Only return a value if the serialized value is valid.
88 // We also check if the db entry is a serialized
89 // boolean 'false' value (which we want to return).
90 if ($cached === serialize(false) || $value !== false) {
100 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
102 $cachekey = $this->getCacheKey($key);
104 $cached = serialize($value);
107 return $this->redis->setex(
113 return $this->redis->set(
123 public function delete($key)
125 $cachekey = $this->getCacheKey($key);
126 return ($this->redis->del($cachekey) > 0);
132 public function clear($outdated = true)
137 return $this->redis->flushAll();
144 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
146 $cachekey = $this->getCacheKey($key);
147 $cached = serialize($value);
149 return $this->redis->setnx($cachekey, $cached);
155 public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
157 $cachekey = $this->getCacheKey($key);
159 $newCached = serialize($newValue);
161 $this->redis->watch($cachekey);
162 // If the old value isn't what we expected, somebody else changed the key meanwhile
163 if ($this->get($key) === $oldValue) {
165 $result = $this->redis->multi()
166 ->setex($cachekey, $ttl, $newCached)
169 $result = $this->redis->multi()
170 ->set($cachekey, $newCached)
173 return $result !== false;
175 $this->redis->unwatch();
182 public function compareDelete($key, $value)
184 $cachekey = $this->getCacheKey($key);
186 $this->redis->watch($cachekey);
187 // If the old value isn't what we expected, somebody else changed the key meanwhile
188 if ($this->get($key) === $value) {
189 $result = $this->redis->multi()
192 return $result !== false;
194 $this->redis->unwatch();
201 public function getName()