RedisSessionHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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__, get_debug_type($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(string $sessionId): string
  63. {
  64. return $this->redis->get($this->prefix.$sessionId) ?: '';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function doWrite(string $sessionId, string $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(string $sessionId): bool
  78. {
  79. static $unlink = true;
  80. if ($unlink) {
  81. try {
  82. $unlink = false !== $this->redis->unlink($this->prefix.$sessionId);
  83. } catch (\Throwable $e) {
  84. $unlink = false;
  85. }
  86. }
  87. if (!$unlink) {
  88. $this->redis->del($this->prefix.$sessionId);
  89. }
  90. return true;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function close(): bool
  96. {
  97. return true;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function gc($maxlifetime): bool
  103. {
  104. return true;
  105. }
  106. /**
  107. * @return bool
  108. */
  109. public function updateTimestamp($sessionId, $data)
  110. {
  111. return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')));
  112. }
  113. }