MemcacheSessionHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = array())
  41. {
  42. if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
  43. throw new \InvalidArgumentException(sprintf(
  44. 'The following options are not supported "%s"', implode(', ', $diff)
  45. ));
  46. }
  47. $this->memcache = $memcache;
  48. $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
  49. $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function open($savePath, $sessionName)
  55. {
  56. return true;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function close()
  62. {
  63. return true;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function read($sessionId)
  69. {
  70. return $this->memcache->get($this->prefix.$sessionId) ?: '';
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function write($sessionId, $data)
  76. {
  77. return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function destroy($sessionId)
  83. {
  84. $this->memcache->delete($this->prefix.$sessionId);
  85. return true;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function gc($maxlifetime)
  91. {
  92. // not required here because memcache will auto expire the records anyhow.
  93. return true;
  94. }
  95. /**
  96. * Return a Memcache instance.
  97. *
  98. * @return \Memcache
  99. */
  100. protected function getMemcache()
  101. {
  102. return $this->memcache;
  103. }
  104. }