/XoopsEngine/lib/Kernel/Service/Memcached.php

https://github.com/xoops-pi/engine · PHP · 122 lines · 91 code · 10 blank · 21 comment · 20 complexity · 670ff6a87ffb3962c3750417584c4698 MD5 · raw file

  1. <?php
  2. /**
  3. * Kernel service
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright Xoops Engine http://www.xoopsengine.org/
  13. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  14. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  15. * @since 3.0
  16. * @package Kernel/Service
  17. * @version $Id$
  18. */
  19. namespace Kernel\Service;
  20. class Memcached extends ServiceAbstract
  21. {
  22. protected static $instances = array();
  23. const DEFAULT_PORT = 11211;
  24. const DEFAULT_WEIGHT = 1;
  25. protected function loadOptions($config)
  26. {
  27. if (is_string($config)) {
  28. $config = \Xoops::loadConfig("memcached." . $config . ".ini.php");
  29. }
  30. $options = array();
  31. if (!empty($config['client'])) {
  32. $clients = array();
  33. // setup memcached client options
  34. foreach ($config['client'] as $name => $value) {
  35. $optId = null;
  36. if (is_int($name)) {
  37. $optId = $name;
  38. } else {
  39. $optConst = 'Memcached::OPT_' . strtoupper($name);
  40. if (defined($optConst)) {
  41. $optId = constant($optConst);
  42. } else {
  43. trigger_error("Unknown memcached client option '{$name}' ({$optConst})");
  44. }
  45. }
  46. if ($optId) {
  47. if (is_string($value)) {
  48. $memcachedValue = 'Memcached::' . strtoupper($value);
  49. $value = defined($memcachedValue) ? constant($memcachedValue) : $value; // For Memcached predefined constants, see http://www.php.net/manual/en/memcached.constants.php
  50. }
  51. $clients[$optId] = $value;
  52. }
  53. }
  54. if (!empty($clients)) {
  55. $options['clients'] = $clients;
  56. }
  57. unset($config['client']);
  58. }
  59. // setup memcached servers
  60. $serverList = isset($config['servers']) ? $config['servers'] : $config;
  61. if (isset($serverList['host'])) {
  62. $serverList = array(0 => $serverList); // Transform it into associative arrays
  63. }
  64. $servers = array();
  65. foreach ($serverList as $idx => $server) {
  66. if (!array_key_exists('port', $server)) {
  67. $server['port'] = static::DEFAULT_PORT;
  68. }
  69. if (!array_key_exists('weight', $server)) {
  70. $server['weight'] = static::DEFAULT_WEIGHT;
  71. }
  72. $servers[] = array($server['host'], $server['port'], $server['weight']);
  73. }
  74. if (!empty($servers)) {
  75. $options['servers'] = $servers;
  76. } else {
  77. $options = array();
  78. }
  79. return $options;
  80. }
  81. public function load($config = null)
  82. {
  83. if (!extension_loaded('memcached')) {
  84. throw new exception('Memcached extension is not available!');
  85. }
  86. // Load default Memcached handler from Xoops::persist to keep consistency
  87. if (empty($config)) {
  88. return \Xoops::persist()->loadHandler("Memcached")->getEngine();
  89. }
  90. $configKey = is_array($config) ? serialize($config) : $config;
  91. if (isset(static::$instances[$configKey])) {
  92. return static::$instances[$configKey];
  93. }
  94. static::$instances[$configKey] = false;
  95. $options = $this->loadOptions($config);
  96. if (empty($options)) {
  97. throw new \exception('No valid options!');
  98. }
  99. $memcached = new \Memcached;
  100. if (!empty($options['client'])) {
  101. // setup memcached client options
  102. foreach ($options['client'] as $optId => $value) {
  103. if (!$memcached->setOption($optId, $value)) {
  104. trigger_error("Setting memcached client option '{$optId}' failed");
  105. }
  106. }
  107. }
  108. $memcached->addServers($options['servers']);
  109. static::$instances[$configKey] = $memcached;
  110. return static::$instances[$configKey];
  111. }
  112. }