MemcachedSessionHandler.php 3.3 KB

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