MemcacheSessionHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. @trigger_error(sprintf('The class %s is deprecated since Symfony 3.4 and will be removed in 4.0. Use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler instead.', MemcacheSessionHandler::class), E_USER_DEPRECATED);
  12. /**
  13. * @author Drak <drak@zikula.org>
  14. *
  15. * @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler instead.
  16. */
  17. class MemcacheSessionHandler implements \SessionHandlerInterface
  18. {
  19. private $memcache;
  20. /**
  21. * @var int Time to live in seconds
  22. */
  23. private $ttl;
  24. /**
  25. * @var string Key prefix for shared environments
  26. */
  27. private $prefix;
  28. /**
  29. * Constructor.
  30. *
  31. * List of available options:
  32. * * prefix: The prefix to use for the memcache keys in order to avoid collision
  33. * * expiretime: The time to live in seconds
  34. *
  35. * @param \Memcache $memcache A \Memcache instance
  36. * @param array $options An associative array of Memcache options
  37. *
  38. * @throws \InvalidArgumentException When unsupported options are passed
  39. */
  40. public function __construct(\Memcache $memcache, array $options = [])
  41. {
  42. if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) {
  43. throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff)));
  44. }
  45. $this->memcache = $memcache;
  46. $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
  47. $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function open($savePath, $sessionName)
  53. {
  54. return true;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function close()
  60. {
  61. return true;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function read($sessionId)
  67. {
  68. return $this->memcache->get($this->prefix.$sessionId) ?: '';
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function write($sessionId, $data)
  74. {
  75. return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function destroy($sessionId)
  81. {
  82. $this->memcache->delete($this->prefix.$sessionId);
  83. return true;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function gc($maxlifetime)
  89. {
  90. // not required here because memcache will auto expire the records anyhow.
  91. return true;
  92. }
  93. /**
  94. * Return a Memcache instance.
  95. *
  96. * @return \Memcache
  97. */
  98. protected function getMemcache()
  99. {
  100. return $this->memcache;
  101. }
  102. }