MemcacheSessionHandler.php 2.6 KB

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