RedisSessionHandler.php 3.1 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. * @var string Key prefix for shared environments
  25. */
  26. private $prefix;
  27. /**
  28. * List of available options:
  29. * * prefix: The prefix to use for the keys in order to avoid collision on the Redis server.
  30. *
  31. * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|RedisProxy $redis
  32. * @param array $options An associative array of options
  33. *
  34. * @throws \InvalidArgumentException When unsupported client or options are passed
  35. */
  36. public function __construct($redis, array $options = [])
  37. {
  38. if (
  39. !$redis instanceof \Redis &&
  40. !$redis instanceof \RedisArray &&
  41. !$redis instanceof \RedisCluster &&
  42. !$redis instanceof \Predis\Client &&
  43. !$redis instanceof RedisProxy &&
  44. !$redis instanceof RedisClusterProxy
  45. ) {
  46. throw new \InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, \is_object($redis) ? \get_class($redis) : \gettype($redis)));
  47. }
  48. if ($diff = array_diff(array_keys($options), ['prefix'])) {
  49. throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff)));
  50. }
  51. $this->redis = $redis;
  52. $this->prefix = $options['prefix'] ?? 'sf_s';
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function doRead($sessionId): string
  58. {
  59. return $this->redis->get($this->prefix.$sessionId) ?: '';
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function doWrite($sessionId, $data): bool
  65. {
  66. $result = $this->redis->setEx($this->prefix.$sessionId, (int) ini_get('session.gc_maxlifetime'), $data);
  67. return $result && !$result instanceof ErrorInterface;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function doDestroy($sessionId): bool
  73. {
  74. $this->redis->del($this->prefix.$sessionId);
  75. return true;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function close(): bool
  81. {
  82. return true;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function gc($maxlifetime): bool
  88. {
  89. return true;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function updateTimestamp($sessionId, $data)
  95. {
  96. return (bool) $this->redis->expire($this->prefix.$sessionId, (int) ini_get('session.gc_maxlifetime'));
  97. }
  98. }