@@ -32,156 +32,156 @@ |
||
32 | 32 | use OCP\IMemcacheTTL; |
33 | 33 | |
34 | 34 | class Redis extends Cache implements IMemcacheTTL { |
35 | - /** |
|
36 | - * @var \Redis $cache |
|
37 | - */ |
|
38 | - private static $cache = null; |
|
39 | - |
|
40 | - public function __construct($prefix = '') { |
|
41 | - parent::__construct($prefix); |
|
42 | - if (is_null(self::$cache)) { |
|
43 | - self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); |
|
44 | - } |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * entries in redis get namespaced to prevent collisions between ownCloud instances and users |
|
49 | - */ |
|
50 | - protected function getNameSpace() { |
|
51 | - return $this->prefix; |
|
52 | - } |
|
53 | - |
|
54 | - public function get($key) { |
|
55 | - $result = self::$cache->get($this->getNameSpace() . $key); |
|
56 | - if ($result === false && !self::$cache->exists($this->getNameSpace() . $key)) { |
|
57 | - return null; |
|
58 | - } else { |
|
59 | - return json_decode($result, true); |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - public function set($key, $value, $ttl = 0) { |
|
64 | - if ($ttl > 0) { |
|
65 | - return self::$cache->setex($this->getNameSpace() . $key, $ttl, json_encode($value)); |
|
66 | - } else { |
|
67 | - return self::$cache->set($this->getNameSpace() . $key, json_encode($value)); |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - public function hasKey($key) { |
|
72 | - return self::$cache->exists($this->getNameSpace() . $key); |
|
73 | - } |
|
74 | - |
|
75 | - public function remove($key) { |
|
76 | - if (self::$cache->del($this->getNameSpace() . $key)) { |
|
77 | - return true; |
|
78 | - } else { |
|
79 | - return false; |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - public function clear($prefix = '') { |
|
84 | - $prefix = $this->getNameSpace() . $prefix . '*'; |
|
85 | - $keys = self::$cache->keys($prefix); |
|
86 | - $deleted = self::$cache->del($keys); |
|
87 | - |
|
88 | - return count($keys) === $deleted; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Set a value in the cache if it's not already stored |
|
93 | - * |
|
94 | - * @param string $key |
|
95 | - * @param mixed $value |
|
96 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - public function add($key, $value, $ttl = 0) { |
|
100 | - // don't encode ints for inc/dec |
|
101 | - if (!is_int($value)) { |
|
102 | - $value = json_encode($value); |
|
103 | - } |
|
104 | - |
|
105 | - $args = ['nx']; |
|
106 | - if ($ttl !== 0 && is_int($ttl)) { |
|
107 | - $args['ex'] = $ttl; |
|
108 | - } |
|
109 | - |
|
110 | - return self::$cache->set($this->getPrefix() . $key, $value, $args); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Increase a stored number |
|
115 | - * |
|
116 | - * @param string $key |
|
117 | - * @param int $step |
|
118 | - * @return int | bool |
|
119 | - */ |
|
120 | - public function inc($key, $step = 1) { |
|
121 | - return self::$cache->incrBy($this->getNameSpace() . $key, $step); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Decrease a stored number |
|
126 | - * |
|
127 | - * @param string $key |
|
128 | - * @param int $step |
|
129 | - * @return int | bool |
|
130 | - */ |
|
131 | - public function dec($key, $step = 1) { |
|
132 | - if (!$this->hasKey($key)) { |
|
133 | - return false; |
|
134 | - } |
|
135 | - return self::$cache->decrBy($this->getNameSpace() . $key, $step); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Compare and set |
|
140 | - * |
|
141 | - * @param string $key |
|
142 | - * @param mixed $old |
|
143 | - * @param mixed $new |
|
144 | - * @return bool |
|
145 | - */ |
|
146 | - public function cas($key, $old, $new) { |
|
147 | - if (!is_int($new)) { |
|
148 | - $new = json_encode($new); |
|
149 | - } |
|
150 | - self::$cache->watch($this->getNameSpace() . $key); |
|
151 | - if ($this->get($key) === $old) { |
|
152 | - $result = self::$cache->multi() |
|
153 | - ->set($this->getNameSpace() . $key, $new) |
|
154 | - ->exec(); |
|
155 | - return $result !== false; |
|
156 | - } |
|
157 | - self::$cache->unwatch(); |
|
158 | - return false; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Compare and delete |
|
163 | - * |
|
164 | - * @param string $key |
|
165 | - * @param mixed $old |
|
166 | - * @return bool |
|
167 | - */ |
|
168 | - public function cad($key, $old) { |
|
169 | - self::$cache->watch($this->getNameSpace() . $key); |
|
170 | - if ($this->get($key) === $old) { |
|
171 | - $result = self::$cache->multi() |
|
172 | - ->del($this->getNameSpace() . $key) |
|
173 | - ->exec(); |
|
174 | - return $result !== false; |
|
175 | - } |
|
176 | - self::$cache->unwatch(); |
|
177 | - return false; |
|
178 | - } |
|
179 | - |
|
180 | - public function setTTL($key, $ttl) { |
|
181 | - self::$cache->expire($this->getNameSpace() . $key, $ttl); |
|
182 | - } |
|
183 | - |
|
184 | - public static function isAvailable() { |
|
185 | - return \OC::$server->getGetRedisFactory()->isAvailable(); |
|
186 | - } |
|
35 | + /** |
|
36 | + * @var \Redis $cache |
|
37 | + */ |
|
38 | + private static $cache = null; |
|
39 | + |
|
40 | + public function __construct($prefix = '') { |
|
41 | + parent::__construct($prefix); |
|
42 | + if (is_null(self::$cache)) { |
|
43 | + self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); |
|
44 | + } |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * entries in redis get namespaced to prevent collisions between ownCloud instances and users |
|
49 | + */ |
|
50 | + protected function getNameSpace() { |
|
51 | + return $this->prefix; |
|
52 | + } |
|
53 | + |
|
54 | + public function get($key) { |
|
55 | + $result = self::$cache->get($this->getNameSpace() . $key); |
|
56 | + if ($result === false && !self::$cache->exists($this->getNameSpace() . $key)) { |
|
57 | + return null; |
|
58 | + } else { |
|
59 | + return json_decode($result, true); |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + public function set($key, $value, $ttl = 0) { |
|
64 | + if ($ttl > 0) { |
|
65 | + return self::$cache->setex($this->getNameSpace() . $key, $ttl, json_encode($value)); |
|
66 | + } else { |
|
67 | + return self::$cache->set($this->getNameSpace() . $key, json_encode($value)); |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + public function hasKey($key) { |
|
72 | + return self::$cache->exists($this->getNameSpace() . $key); |
|
73 | + } |
|
74 | + |
|
75 | + public function remove($key) { |
|
76 | + if (self::$cache->del($this->getNameSpace() . $key)) { |
|
77 | + return true; |
|
78 | + } else { |
|
79 | + return false; |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + public function clear($prefix = '') { |
|
84 | + $prefix = $this->getNameSpace() . $prefix . '*'; |
|
85 | + $keys = self::$cache->keys($prefix); |
|
86 | + $deleted = self::$cache->del($keys); |
|
87 | + |
|
88 | + return count($keys) === $deleted; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Set a value in the cache if it's not already stored |
|
93 | + * |
|
94 | + * @param string $key |
|
95 | + * @param mixed $value |
|
96 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + public function add($key, $value, $ttl = 0) { |
|
100 | + // don't encode ints for inc/dec |
|
101 | + if (!is_int($value)) { |
|
102 | + $value = json_encode($value); |
|
103 | + } |
|
104 | + |
|
105 | + $args = ['nx']; |
|
106 | + if ($ttl !== 0 && is_int($ttl)) { |
|
107 | + $args['ex'] = $ttl; |
|
108 | + } |
|
109 | + |
|
110 | + return self::$cache->set($this->getPrefix() . $key, $value, $args); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Increase a stored number |
|
115 | + * |
|
116 | + * @param string $key |
|
117 | + * @param int $step |
|
118 | + * @return int | bool |
|
119 | + */ |
|
120 | + public function inc($key, $step = 1) { |
|
121 | + return self::$cache->incrBy($this->getNameSpace() . $key, $step); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Decrease a stored number |
|
126 | + * |
|
127 | + * @param string $key |
|
128 | + * @param int $step |
|
129 | + * @return int | bool |
|
130 | + */ |
|
131 | + public function dec($key, $step = 1) { |
|
132 | + if (!$this->hasKey($key)) { |
|
133 | + return false; |
|
134 | + } |
|
135 | + return self::$cache->decrBy($this->getNameSpace() . $key, $step); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Compare and set |
|
140 | + * |
|
141 | + * @param string $key |
|
142 | + * @param mixed $old |
|
143 | + * @param mixed $new |
|
144 | + * @return bool |
|
145 | + */ |
|
146 | + public function cas($key, $old, $new) { |
|
147 | + if (!is_int($new)) { |
|
148 | + $new = json_encode($new); |
|
149 | + } |
|
150 | + self::$cache->watch($this->getNameSpace() . $key); |
|
151 | + if ($this->get($key) === $old) { |
|
152 | + $result = self::$cache->multi() |
|
153 | + ->set($this->getNameSpace() . $key, $new) |
|
154 | + ->exec(); |
|
155 | + return $result !== false; |
|
156 | + } |
|
157 | + self::$cache->unwatch(); |
|
158 | + return false; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Compare and delete |
|
163 | + * |
|
164 | + * @param string $key |
|
165 | + * @param mixed $old |
|
166 | + * @return bool |
|
167 | + */ |
|
168 | + public function cad($key, $old) { |
|
169 | + self::$cache->watch($this->getNameSpace() . $key); |
|
170 | + if ($this->get($key) === $old) { |
|
171 | + $result = self::$cache->multi() |
|
172 | + ->del($this->getNameSpace() . $key) |
|
173 | + ->exec(); |
|
174 | + return $result !== false; |
|
175 | + } |
|
176 | + self::$cache->unwatch(); |
|
177 | + return false; |
|
178 | + } |
|
179 | + |
|
180 | + public function setTTL($key, $ttl) { |
|
181 | + self::$cache->expire($this->getNameSpace() . $key, $ttl); |
|
182 | + } |
|
183 | + |
|
184 | + public static function isAvailable() { |
|
185 | + return \OC::$server->getGetRedisFactory()->isAvailable(); |
|
186 | + } |
|
187 | 187 | } |