3 namespace Friendica\Core\Cache;
6 use Friendica\Core\Cache;
7 use Friendica\Core\Config\Configuration;
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 AbstractCache implements IMemoryCache
26 public function __construct(string $hostname, Configuration $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 (!$this->redis->connect($redis_host, $redis_port)) {
42 throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
45 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
46 throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
49 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
50 throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
57 public function getAllKeys($prefix = null)
62 $search = $prefix . '*';
65 $list = $this->redis->keys($this->getCacheKey($search));
67 return $this->getOriginalKeys($list);
73 public function get($key)
76 $cachekey = $this->getCacheKey($key);
78 $cached = $this->redis->get($cachekey);
79 if ($cached === false && !$this->redis->exists($cachekey)) {
83 $value = unserialize($cached);
85 // Only return a value if the serialized value is valid.
86 // We also check if the db entry is a serialized
87 // boolean 'false' value (which we want to return).
88 if ($cached === serialize(false) || $value !== false) {
98 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
100 $cachekey = $this->getCacheKey($key);
102 $cached = serialize($value);
105 return $this->redis->setex(
111 return $this->redis->set(
121 public function delete($key)
123 $cachekey = $this->getCacheKey($key);
124 return ($this->redis->delete($cachekey) > 0);
130 public function clear($outdated = true)
135 return $this->redis->flushAll();
142 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
144 $cachekey = $this->getCacheKey($key);
145 $cached = serialize($value);
147 return $this->redis->setnx($cachekey, $cached);
153 public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
155 $cachekey = $this->getCacheKey($key);
157 $newCached = serialize($newValue);
159 $this->redis->watch($cachekey);
160 // If the old value isn't what we expected, somebody else changed the key meanwhile
161 if ($this->get($key) === $oldValue) {
163 $result = $this->redis->multi()
164 ->setex($cachekey, $ttl, $newCached)
167 $result = $this->redis->multi()
168 ->set($cachekey, $newCached)
171 return $result !== false;
173 $this->redis->unwatch();
180 public function compareDelete($key, $value)
182 $cachekey = $this->getCacheKey($key);
184 $this->redis->watch($cachekey);
185 // If the old value isn't what we expected, somebody else changed the key meanwhile
186 if ($this->get($key) === $value) {
187 $result = $this->redis->multi()
190 return $result !== false;
192 $this->redis->unwatch();
196 public function __toString()
198 return self::TYPE_REDIS;