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