RedisSessionHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  11. use Predis\Response\ErrorInterface;
  12. use Symfony\Component\Cache\Traits\RedisClusterProxy;
  13. use Symfony\Component\Cache\Traits\RedisProxy;
  14. /**
  15. * Redis based session storage handler based on the Redis class
  16. * provided by the PHP redis extension.
  17. *
  18. * @author Dalibor Karlović <dalibor@flexolabs.io>
  19. */
  20. class RedisSessionHandler extends AbstractSessionHandler
  21. {
  22. private $redis;
  23. /**
  24. * @var string Key prefix for shared environments
  25. */
  26. private $prefix;
  27. /**
  28. * @var int Time to live in seconds
  29. */
  30. private $ttl;
  31. /**
  32. * List of available options:
  33. * * prefix: The prefix to use for the keys in order to avoid collision on the Redis server
  34. * * ttl: The time to live in seconds.
  35. *
  36. * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis
  37. *
  38. * @throws \InvalidArgumentException When unsupported client or options are passed
  39. */
  40. public function __construct($redis, array $options = [])
  41. {
  42. if (
  43. !$redis instanceof \Redis &&
  44. !$redis instanceof \RedisArray &&
  45. !$redis instanceof \RedisCluster &&
  46. !$redis instanceof \Predis\ClientInterface &&
  47. !$redis instanceof RedisProxy &&
  48. !$redis instanceof RedisClusterProxy
  49. ) {
  50. 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)));
  51. }
  52. if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
  53. throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
  54. }
  55. $this->redis = $redis;
  56. $this->prefix = $options['prefix'] ?? 'sf_s';
  57. $this->ttl = $options['ttl'] ?? null;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function doRead($sessionId): string
  63. {
  64. return $this->redis->get($this->prefix.$sessionId) ?: '';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function doWrite($sessionId, $data): bool
  70. {
  71. $result = $this->redis->setEx($this->prefix.$sessionId, (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')), $data);
  72. return $result && !$result instanceof ErrorInterface;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function doDestroy($sessionId): bool
  78. {
  79. $this->redis->del($this->prefix.$sessionId);
  80. return true;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function close(): bool
  86. {
  87. return true;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function gc($maxlifetime): bool
  93. {
  94. return true;
  95. }
  96. /**
  97. * @return bool
  98. */
  99. public function updateTimestamp($sessionId, $data)
  100. {
  101. return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')));
  102. }
  103. }