RedisSessionHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * Key prefix for shared environments.
  25. */
  26. private string $prefix;
  27. /**
  28. * Time to live in seconds.
  29. */
  30. private ?int $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. * @throws \InvalidArgumentException When unsupported client or options are passed
  37. */
  38. public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redis, array $options = [])
  39. {
  40. if ($diff = array_diff(array_keys($options), ['prefix', 'ttl'])) {
  41. throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
  42. }
  43. $this->redis = $redis;
  44. $this->prefix = $options['prefix'] ?? 'sf_s';
  45. $this->ttl = $options['ttl'] ?? null;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function doRead(string $sessionId): string
  51. {
  52. return $this->redis->get($this->prefix.$sessionId) ?: '';
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function doWrite(string $sessionId, string $data): bool
  58. {
  59. $result = $this->redis->setEx($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')), $data);
  60. return $result && !$result instanceof ErrorInterface;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function doDestroy(string $sessionId): bool
  66. {
  67. static $unlink = true;
  68. if ($unlink) {
  69. try {
  70. $unlink = false !== $this->redis->unlink($this->prefix.$sessionId);
  71. } catch (\Throwable $e) {
  72. $unlink = false;
  73. }
  74. }
  75. if (!$unlink) {
  76. $this->redis->del($this->prefix.$sessionId);
  77. }
  78. return true;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. #[\ReturnTypeWillChange]
  84. public function close(): bool
  85. {
  86. return true;
  87. }
  88. public function gc(int $maxlifetime): int|false
  89. {
  90. return 0;
  91. }
  92. public function updateTimestamp(string $sessionId, string $data): bool
  93. {
  94. return $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime')));
  95. }
  96. }