3 * @copyright Copyright (C) 2010-2022, 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\Capability\ICanCacheInMemory;
27 use Friendica\Core\Cache\Enum\Type;
28 use Friendica\Core\Cache\Exception\CachePersistenceException;
29 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
30 use Friendica\Core\Config\Capability\IManageConfigValues;
34 * Redis Cache. This driver is based on Memcache driver
36 class RedisCache extends AbstractCache implements ICanCacheInMemory
44 * @throws InvalidCacheDriverException
45 * @throws CachePersistenceException
47 public function __construct(string $hostname, IManageConfigValues $config)
49 if (!class_exists('Redis', false)) {
50 throw new InvalidCacheDriverException('Redis class isn\'t available');
53 parent::__construct($hostname);
55 $this->redis = new Redis();
57 $redis_host = $config->get('system', 'redis_host');
58 $redis_port = $config->get('system', 'redis_port');
59 $redis_pw = $config->get('system', 'redis_password');
60 $redis_db = $config->get('system', 'redis_db', 0);
62 if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
63 throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
64 } elseif (!@$this->redis->connect($redis_host)) {
65 throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ' isn\'t available');
68 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
69 throw new CachePersistenceException('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
72 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
73 throw new CachePersistenceException('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
80 public function getAllKeys(?string $prefix = null): array
85 $search = $prefix . '*';
88 $list = $this->redis->keys($this->getCacheKey($search));
90 return $this->getOriginalKeys($list);
96 public function get(string $key)
99 $cacheKey = $this->getCacheKey($key);
101 $cached = $this->redis->get($cacheKey);
102 if ($cached === false && !$this->redis->exists($cacheKey)) {
106 $value = unserialize($cached);
108 // Only return a value if the serialized value is valid.
109 // We also check if the db entry is a serialized
110 // boolean 'false' value (which we want to return).
111 if ($cached === serialize(false) || $value !== false) {
121 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
123 $cacheKey = $this->getCacheKey($key);
125 $cached = serialize($value);
128 return $this->redis->setex(
134 return $this->redis->set(
144 public function delete(string $key): bool
146 $cacheKey = $this->getCacheKey($key);
147 $this->redis->del($cacheKey);
148 // Redis doesn't have an error state for del()
155 public function clear(bool $outdated = true): bool
160 return $this->redis->flushAll();
167 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
169 $cacheKey = $this->getCacheKey($key);
170 $cached = serialize($value);
172 return $this->redis->setnx($cacheKey, $cached);
178 public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
180 $cacheKey = $this->getCacheKey($key);
182 $newCached = serialize($newValue);
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) === $oldValue) {
188 $result = $this->redis->multi()->setex($cacheKey, $ttl, $newCached)->exec();
190 $result = $this->redis->multi()->set($cacheKey, $newCached)->exec();
192 return $result !== false;
194 $this->redis->unwatch();
201 public function compareDelete(string $key, $value): bool
203 $cacheKey = $this->getCacheKey($key);
205 $this->redis->watch($cacheKey);
206 // If the old value isn't what we expected, somebody else changed the key meanwhile
207 if ($this->get($key) === $value) {
208 $this->redis->multi()->del($cacheKey)->exec();
211 $this->redis->unwatch();
218 public function getName(): string