3 namespace Friendica\Core\Cache;
6 use Friendica\Core\Cache;
10 * Redis Cache Driver. This driver is based on Memcache driver
12 * @author Hypolite Petovan <hypolite@mrpetovan.com>
13 * @author Roland Haeder <roland@mxchange.org>
15 class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
23 * @param string $redis_host
24 * @param int $redis_port
25 * @param int $redis_db (Default = 0, maximum is 15)
26 * @param string? $redis_pw
29 public function __construct($redis_host, $redis_port, $redis_db = 0, $redis_pw = null)
31 if (!class_exists('Redis', false)) {
32 throw new Exception('Redis class isn\'t available');
35 $this->redis = new Redis();
37 if (!$this->redis->connect($redis_host, $redis_port)) {
38 throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
41 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
42 throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
45 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
46 throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
53 public function getAllKeys($prefix = null)
58 $search = $prefix . '*';
61 $list = $this->redis->keys($this->getCacheKey($search));
63 return $this->getOriginalKeys($list);
69 public function get($key)
72 $cachekey = $this->getCacheKey($key);
74 $cached = $this->redis->get($cachekey);
75 if ($cached === false && !$this->redis->exists($cachekey)) {
79 $value = unserialize($cached);
81 // Only return a value if the serialized value is valid.
82 // We also check if the db entry is a serialized
83 // boolean 'false' value (which we want to return).
84 if ($cached === serialize(false) || $value !== false) {
94 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
96 $cachekey = $this->getCacheKey($key);
98 $cached = serialize($value);
101 return $this->redis->setex(
107 return $this->redis->set(
117 public function delete($key)
119 $cachekey = $this->getCacheKey($key);
120 return ($this->redis->delete($cachekey) > 0);
126 public function clear($outdated = true)
131 return $this->redis->flushAll();
138 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
140 $cachekey = $this->getCacheKey($key);
141 $cached = serialize($value);
143 return $this->redis->setnx($cachekey, $cached);
149 public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
151 $cachekey = $this->getCacheKey($key);
153 $newCached = serialize($newValue);
155 $this->redis->watch($cachekey);
156 // If the old value isn't what we expected, somebody else changed the key meanwhile
157 if ($this->get($key) === $oldValue) {
159 $result = $this->redis->multi()
160 ->setex($cachekey, $ttl, $newCached)
163 $result = $this->redis->multi()
164 ->set($cachekey, $newCached)
167 return $result !== false;
169 $this->redis->unwatch();
176 public function compareDelete($key, $value)
178 $cachekey = $this->getCacheKey($key);
180 $this->redis->watch($cachekey);
181 // If the old value isn't what we expected, somebody else changed the key meanwhile
182 if ($this->get($key) === $value) {
183 $result = $this->redis->multi()
186 return $result !== false;
188 $this->redis->unwatch();