diff --git a/src/Cache/CacheFactory.php b/src/Cache/CacheFactory.php index 6dfa966..d8e25d1 100644 --- a/src/Cache/CacheFactory.php +++ b/src/Cache/CacheFactory.php @@ -20,6 +20,8 @@ public static function make(string $type = 'file'): Cacheable return new RedisStore(); case 'apcu': return new ApcuStore(); + case 'sentinel': + return new RedisSentinelStore(); } return new FileStore(); } diff --git a/src/Cache/RedisSentinelStore.php b/src/Cache/RedisSentinelStore.php new file mode 100644 index 0000000..df73901 --- /dev/null +++ b/src/Cache/RedisSentinelStore.php @@ -0,0 +1,105 @@ +redis = new RedisClient($params, $options); + } + + /** + * Get redis. + * + * @return RedisClient + */ + public function getRedis(): RedisClient + { + return $this->redis; + } + + /** + * {@inheritDoc} + */ + public function get(string $key, bool $withExpired = false) + { + $prefix = $this->getPrefix(); + + if (false === strpos($key, $prefix)) { + $key = $prefix . $key; + } + + $contents = $this->redis->get($key); + if (null !== $contents) { + $contents = json_decode($contents, true); + } + + if ($withExpired) { + return $contents; + } + + if ( ! $contents) { + return null; + } + + $isExpired = Carbon::parse($contents['expires_at'])->lt(Carbon::now()); + + return $isExpired ? null : $contents; + } + + /** + * {@inheritDoc} + */ + public function set(string $key, $value) + { + $contents = $this->get($key) ?? []; + + if (\is_array($value)) { + $contents = $value + $contents; + } else { + $contents[] = $value; + } + + $status = $this->redis->set($this->getPrefix() . $key, json_encode($contents)); + + return 'OK' === $status->getPayload(); + } + + /** + * {@inheritDoc} + */ + public function delete(string $key): bool + { + $prefix = $this->getPrefix(); + + if (false === strpos($key, $prefix)) { + $key = $prefix . $key; + } + + return $this->redis->del([$key]) > 0; + } + + /** + * {@inheritDoc} + */ + public function keys(): array + { + return $this->redis->keys($this->getPrefix() . '*'); + } +}