]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/RedisCacheDriver.php
Merge pull request #5325 from nupplaphil/lock_followup
[friendica.git] / src / Core / Cache / RedisCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\Cache;
6
7 /**
8  * Redis Cache Driver. This driver is based on Memcache driver
9  *
10  * @author Hypolite Petovan <mrpetovan@gmail.com>
11  * @author Roland Haeder <roland@mxchange.org>
12  */
13 class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
14 {
15         /**
16          * @var \Redis
17          */
18         private $redis;
19
20         public function __construct($redis_host, $redis_port)
21         {
22                 if (!class_exists('Redis', false)) {
23                         throw new \Exception('Redis class isn\'t available');
24                 }
25
26                 $this->redis = new \Redis();
27
28                 if (!$this->redis->connect($redis_host, $redis_port)) {
29                         throw new \Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
30                 }
31         }
32
33         public function get($key)
34         {
35                 $return = null;
36                 $cachekey = $this->getCacheKey($key);
37
38                 // We fetch with the hostname as key to avoid problems with other applications
39                 $cached = $this->redis->get($cachekey);
40
41                 // @see http://php.net/manual/en/redis.get.php#84275
42                 if (is_bool($cached) || is_double($cached) || is_long($cached)) {
43                         return $return;
44                 }
45
46                 $value = @unserialize($cached);
47
48                 // Only return a value if the serialized value is valid.
49                 // We also check if the db entry is a serialized
50                 // boolean 'false' value (which we want to return).
51                 if ($cached === serialize(false) || $value !== false) {
52                         $return = $value;
53                 }
54
55                 return $return;
56         }
57
58         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
59         {
60                 $cachekey = $this->getCacheKey($key);
61
62                 // We store with the hostname as key to avoid problems with other applications
63                 if ($ttl > 0) {
64                         return $this->redis->setex(
65                                 $cachekey,
66                                 time() + $ttl,
67                                 serialize($value)
68                         );
69                 } else {
70                         return $this->redis->set(
71                                 $cachekey,
72                                 serialize($value)
73                         );
74                 }
75         }
76
77         public function delete($key)
78         {
79                 return $this->redis->delete($key);
80         }
81
82         public function clear()
83         {
84                 return true;
85         }
86
87
88         /**
89          * (@inheritdoc)
90          */
91         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
92         {
93                 $cachekey = $this->getCacheKey($key);
94
95                 if (!is_int($value)) {
96                         $value = serialize($value);
97                 }
98
99                 return $this->redis->setnx($cachekey, $value);
100         }
101
102         /**
103          * (@inheritdoc)
104          */
105         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
106         {
107                 $cachekey = $this->getCacheKey($key);
108
109                 if (!is_int($newValue)) {
110                         $newValue = serialize($newValue);
111                 }
112
113                 $this->redis->watch($cachekey);
114                 // If the old value isn't what we expected, somebody else changed the key meanwhile
115                 if ($this->get($cachekey) === $oldValue) {
116                         if ($ttl > 0) {
117                                 $result = $this->redis->multi()
118                                         ->setex($cachekey, $ttl, $newValue)
119                                         ->exec();
120                         } else {
121                                 $result = $this->redis->multi()
122                                         ->set($cachekey, $newValue)
123                                         ->exec();
124                         }
125                         return $result !== false;
126                 }
127                 $this->redis->unwatch();
128                 return false;
129         }
130         /**
131          * (@inheritdoc)
132          */
133         public function compareDelete($key, $value)
134         {
135                 $cachekey = $this->getCacheKey($key);
136
137                 $this->redis->watch($cachekey);
138                 // If the old value isn't what we expected, somebody else changed the key meanwhile
139                 if ($this->get($key) === $value) {
140                         $result = $this->redis->multi()
141                                 ->del($cachekey)
142                                 ->exec();
143                         return $result !== false;
144                 }
145                 $this->redis->unwatch();
146                 return false;
147         }
148 }