3 namespace Friendica\Core\Cache;
6 use Friendica\Core\Config\Configuration;
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, Configuration $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 (!$this->redis->connect($redis_host, $redis_port)) {
41 throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
44 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
45 throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
48 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
49 throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
56 public function getAllKeys($prefix = null)
61 $search = $prefix . '*';
64 $list = $this->redis->keys($this->getCacheKey($search));
66 return $this->getOriginalKeys($list);
72 public function get($key)
75 $cachekey = $this->getCacheKey($key);
77 $cached = $this->redis->get($cachekey);
78 if ($cached === false && !$this->redis->exists($cachekey)) {
82 $value = unserialize($cached);
84 // Only return a value if the serialized value is valid.
85 // We also check if the db entry is a serialized
86 // boolean 'false' value (which we want to return).
87 if ($cached === serialize(false) || $value !== false) {
97 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
99 $cachekey = $this->getCacheKey($key);
101 $cached = serialize($value);
104 return $this->redis->setex(
110 return $this->redis->set(
120 public function delete($key)
122 $cachekey = $this->getCacheKey($key);
123 return ($this->redis->delete($cachekey) > 0);
129 public function clear($outdated = true)
134 return $this->redis->flushAll();
141 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
143 $cachekey = $this->getCacheKey($key);
144 $cached = serialize($value);
146 return $this->redis->setnx($cachekey, $cached);
152 public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
154 $cachekey = $this->getCacheKey($key);
156 $newCached = serialize($newValue);
158 $this->redis->watch($cachekey);
159 // If the old value isn't what we expected, somebody else changed the key meanwhile
160 if ($this->get($key) === $oldValue) {
162 $result = $this->redis->multi()
163 ->setex($cachekey, $ttl, $newCached)
166 $result = $this->redis->multi()
167 ->set($cachekey, $newCached)
170 return $result !== false;
172 $this->redis->unwatch();
179 public function compareDelete($key, $value)
181 $cachekey = $this->getCacheKey($key);
183 $this->redis->watch($cachekey);
184 // If the old value isn't what we expected, somebody else changed the key meanwhile
185 if ($this->get($key) === $value) {
186 $result = $this->redis->multi()
189 return $result !== false;
191 $this->redis->unwatch();
195 public function __toString()
197 return self::TYPE_REDIS;