3 * @copyright Copyright (C) 2010-2023, 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;
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Exception\CachePersistenceException;
27 use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
28 use Friendica\Core\Config\Capability\IManageConfigValues;
32 * Redis Cache. This driver is based on Memcache driver
34 class RedisCache extends AbstractCache implements ICanCacheInMemory
36 public static $NAME = 'redis';
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);
64 if (!empty($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
65 throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
66 } else if (!@$this->redis->connect($redis_host)) {
67 throw new CachePersistenceException('Expected Redis server at ' . $redis_host . ' isn\'t available');
70 if (!empty($redis_pw) && !$this->redis->auth($redis_pw)) {
71 throw new CachePersistenceException('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
74 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
75 throw new CachePersistenceException('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
77 } catch (\RedisException $exception) {
78 throw new CachePersistenceException('Redis connection fails unexpectedly', $exception);
85 public function getAllKeys(?string $prefix = null): array
90 $search = $prefix . '*';
93 $list = $this->redis->keys($this->getCacheKey($search));
95 return $this->getOriginalKeys($list);
101 public function get(string $key)
104 $cacheKey = $this->getCacheKey($key);
106 $cached = $this->redis->get($cacheKey);
107 if ($cached === false && !$this->redis->exists($cacheKey)) {
111 $value = unserialize($cached);
113 // Only return a value if the serialized value is valid.
114 // We also check if the db entry is a serialized
115 // boolean 'false' value (which we want to return).
116 if ($cached === serialize(false) || $value !== false) {
126 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
128 $cacheKey = $this->getCacheKey($key);
130 $cached = serialize($value);
133 return $this->redis->setex(
139 return $this->redis->set(
149 public function delete(string $key): bool
151 $cacheKey = $this->getCacheKey($key);
152 $this->redis->del($cacheKey);
153 // Redis doesn't have an error state for del()
160 public function clear(bool $outdated = true): bool
165 return $this->redis->flushAll();
172 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
174 $cacheKey = $this->getCacheKey($key);
175 $cached = serialize($value);
177 return $this->redis->setnx($cacheKey, $cached);
183 public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
185 $cacheKey = $this->getCacheKey($key);
187 $newCached = serialize($newValue);
189 $this->redis->watch($cacheKey);
190 // If the old value isn't what we expected, somebody else changed the key meanwhile
191 if ($this->get($key) === $oldValue) {
193 $result = $this->redis->multi()->setex($cacheKey, $ttl, $newCached)->exec();
195 $result = $this->redis->multi()->set($cacheKey, $newCached)->exec();
197 return $result !== false;
199 $this->redis->unwatch();
206 public function compareDelete(string $key, $value): bool
208 $cacheKey = $this->getCacheKey($key);
210 $this->redis->watch($cacheKey);
211 // If the old value isn't what we expected, somebody else changed the key meanwhile
212 if ($this->get($key) === $value) {
213 $this->redis->multi()->del($cacheKey)->exec();
216 $this->redis->unwatch();