MemcachedSessionHandler.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 http://php.net/memcached
  16. *
  17. * @author Drak <drak@zikula.org>
  18. */
  19. class MemcachedSessionHandler extends AbstractSessionHandler
  20. {
  21. private $memcached;
  22. /**
  23. * @var int Time to live in seconds
  24. */
  25. private $ttl;
  26. /**
  27. * @var string Key prefix for shared environments
  28. */
  29. private $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. * * expiretime: The time to live in seconds.
  36. *
  37. * @param \Memcached $memcached A \Memcached instance
  38. * @param array $options An associative array of Memcached options
  39. *
  40. * @throws \InvalidArgumentException When unsupported options are passed
  41. */
  42. public function __construct(\Memcached $memcached, array $options = array())
  43. {
  44. $this->memcached = $memcached;
  45. if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
  46. throw new \InvalidArgumentException(sprintf(
  47. 'The following options are not supported "%s"', implode(', ', $diff)
  48. ));
  49. }
  50. $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
  51. $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function close()
  57. {
  58. return true;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doRead($sessionId)
  64. {
  65. return $this->memcached->get($this->prefix.$sessionId) ?: '';
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function updateTimestamp($sessionId, $data)
  71. {
  72. $this->memcached->touch($this->prefix.$sessionId, time() + $this->ttl);
  73. return true;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function doWrite($sessionId, $data)
  79. {
  80. return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function doDestroy($sessionId)
  86. {
  87. $result = $this->memcached->delete($this->prefix.$sessionId);
  88. return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function gc($maxlifetime)
  94. {
  95. // not required here because memcached will auto expire the records anyhow.
  96. return true;
  97. }
  98. /**
  99. * Return a Memcached instance.
  100. *
  101. * @return \Memcached
  102. */
  103. protected function getMemcached()
  104. {
  105. return $this->memcached;
  106. }
  107. }