CacheConfigBuilder.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. class LtCacheConfigBuilder
  3. {
  4. protected $servers = array();
  5. protected $defaultConfig = array(
  6. "adapter" => "phps", //apc,xcach,ea; file, phps; memcached
  7. //"prefix" => ""
  8. //"host" => "localhost", //some ip, hostname
  9. //"port" => 3306,
  10. );
  11. public function addSingleHost($hostConfig)
  12. {
  13. $this->addHost("group_0", "node_0", "master", $hostConfig);
  14. }
  15. public function addHost($groupId, $nodeId = "node_0", $role = "master", $hostConfig)
  16. {
  17. if (isset($this->servers[$groupId][$nodeId][$role]))
  18. {//以相同role的第一个host为默认配置
  19. $ref = $this->servers[$groupId][$nodeId][$role][0];
  20. }
  21. else if ("slave" == $role && isset($this->servers[$groupId][$nodeId]["master"]))
  22. {//slave host以master的第一个host为默认配置
  23. $ref = $this->servers[$groupId][$nodeId]["master"][0];
  24. }
  25. else if (isset($this->servers[$groupId]) && count($this->servers[$groupId]))
  26. {//以本group第一个node的master第一个host为默认配置
  27. $refNode = key($this->servers[$groupId]);
  28. $ref = $this->servers[$groupId][$refNode]["master"][0];
  29. }
  30. else
  31. {
  32. if (!isset($hostConfig["adapter"]))
  33. {
  34. trigger_error("No db adapter specified");
  35. }
  36. $ref = $this->defaultConfig;
  37. }
  38. $conf = array_merge($ref, $hostConfig);
  39. $this->servers[$groupId][$nodeId][$role][] = $conf;
  40. }
  41. public function getServers()
  42. {
  43. return $this->servers;
  44. }
  45. }