]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/RedisCache.php
Merge pull request #8130 from nupplaphil/task/di_pconfig
[friendica.git] / src / Core / Cache / RedisCache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Exception;
6 use Friendica\Core\BaseCache;
7 use Friendica\Core\Config\IConfiguration;
8 use Redis;
9
10 /**
11  * Redis Cache. This driver is based on Memcache driver
12  *
13  * @author Hypolite Petovan <hypolite@mrpetovan.com>
14  * @author Roland Haeder <roland@mxchange.org>
15  */
16 class RedisCache extends BaseCache implements IMemoryCache
17 {
18         /**
19          * @var Redis
20          */
21         private $redis;
22
23         /**
24          * @throws Exception
25          */
26         public function __construct(string $hostname, IConfiguration $config)
27         {
28                 if (!class_exists('Redis', false)) {
29                         throw new Exception('Redis class isn\'t available');
30                 }
31
32                 parent::__construct($hostname);
33
34                 $this->redis = new Redis();
35
36                 $redis_host = $config->get('system', 'redis_host');
37                 $redis_port = $config->get('system', 'redis_port');
38                 $redis_pw   = $config->get('system', 'redis_password');
39                 $redis_db   = $config->get('system', 'redis_db', 0);
40
41                 if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
42                         throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
43                 } elseif (!@$this->redis->connect($redis_host)) {
44                         throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
45                 }
46
47                 if (isset($redis_pw) && !$this->redis->auth($redis_pw)) {
48                         throw new Exception('Cannot authenticate redis server at ' . $redis_host . ':' . $redis_port);
49                 }
50
51                 if ($redis_db !== 0 && !$this->redis->select($redis_db)) {
52                         throw new Exception('Cannot switch to redis db ' . $redis_db . ' at ' . $redis_host . ':' . $redis_port);
53                 }
54         }
55
56         /**
57          * (@inheritdoc)
58          */
59         public function getAllKeys($prefix = null)
60         {
61                 if (empty($prefix)) {
62                         $search = '*';
63                 } else {
64                         $search = $prefix . '*';
65                 }
66
67                 $list = $this->redis->keys($this->getCacheKey($search));
68
69                 return $this->getOriginalKeys($list);
70         }
71
72         /**
73          * (@inheritdoc)
74          */
75         public function get($key)
76         {
77                 $return = null;
78                 $cachekey = $this->getCacheKey($key);
79
80                 $cached = $this->redis->get($cachekey);
81                 if ($cached === false && !$this->redis->exists($cachekey)) {
82                         return null;
83                 }
84
85                 $value = unserialize($cached);
86
87                 // Only return a value if the serialized value is valid.
88                 // We also check if the db entry is a serialized
89                 // boolean 'false' value (which we want to return).
90                 if ($cached === serialize(false) || $value !== false) {
91                         $return = $value;
92                 }
93
94                 return $return;
95         }
96
97         /**
98          * (@inheritdoc)
99          */
100         public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
101         {
102                 $cachekey = $this->getCacheKey($key);
103
104                 $cached = serialize($value);
105
106                 if ($ttl > 0) {
107                         return $this->redis->setex(
108                                 $cachekey,
109                                 $ttl,
110                                 $cached
111                         );
112                 } else {
113                         return $this->redis->set(
114                                 $cachekey,
115                                 $cached
116                         );
117                 }
118         }
119
120         /**
121          * (@inheritdoc)
122          */
123         public function delete($key)
124         {
125                 $cachekey = $this->getCacheKey($key);
126                 return ($this->redis->del($cachekey) > 0);
127         }
128
129         /**
130          * (@inheritdoc)
131          */
132         public function clear($outdated = true)
133         {
134                 if ($outdated) {
135                         return true;
136                 } else {
137                         return $this->redis->flushAll();
138                 }
139         }
140
141         /**
142          * (@inheritdoc)
143          */
144         public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
145         {
146                 $cachekey = $this->getCacheKey($key);
147                 $cached = serialize($value);
148
149                 return $this->redis->setnx($cachekey, $cached);
150         }
151
152         /**
153          * (@inheritdoc)
154          */
155         public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
156         {
157                 $cachekey = $this->getCacheKey($key);
158
159                 $newCached = serialize($newValue);
160
161                 $this->redis->watch($cachekey);
162                 // If the old value isn't what we expected, somebody else changed the key meanwhile
163                 if ($this->get($key) === $oldValue) {
164                         if ($ttl > 0) {
165                                 $result = $this->redis->multi()
166                                         ->setex($cachekey, $ttl, $newCached)
167                                         ->exec();
168                         } else {
169                                 $result = $this->redis->multi()
170                                         ->set($cachekey, $newCached)
171                                         ->exec();
172                         }
173                         return $result !== false;
174                 }
175                 $this->redis->unwatch();
176                 return false;
177         }
178
179         /**
180          * (@inheritdoc)
181          */
182         public function compareDelete($key, $value)
183         {
184                 $cachekey = $this->getCacheKey($key);
185
186                 $this->redis->watch($cachekey);
187                 // If the old value isn't what we expected, somebody else changed the key meanwhile
188                 if ($this->get($key) === $value) {
189                         $result = $this->redis->multi()
190                                 ->del($cachekey)
191                                 ->exec();
192                         return $result !== false;
193                 }
194                 $this->redis->unwatch();
195                 return false;
196         }
197
198         /**
199          * {@inheritDoc}
200          */
201         public function getName()
202         {
203                 return Type::REDIS;
204         }
205 }