MemcacheSessionHandler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Memcache driver.
  20. *
  21. * @var \Memcache
  22. */
  23. private $memcache;
  24. /**
  25. * Configuration options.
  26. *
  27. * @var array
  28. */
  29. private $memcacheOptions;
  30. /**
  31. * Key prefix for shared environments.
  32. *
  33. * @var string
  34. */
  35. private $prefix;
  36. /**
  37. * Constructor.
  38. *
  39. * @param \Memcache $memcache A \Memcache instance
  40. * @param array $memcacheOptions An associative array of Memcache options
  41. * @param array $options Session configuration options.
  42. */
  43. public function __construct(\Memcache $memcache, array $memcacheOptions = array(), array $options = array())
  44. {
  45. $this->memcache = $memcache;
  46. // defaults
  47. if (!isset($memcacheOptions['serverpool'])) {
  48. $memcacheOptions['serverpool'] = array(array(
  49. 'host' => '127.0.0.1',
  50. 'port' => 11211,
  51. 'timeout' => 1,
  52. 'persistent' => false,
  53. 'weight' => 1,
  54. 'retry_interval' => 15,
  55. ));
  56. }
  57. $memcacheOptions['expiretime'] = isset($memcacheOptions['expiretime']) ? (int)$memcacheOptions['expiretime'] : 86400;
  58. $this->prefix = isset($memcacheOptions['prefix']) ? $memcacheOptions['prefix'] : 'sf2s';
  59. $this->memcacheOptions = $memcacheOptions;
  60. }
  61. protected function addServer(array $server)
  62. {
  63. if (!array_key_exists('host', $server)) {
  64. throw new \InvalidArgumentException('host key must be set');
  65. }
  66. $server['port'] = isset($server['port']) ? (int)$server['port'] : 11211;
  67. $server['timeout'] = isset($server['timeout']) ? (int)$server['timeout'] : 1;
  68. $server['persistent'] = isset($server['persistent']) ? (bool)$server['persistent'] : false;
  69. $server['weight'] = isset($server['weight']) ? (int)$server['weight'] : 1;
  70. $server['retry_interval'] = isset($server['retry_interval']) ? (int)$server['retry_interval'] : 15;
  71. $this->memcache->addserver($server['host'], $server['port'], $server['persistent'],$server['weight'],$server['timeout'],$server['retry_interval']);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function open($savePath, $sessionName)
  77. {
  78. foreach ($this->memcacheOptions['serverpool'] as $server) {
  79. $this->addServer($server);
  80. }
  81. return true;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function close()
  87. {
  88. return $this->memcache->close();
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function read($sessionId)
  94. {
  95. return $this->memcache->get($this->prefix.$sessionId) ?: '';
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function write($sessionId, $data)
  101. {
  102. return $this->memcache->set($this->prefix.$sessionId, $data, 0, $this->memcacheOptions['expiretime']);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function destroy($sessionId)
  108. {
  109. return $this->memcache->delete($this->prefix.$sessionId);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function gc($lifetime)
  115. {
  116. // not required here because memcache will auto expire the records anyhow.
  117. return true;
  118. }
  119. }