3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Cache\Type;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Cache\IMemoryCache;
27 use Friendica\Core\Cache\Enum\Type;
28 use Friendica\Core\Config\IConfig;
32 * Redis Cache. This driver is based on Memcache driver
34 class RedisCache extends BaseCache implements IMemoryCache
44 public function __construct(string $hostname, IConfig $config)
46 if (!class_exists('Redis', false)) {
47 throw new Exception('Redis class isn\'t available');
50 parent::__construct($hostname);
52 $this->redis = new Redis();
54 $redis_host = $config->get('system', 'redis_host');
55 $redis_port = $config->get('system', 'redis_port');
56 $redis_pw = $config->get('system', 'redis_password');
57 $redis_db = $config->get('system', 'redis_db', 0);
59 if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
60 throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
61 } elseif (!@$this->redis->connect($redis_host)) {
62 throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
65 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
66 throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
69 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
70 throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
77 public function getAllKeys($prefix = null)
82 $search = $prefix . '*';
85 $list = $this->redis->keys($this->getCacheKey($search));
87 return $this->getOriginalKeys($list);
93 public function get($key)
96 $cachekey = $this->getCacheKey($key);
98 $cached = $this->redis->get($cachekey);
99 if ($cached === false && !$this->redis->exists($cachekey)) {
103 $value = unserialize($cached);
105 // Only return a value if the serialized value is valid.
106 // We also check if the db entry is a serialized
107 // boolean 'false' value (which we want to return).
108 if ($cached === serialize(false) || $value !== false) {
118 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
120 $cachekey = $this->getCacheKey($key);
122 $cached = serialize($value);
125 return $this->redis->setex(
131 return $this->redis->set(
141 public function delete($key)
143 $cachekey = $this->getCacheKey($key);
144 $this->redis->del($cachekey);
145 // Redis doesn't have an error state for del()
152 public function clear($outdated = true)
157 return $this->redis->flushAll();
164 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
166 $cachekey = $this->getCacheKey($key);
167 $cached = serialize($value);
169 return $this->redis->setnx($cachekey, $cached);
175 public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
177 $cachekey = $this->getCacheKey($key);
179 $newCached = serialize($newValue);
181 $this->redis->watch($cachekey);
182 // If the old value isn't what we expected, somebody else changed the key meanwhile
183 if ($this->get($key) === $oldValue) {
185 $result = $this->redis->multi()
186 ->setex($cachekey, $ttl, $newCached)
189 $result = $this->redis->multi()
190 ->set($cachekey, $newCached)
193 return $result !== false;
195 $this->redis->unwatch();
202 public function compareDelete($key, $value)
204 $cachekey = $this->getCacheKey($key);
206 $this->redis->watch($cachekey);
207 // If the old value isn't what we expected, somebody else changed the key meanwhile
208 if ($this->get($key) === $value) {
209 $result = $this->redis->multi()
212 return $result !== false;
214 $this->redis->unwatch();
221 public function getName()