MemcachedSessionHandler.php 2.9 KB

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