菜谱项目

MemcacheSessionHandler.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * List of available options:
  27. * * prefix: The prefix to use for the memcache keys in order to avoid collision
  28. * * expiretime: The time to live in seconds
  29. *
  30. * @param \Memcache $memcache A \Memcache instance
  31. * @param array $options An associative array of Memcache options
  32. *
  33. * @throws \InvalidArgumentException When unsupported options are passed
  34. */
  35. public function __construct(\Memcache $memcache, array $options = array())
  36. {
  37. if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
  38. throw new \InvalidArgumentException(sprintf(
  39. 'The following options are not supported "%s"', implode(', ', $diff)
  40. ));
  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. }