persist = $providerInfo->isPersistent(); $this->redis = new Redis; $this->redis->setOption(Redis::OPT_PREFIX, $providerInfo->getPrefix()); $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); if($this->persist) $this->redis->pconnect($providerInfo->getServerHost(), $providerInfo->getServerPort()); else $this->redis->connect($providerInfo->getServerHost(), $providerInfo->getServerPort()); if($providerInfo->hasPassword()) { if($providerInfo->hasUsername()) $this->redis->auth([$providerInfo->getUsername(), $providerInfo->getPassword()]); else $this->redis->auth($providerInfo->getPassword()); } if($providerInfo->hasDatabaseNumber()) $this->redis->select($providerInfo->getDatabaseNumber()); } public function get(string $key): mixed { $result = $this->redis->get($key); return $result === false ? null : $result; } public function set(string $key, mixed $value, int $ttl = 0): void { if($ttl < 0) throw new InvalidArgumentException('$ttl must be equal to or greater than 0.'); $this->redis->setEx($key, $ttl, $value); } public function delete(string $key): void { $this->redis->unlink($key); } public function touch(string $key, int $ttl = 0): void { if($ttl < 0) throw new InvalidArgumentException('$ttl must be equal to or greater than 0.'); $this->redis->expire($key, $ttl); } public function increment(string $key, int $amount = 1): int { return $this->redis->incrBy($key, $amount); } public function decrement(string $key, int $amount = 1): int { return $this->redis->decrBy($key, $amount); } public function close(): void { if(!$this->persist) $this->redis->close(); } public function __destruct() { $this->close(); } }