Cache.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. class LtCache
  3. {
  4. public $configHandle;
  5. public $group;
  6. public $node;
  7. protected $ch;
  8. public function __construct()
  9. {
  10. if (! $this->configHandle instanceof LtConfig)
  11. {
  12. if (class_exists("LtObjectUtil", false))
  13. {
  14. $this->configHandle = LtObjectUtil::singleton("LtConfig");
  15. }
  16. else
  17. {
  18. $this->configHandle = new LtConfig;
  19. }
  20. }
  21. }
  22. public function init()
  23. {
  24. $this->ch = new LtCacheHandle;
  25. $this->ch->configHandle = $this->configHandle;
  26. $this->ch->init();
  27. $this->ch->group = $this->getGroup();
  28. $this->ch->node = $this->getNode();
  29. }
  30. public function getTDG($tableName)
  31. {
  32. $tdg = new LtCacheTableDataGateway;
  33. $tdg->tableName = $tableName;
  34. $tdg->ch = $this->ch;
  35. return $tdg;
  36. }
  37. public function changeNode($node)
  38. {
  39. $this->node = $node;
  40. $this->dbh->node = $node;
  41. }
  42. protected function getGroup()
  43. {
  44. if ($this->group)
  45. {
  46. return $this->group;
  47. }
  48. $servers = $this->configHandle->get("cache.servers");
  49. if (1 == count($servers))
  50. {
  51. return key($servers);
  52. }
  53. return false;
  54. }
  55. protected function getNode()
  56. {
  57. if ($this->node)
  58. {
  59. return $this->node;
  60. }
  61. $servers = $this->configHandle->get("cache.servers");
  62. if (1 == count($servers[$this->getGroup()]))
  63. {
  64. return key($servers[$this->getGroup()]);
  65. }
  66. return false;
  67. }
  68. }