@@ -32,149 +32,149 @@ |
||
| 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 = '', string $logFile = '') { |
|
| 41 | - parent::__construct($prefix); |
|
| 42 | - if (is_null(self::$cache)) { |
|
| 43 | - self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function get($key) { |
|
| 48 | - $result = self::$cache->get($this->getPrefix() . $key); |
|
| 49 | - if ($result === false && !self::$cache->exists($this->getPrefix() . $key)) { |
|
| 50 | - return null; |
|
| 51 | - } else { |
|
| 52 | - return json_decode($result, true); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public function set($key, $value, $ttl = 0) { |
|
| 57 | - if ($ttl > 0) { |
|
| 58 | - return self::$cache->setex($this->getPrefix() . $key, $ttl, json_encode($value)); |
|
| 59 | - } else { |
|
| 60 | - return self::$cache->set($this->getPrefix() . $key, json_encode($value)); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public function hasKey($key) { |
|
| 65 | - return (bool)self::$cache->exists($this->getPrefix() . $key); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function remove($key) { |
|
| 69 | - if (self::$cache->del($this->getPrefix() . $key)) { |
|
| 70 | - return true; |
|
| 71 | - } else { |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function clear($prefix = '') { |
|
| 77 | - $prefix = $this->getPrefix() . $prefix . '*'; |
|
| 78 | - $keys = self::$cache->keys($prefix); |
|
| 79 | - $deleted = self::$cache->del($keys); |
|
| 80 | - |
|
| 81 | - return (is_array($keys) && (count($keys) === $deleted)); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Set a value in the cache if it's not already stored |
|
| 86 | - * |
|
| 87 | - * @param string $key |
|
| 88 | - * @param mixed $value |
|
| 89 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function add($key, $value, $ttl = 0) { |
|
| 93 | - // don't encode ints for inc/dec |
|
| 94 | - if (!is_int($value)) { |
|
| 95 | - $value = json_encode($value); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $args = ['nx']; |
|
| 99 | - if ($ttl !== 0 && is_int($ttl)) { |
|
| 100 | - $args['ex'] = $ttl; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return self::$cache->set($this->getPrefix() . $key, $value, $args); |
|
| 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->getPrefix() . $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->getPrefix() . $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->getPrefix() . $key); |
|
| 144 | - if ($this->get($key) === $old) { |
|
| 145 | - $result = self::$cache->multi() |
|
| 146 | - ->set($this->getPrefix() . $key, $new) |
|
| 147 | - ->exec(); |
|
| 148 | - return $result !== false; |
|
| 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->getPrefix() . $key); |
|
| 163 | - if ($this->get($key) === $old) { |
|
| 164 | - $result = self::$cache->multi() |
|
| 165 | - ->del($this->getPrefix() . $key) |
|
| 166 | - ->exec(); |
|
| 167 | - return $result !== false; |
|
| 168 | - } |
|
| 169 | - self::$cache->unwatch(); |
|
| 170 | - return false; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function setTTL($key, $ttl) { |
|
| 174 | - self::$cache->expire($this->getPrefix() . $key, $ttl); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - public static function isAvailable(): bool { |
|
| 178 | - return \OC::$server->getGetRedisFactory()->isAvailable(); |
|
| 179 | - } |
|
| 35 | + /** |
|
| 36 | + * @var \Redis $cache |
|
| 37 | + */ |
|
| 38 | + private static $cache = null; |
|
| 39 | + |
|
| 40 | + public function __construct($prefix = '', string $logFile = '') { |
|
| 41 | + parent::__construct($prefix); |
|
| 42 | + if (is_null(self::$cache)) { |
|
| 43 | + self::$cache = \OC::$server->getGetRedisFactory()->getInstance(); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function get($key) { |
|
| 48 | + $result = self::$cache->get($this->getPrefix() . $key); |
|
| 49 | + if ($result === false && !self::$cache->exists($this->getPrefix() . $key)) { |
|
| 50 | + return null; |
|
| 51 | + } else { |
|
| 52 | + return json_decode($result, true); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public function set($key, $value, $ttl = 0) { |
|
| 57 | + if ($ttl > 0) { |
|
| 58 | + return self::$cache->setex($this->getPrefix() . $key, $ttl, json_encode($value)); |
|
| 59 | + } else { |
|
| 60 | + return self::$cache->set($this->getPrefix() . $key, json_encode($value)); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public function hasKey($key) { |
|
| 65 | + return (bool)self::$cache->exists($this->getPrefix() . $key); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function remove($key) { |
|
| 69 | + if (self::$cache->del($this->getPrefix() . $key)) { |
|
| 70 | + return true; |
|
| 71 | + } else { |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function clear($prefix = '') { |
|
| 77 | + $prefix = $this->getPrefix() . $prefix . '*'; |
|
| 78 | + $keys = self::$cache->keys($prefix); |
|
| 79 | + $deleted = self::$cache->del($keys); |
|
| 80 | + |
|
| 81 | + return (is_array($keys) && (count($keys) === $deleted)); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Set a value in the cache if it's not already stored |
|
| 86 | + * |
|
| 87 | + * @param string $key |
|
| 88 | + * @param mixed $value |
|
| 89 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function add($key, $value, $ttl = 0) { |
|
| 93 | + // don't encode ints for inc/dec |
|
| 94 | + if (!is_int($value)) { |
|
| 95 | + $value = json_encode($value); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $args = ['nx']; |
|
| 99 | + if ($ttl !== 0 && is_int($ttl)) { |
|
| 100 | + $args['ex'] = $ttl; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return self::$cache->set($this->getPrefix() . $key, $value, $args); |
|
| 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->getPrefix() . $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->getPrefix() . $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->getPrefix() . $key); |
|
| 144 | + if ($this->get($key) === $old) { |
|
| 145 | + $result = self::$cache->multi() |
|
| 146 | + ->set($this->getPrefix() . $key, $new) |
|
| 147 | + ->exec(); |
|
| 148 | + return $result !== false; |
|
| 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->getPrefix() . $key); |
|
| 163 | + if ($this->get($key) === $old) { |
|
| 164 | + $result = self::$cache->multi() |
|
| 165 | + ->del($this->getPrefix() . $key) |
|
| 166 | + ->exec(); |
|
| 167 | + return $result !== false; |
|
| 168 | + } |
|
| 169 | + self::$cache->unwatch(); |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function setTTL($key, $ttl) { |
|
| 174 | + self::$cache->expire($this->getPrefix() . $key, $ttl); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + public static function isAvailable(): bool { |
|
| 178 | + return \OC::$server->getGetRedisFactory()->isAvailable(); |
|
| 179 | + } |
|
| 180 | 180 | } |