MemcacheSessionHandler.php 2.7 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. /**
  12. * MemcacheSessionHandler.
  13. *
  14. * @author Drak <drak@zikula.org>
  15. */
  16. class MemcacheSessionHandler implements \SessionHandlerInterface
  17. {
  18. /**
  19. * @var \Memcache Memcache driver
  20. */
  21. private $memcache;
  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 memcache keys in order to avoid collision
  35. * * expiretime: The time to live in seconds
  36. *
  37. * @param \Memcache $memcache A \Memcache instance
  38. * @param array $options An associative array of Memcache options
  39. *
  40. * @throws \InvalidArgumentException When unsupported options are passed
  41. */
  42. public function __construct(\Memcache $memcache, array $options = array())
  43. {
  44. if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
  45. throw new \InvalidArgumentException(sprintf(
  46. 'The following options are not supported "%s"', implode(', ', $diff)
  47. ));
  48. }
  49. $this->memcache = $memcache;
  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 open($savePath, $sessionName)
  57. {
  58. return true;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function close()
  64. {
  65. return true;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function read($sessionId)
  71. {
  72. return $this->memcache->get($this->prefix.$sessionId) ?: '';
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function write($sessionId, $data)
  78. {
  79. return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function destroy($sessionId)
  85. {
  86. return $this->memcache->delete($this->prefix.$sessionId);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function gc($maxlifetime)
  92. {
  93. // not required here because memcache will auto expire the records anyhow.
  94. return true;
  95. }
  96. /**
  97. * Return a Memcache instance.
  98. *
  99. * @return \Memcache
  100. */
  101. protected function getMemcache()
  102. {
  103. return $this->memcache;
  104. }
  105. }