NativeMemcachedSessionHandler.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. * NativeMemcachedSessionHandler.
  13. *
  14. * Driver for the memcached session save hadlers provided by the memcached PHP extension.
  15. *
  16. * @see http://php.net/memcached.sessions
  17. *
  18. * @author Drak <drak@zikula.org>
  19. */
  20. class NativeMemcachedSessionHandler extends NativeSessionHandler
  21. {
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $savePath Comma separated list of servers: e.g. memcache1.example.com:11211,memcache2.example.com:11211
  26. * @param array $options Session configuration options.
  27. */
  28. public function __construct($savePath = '127.0.0.1:11211', array $options = array())
  29. {
  30. if (!extension_loaded('memcached')) {
  31. throw new \RuntimeException('PHP does not have "memcached" session module registered');
  32. }
  33. if (null === $savePath) {
  34. $savePath = ini_get('session.save_path');
  35. }
  36. ini_set('session.save_handler', 'memcached');
  37. ini_set('session.save_path', $savePath);
  38. $this->setOptions($options);
  39. }
  40. /**
  41. * Set any memcached ini values.
  42. *
  43. * @see https://github.com/php-memcached-dev/php-memcached/blob/master/memcached.ini
  44. */
  45. protected function setOptions(array $options)
  46. {
  47. foreach ($options as $key => $value) {
  48. if (in_array($key, array(
  49. 'memcached.sess_locking', 'memcached.sess_lock_wait',
  50. 'memcached.sess_prefix', 'memcached.compression_type',
  51. 'memcached.compression_factor', 'memcached.compression_threshold',
  52. 'memcached.serializer'))) {
  53. ini_set($key, $value);
  54. }
  55. }
  56. }
  57. }