123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace OC\Memcache;
- use OCP\IMemcacheTTL;
- class Redis extends Cache implements IMemcacheTTL {
-
- public const LUA_SCRIPTS = [
- 'dec' => [
- 'if redis.call("exists", KEYS[1]) == 1 then return redis.call("decrby", KEYS[1], ARGV[1]) else return "NEX" end',
- '720b40cb66cef1579f2ef16ec69b3da8c85510e9',
- ],
- 'cas' => [
- 'if redis.call("get", KEYS[1]) == ARGV[1] then redis.call("set", KEYS[1], ARGV[2]) return 1 else return 0 end',
- '94eac401502554c02b811e3199baddde62d976d4',
- ],
- 'cad' => [
- 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end',
- 'cf0e94b2e9ffc7e04395cf88f7583fc309985910',
- ],
- ];
-
- private static $cache = null;
- public function __construct($prefix = '', string $logFile = '') {
- parent::__construct($prefix);
- }
-
- public function getCache() {
- if (is_null(self::$cache)) {
- self::$cache = \OC::$server->getGetRedisFactory()->getInstance();
- }
- return self::$cache;
- }
- public function get($key) {
- $result = $this->getCache()->get($this->getPrefix() . $key);
- if ($result === false) {
- return null;
- }
- return self::decodeValue($result);
- }
- public function set($key, $value, $ttl = 0) {
- $value = self::encodeValue($value);
- if ($ttl > 0) {
- return $this->getCache()->setex($this->getPrefix() . $key, $ttl, $value);
- } else {
- return $this->getCache()->set($this->getPrefix() . $key, $value);
- }
- }
- public function hasKey($key) {
- return (bool)$this->getCache()->exists($this->getPrefix() . $key);
- }
- public function remove($key) {
- if ($this->getCache()->unlink($this->getPrefix() . $key)) {
- return true;
- } else {
- return false;
- }
- }
- public function clear($prefix = '') {
-
- $prefix = $this->getPrefix() . $prefix . '*';
- $keys = $this->getCache()->keys($prefix);
- $deleted = $this->getCache()->del($keys);
- return (is_array($keys) && (count($keys) === $deleted));
- }
-
- public function add($key, $value, $ttl = 0) {
- $value = self::encodeValue($value);
- $args = ['nx'];
- if ($ttl !== 0 && is_int($ttl)) {
- $args['ex'] = $ttl;
- }
- return $this->getCache()->set($this->getPrefix() . $key, $value, $args);
- }
-
- public function inc($key, $step = 1) {
- return $this->getCache()->incrBy($this->getPrefix() . $key, $step);
- }
-
- public function dec($key, $step = 1) {
- $res = $this->evalLua('dec', [$key], [$step]);
- return ($res === 'NEX') ? false : $res;
- }
-
- public function cas($key, $old, $new) {
- $old = self::encodeValue($old);
- $new = self::encodeValue($new);
- return $this->evalLua('cas', [$key], [$old, $new]) > 0;
- }
-
- public function cad($key, $old) {
- $old = self::encodeValue($old);
- return $this->evalLua('cad', [$key], [$old]) > 0;
- }
- public function setTTL($key, $ttl) {
- $this->getCache()->expire($this->getPrefix() . $key, $ttl);
- }
- public static function isAvailable(): bool {
- return \OC::$server->getGetRedisFactory()->isAvailable();
- }
- protected function evalLua(string $scriptName, array $keys, array $args) {
- $keys = array_map(fn ($key) => $this->getPrefix() . $key, $keys);
- $args = array_merge($keys, $args);
- $script = self::LUA_SCRIPTS[$scriptName];
- $result = $this->getCache()->evalSha($script[1], $args, count($keys));
- if (false === $result) {
- $result = $this->getCache()->eval($script[0], $args, count($keys));
- }
- return $result;
- }
- protected static function encodeValue(mixed $value): string {
- return is_int($value) ? (string) $value : json_encode($value);
- }
- protected static function decodeValue(string $value): mixed {
- return is_numeric($value) ? (int) $value : json_decode($value, true);
- }
- }
|