3 namespace Friendica\Core\Cache;
6 use Friendica\Core\Config\IConfiguration;
10 * Redis Cache. This driver is based on Memcache driver
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
13 * @author Roland Haeder <roland@mxchange.org>
15 class RedisCache extends Cache implements IMemoryCache
25 public function __construct(string $hostname, IConfiguration $config)
27 if (!class_exists('Redis', false)) {
28 throw new Exception('Redis class isn\'t available');
31 parent::__construct($hostname);
33 $this->redis = new Redis();
35 $redis_host = $config->get('system', 'redis_host');
36 $redis_port = $config->get('system', 'redis_port');
37 $redis_pw = $config->get('system', 'redis_password');
38 $redis_db = $config->get('system', 'redis_db', 0);
40 if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
41 throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
42 } elseif (!@$this->redis->connect($redis_host)) {
43 throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
46 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
47 throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
50 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
51 throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
58 public function getAllKeys($prefix = null)
63 $search = $prefix . '*';
66 $list = $this->redis->keys($this->getCacheKey($search));
68 return $this->getOriginalKeys($list);
74 public function get($key)
77 $cachekey = $this->getCacheKey($key);
79 $cached = $this->redis->get($cachekey);
80 if ($cached === false && !$this->redis->exists($cachekey)) {
84 $value = unserialize($cached);
86 // Only return a value if the serialized value is valid.
87 // We also check if the db entry is a serialized
88 // boolean 'false' value (which we want to return).
89 if ($cached === serialize(false) || $value !== false) {
99 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
101 $cachekey = $this->getCacheKey($key);
103 $cached = serialize($value);
106 return $this->redis->setex(
112 return $this->redis->set(
122 public function delete($key)
124 $cachekey = $this->getCacheKey($key);
125 return ($this->redis->del($cachekey) > 0);
131 public function clear($outdated = true)
136 return $this->redis->flushAll();
143 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
145 $cachekey = $this->getCacheKey($key);
146 $cached = serialize($value);
148 return $this->redis->setnx($cachekey, $cached);
154 public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
156 $cachekey = $this->getCacheKey($key);
158 $newCached = serialize($newValue);
160 $this->redis->watch($cachekey);
161 // If the old value isn't what we expected, somebody else changed the key meanwhile
162 if ($this->get($key) === $oldValue) {
164 $result = $this->redis->multi()
165 ->setex($cachekey, $ttl, $newCached)
168 $result = $this->redis->multi()
169 ->set($cachekey, $newCached)
172 return $result !== false;
174 $this->redis->unwatch();
181 public function compareDelete($key, $value)
183 $cachekey = $this->getCacheKey($key);
185 $this->redis->watch($cachekey);
186 // If the old value isn't what we expected, somebody else changed the key meanwhile
187 if ($this->get($key) === $value) {
188 $result = $this->redis->multi()
191 return $result !== false;
193 $this->redis->unwatch();
200 public function getName()
202 return self::TYPE_REDIS;