RedisSessionHandler.php 2.8KB

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