14 use Predis\Response\ErrorInterface;
15 use Symfony\Component\Cache\Traits\RedisClusterProxy;
16 use Symfony\Component\Cache\Traits\RedisProxy;
47 public function __construct($redis, array $options = [])
50 !$redis instanceof \Redis &&
51 !$redis instanceof \RedisArray &&
52 !$redis instanceof \RedisCluster &&
53 !$redis instanceof \Predis\ClientInterface &&
54 !$redis instanceof RedisProxy &&
55 !$redis instanceof RedisClusterProxy
57 throw new \InvalidArgumentException(sprintf(
'"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
60 if ($diff = array_diff(array_keys($options), [
'prefix',
'ttl'])) {
61 throw new \InvalidArgumentException(sprintf(
'The following options are not supported "%s".', implode(
', ', $diff)));
64 $this->redis = $redis;
65 $this->prefix = $options[
'prefix'] ??
'sf_s';
66 $this->ttl = $options[
'ttl'] ??
null;
72 protected function doRead($sessionId): string
74 return $this->redis->get($this->prefix.$sessionId) ?:
'';
80 protected function doWrite($sessionId, $data): bool
82 $result = $this->redis->setEx($this->prefix.$sessionId, (
int) ($this->ttl ?? ini_get(
'session.gc_maxlifetime')), $data);
84 return $result && !$result instanceof ErrorInterface;
90 protected function doDestroy($sessionId): bool
92 $this->redis->del($this->prefix.$sessionId);
100 public function close(): bool
108 public function gc($maxlifetime): bool
118 return (
bool) $this->redis->expire($this->prefix.$sessionId, (
int) ($this->ttl ?? ini_get(
'session.gc_maxlifetime')));