MemcachedSessionHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  12. * Memcached based session storage handler based on the Memcached class
  13. * provided by the PHP memcached extension.
  14. *
  15. * @see https://php.net/memcached
  16. *
  17. * @author Drak <drak@zikula.org>
  18. */
  19. class MemcachedSessionHandler extends AbstractSessionHandler
  20. {
  21. private $memcached;
  22. /**
  23. * Time to live in seconds.
  24. */
  25. private ?int $ttl;
  26. /**
  27. * Key prefix for shared environments.
  28. */
  29. private string $prefix;
  30. /**
  31. * Constructor.
  32. *
  33. * List of available options:
  34. * * prefix: The prefix to use for the memcached keys in order to avoid collision
  35. * * ttl: The time to live in seconds.
  36. *
  37. * @throws \InvalidArgumentException When unsupported options are passed
  38. */
  39. public function __construct(\Memcached $memcached, array $options = [])
  40. {
  41. $this->memcached = $memcached;
  42. if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime', 'ttl'])) {
  43. throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
  44. }
  45. $this->ttl = $options['expiretime'] ?? $options['ttl'] ?? null;
  46. $this->prefix = $options['prefix'] ?? 'sf2s';
  47. }
  48. public function close(): bool
  49. {
  50. return $this->memcached->quit();
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function doRead(string $sessionId): string
  56. {
  57. return $this->memcached->get($this->prefix.$sessionId) ?: '';
  58. }
  59. public function updateTimestamp(string $sessionId, string $data): bool
  60. {
  61. $this->memcached->touch($this->prefix.$sessionId, $this->getCompatibleTtl());
  62. return true;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function doWrite(string $sessionId, string $data): bool
  68. {
  69. return $this->memcached->set($this->prefix.$sessionId, $data, $this->getCompatibleTtl());
  70. }
  71. private function getCompatibleTtl(): int
  72. {
  73. $ttl = (int) ($this->ttl ?? \ini_get('session.gc_maxlifetime'));
  74. // If the relative TTL that is used exceeds 30 days, memcached will treat the value as Unix time.
  75. // We have to convert it to an absolute Unix time at this point, to make sure the TTL is correct.
  76. if ($ttl > 60 * 60 * 24 * 30) {
  77. $ttl += time();
  78. }
  79. return $ttl;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function doDestroy(string $sessionId): bool
  85. {
  86. $result = $this->memcached->delete($this->prefix.$sessionId);
  87. return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
  88. }
  89. public function gc(int $maxlifetime): int|false
  90. {
  91. // not required here because memcached will auto expire the records anyhow.
  92. return 0;
  93. }
  94. /**
  95. * Return a Memcached instance.
  96. */
  97. protected function getMemcached(): \Memcached
  98. {
  99. return $this->memcached;
  100. }
  101. }