]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/RedisCacheDriver.php
d23fa2697bfe9b2398a4fdd5d29e780b4c732a28
[friendica.git] / src / Core / Cache / RedisCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Cache;
7
8 /**
9  * Redis Cache Driver. This driver is based on Memcache driver
10  *
11  * @author Hypolite Petovan <mrpetovan@gmail.com>
12  * @author Roland Haeder <roland@mxchange.org>
13  */
14 class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
15 {
16         /**
17          * @var Redis
18          */
19         private $redis;
20
21         public function __construct($redis_host, $redis_port)
22         {
23                 if (!class_exists('Redis', false)) {
24                         throw new \Exception('Redis class isn\'t available');
25                 }
26
27                 $this->redis = new \Redis();
28
29                 if (!$this->redis->connect($redis_host, $redis_port)) {
30                         throw new \Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
31                 }
32         }
33
34         public function get($key)
35         {
36                 $return = null;
37
38                 // We fetch with the hostname as key to avoid problems with other applications
39                 $cached = $this->redis->get(self::getApp()->get_hostname() . ':' . $key);
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                 // We store with the hostname as key to avoid problems with other applications
61                 if ($ttl > 0) {
62                         return $this->redis->setex(
63                                 self::getApp()->get_hostname() . ":" . $key,
64                                 time() + $ttl,
65                                 serialize($value)
66                         );
67                 } else {
68                         return $this->redis->set(
69                                 self::getApp()->get_hostname() . ":" . $key,
70                                 serialize($value)
71                         );
72                 }
73         }
74
75         public function delete($key)
76         {
77                 return $this->redis->delete($key);
78         }
79
80         public function clear()
81         {
82                 return true;
83         }
84
85
86         /**
87          * @brief Sets a value if it's not already stored
88          *
89          * @param string $key The cache key
90          * @param mixed $value The old value we know from the cache
91          * @param int    $ttl      The cache lifespan, must be one of the Cache constants
92          * @return bool
93          */
94         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
95         {
96                 if (!is_int($value)) {
97                         $value = serialize($value);
98                 }
99
100                 return $this->redis->setnx(self::getApp()->get_hostname() . ":" . $key, $value);
101         }
102
103         /**
104          * @brief Compares if the old value is set and sets the new value
105          *
106          * @param string $key The cache key
107          * @param mixed $oldValue The old value we know
108          * @param mixed $newValue The new value we want to set
109          * @param int    $ttl      The cache lifespan, must be one of the Cache constants
110          * @return bool
111          */
112         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
113         {
114                 if (!is_int($newValue)) {
115                         $newValue = serialize($newValue);
116                 }
117
118                 $this->redis->watch(self::getApp()->get_hostname() . ":" . $key);
119                 // If the old value isn't what we expected, somebody else changed the key meanwhile
120                 if ($this->get($key) === $oldValue) {
121                         if ($ttl > 0) {
122                                 $result = $this->redis->multi()
123                                         ->setex(self::getApp()->get_hostname() . ":" . $ttl, $key, $newValue)
124                                         ->exec();
125                         } else {
126                                 $result = $this->redis->multi()
127                                         ->set(self::getApp()->get_hostname() . ":" . $key, $newValue)
128                                         ->exec();
129                         }
130                         return $result !== false;
131                 }
132                 $this->redis->unwatch();
133                 return false;
134         }
135         /**
136          * @brief Compares if the old value is set and removes it
137          *
138          * @param string $key The cache key
139          * @param mixed $value The old value we know and want to delete
140          * @return bool
141          */
142         public function compareDelete($key, $value)
143         {
144                 $this->redis->watch(self::getApp()->get_hostname() . ":" . $key);
145                 // If the old value isn't what we expected, somebody else changed the key meanwhile
146                 if ($this->get($key) === $value) {
147                         $result = $this->redis->multi()
148                                 ->del(self::getApp()->get_hostname() . ":" . $key)
149                                 ->exec();
150                         return $result !== false;
151                 }
152                 $this->redis->unwatch();
153                 return false;
154         }
155 }