RedisSessionHandler.php 3.0 KB

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