NativeMemcacheSessionHandler.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. * NativeMemcacheSessionHandler.
  13. *
  14. * Driver for the memcache session save hadlers provided by the memcache PHP extension.
  15. *
  16. * @see http://php.net/memcache
  17. *
  18. * @author Drak <drak@zikula.org>
  19. */
  20. class NativeMemcacheSessionHandler extends NativeSessionHandler
  21. {
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $savePath Path of memcache server.
  26. * @param array $options Session configuration options.
  27. */
  28. public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array())
  29. {
  30. if (!extension_loaded('memcache')) {
  31. throw new \RuntimeException('PHP does not have "memcache" session module registered');
  32. }
  33. if (null === $savePath) {
  34. $savePath = ini_get('session.save_path');
  35. }
  36. ini_set('session.save_handler', 'memcache');
  37. ini_set('session.save_path', $savePath);
  38. $this->setOptions($options);
  39. }
  40. /**
  41. * Set any memcached ini values.
  42. *
  43. * @see http://php.net/memcache.ini
  44. */
  45. protected function setOptions(array $options)
  46. {
  47. foreach ($options as $key => $value) {
  48. if (in_array($key, array(
  49. 'memcache.allow_failover', 'memcache.max_failover_attempts',
  50. 'memcache.chunk_size', 'memcache.default_port', 'memcache.hash_strategy',
  51. 'memcache.hash_function', 'memcache.protocol', 'memcache.redundancy',
  52. 'memcache.session_redundancy', 'memcache.compress_threshold',
  53. 'memcache.lock_timeout'))) {
  54. ini_set($key, $value);
  55. }
  56. }
  57. }
  58. }