]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/RedisCacheDriver.php
Redis serialize instead of json because of objects
[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                 $cached = $this->redis->get($cachekey);
39                 if ($cached === false && !$this->redis->exists($cachekey)) {
40                         return null;
41                 }
42
43                 $value = unserialize($cached);
44
45                 // Only return a value if the serialized value is valid.
46                 // We also check if the db entry is a serialized
47                 // boolean 'false' value (which we want to return).
48                 if ($cached === serialize(false) || $value !== false) {
49                         $return = $value;
50                 }
51
52                 return $return;
53         }
54
55         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
56         {
57                 $cachekey = $this->getCacheKey($key);
58
59                 $cached = serialize($value);
60
61                 if ($ttl > 0) {
62                         return $this->redis->setex(
63                                 $cachekey,
64                                 $ttl,
65                                 $cached
66                         );
67                 } else {
68                         return $this->redis->set(
69                                 $cachekey,
70                                 $cached
71                         );
72                 }
73         }
74
75         public function delete($key)
76         {
77                 $cachekey = $this->getCacheKey($key);
78                 return ($this->redis->delete($cachekey) > 0);
79         }
80
81         public function clear($outdated = true)
82         {
83                 if ($outdated) {
84                         return true;
85                 } else {
86                         return $this->redis->flushAll();
87                 }
88         }
89
90         /**
91          * (@inheritdoc)
92          */
93         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
94         {
95                 $cachekey = $this->getCacheKey($key);
96                 $cached = serialize($value);
97
98                 return $this->redis->setnx($cachekey, $cached);
99         }
100
101         /**
102          * (@inheritdoc)
103          */
104         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
105         {
106                 $cachekey = $this->getCacheKey($key);
107
108                 $newCached = serialize($newValue);
109
110                 $this->redis->watch($cachekey);
111                 // If the old value isn't what we expected, somebody else changed the key meanwhile
112                 if ($this->get($key) === $oldValue) {
113                         if ($ttl > 0) {
114                                 $result = $this->redis->multi()
115                                         ->setex($cachekey, $ttl, $newCached)
116                                         ->exec();
117                         } else {
118                                 $result = $this->redis->multi()
119                                         ->set($cachekey, $newValue)
120                                         ->exec();
121                         }
122                         return $result !== false;
123                 }
124                 $this->redis->unwatch();
125                 return false;
126         }
127         /**
128          * (@inheritdoc)
129          */
130         public function compareDelete($key, $value)
131         {
132                 $cachekey = $this->getCacheKey($key);
133
134                 $this->redis->watch($cachekey);
135                 // If the old value isn't what we expected, somebody else changed the key meanwhile
136                 if ($this->get($key) === $value) {
137                         $result = $this->redis->multi()
138                                 ->del($cachekey)
139                                 ->exec();
140                         return $result !== false;
141                 }
142                 $this->redis->unwatch();
143                 return false;
144         }
145 }