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