Manager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Client pool manager for multi-server configurations
  4. */
  5. class Redis_Client_Manager
  6. {
  7. /**
  8. * Redis default host
  9. */
  10. const REDIS_DEFAULT_HOST = '127.0.0.1';
  11. /**
  12. * Redis default port
  13. */
  14. const REDIS_DEFAULT_PORT = 6379;
  15. /**
  16. * Redis default socket (will override host and port)
  17. */
  18. const REDIS_DEFAULT_SOCKET = null;
  19. /**
  20. * Redis default database: will select none (Database 0)
  21. */
  22. const REDIS_DEFAULT_BASE = null;
  23. /**
  24. * Redis default password: will not authenticate
  25. */
  26. const REDIS_DEFAULT_PASSWORD = null;
  27. /**
  28. * Default realm
  29. */
  30. const REALM_DEFAULT = 'default';
  31. /**
  32. * Client interface name (PhpRedis or Predis)
  33. *
  34. * @var string
  35. */
  36. private $interfaceName;
  37. /**
  38. * @var array[]
  39. */
  40. private $serverList = array();
  41. /**
  42. * @var mixed[]
  43. */
  44. private $clients = array();
  45. /**
  46. * @var Redis_Client_FactoryInterface
  47. */
  48. private $factory;
  49. /**
  50. * Default constructor
  51. *
  52. * @param Redis_Client_FactoryInterface $factory
  53. * Client factory
  54. * @param array $serverList
  55. * Server connection info list
  56. */
  57. public function __construct(Redis_Client_FactoryInterface $factory, $serverList = array())
  58. {
  59. $this->factory = $factory;
  60. $this->serverList = $serverList;
  61. }
  62. /**
  63. * Get client for the given realm
  64. *
  65. * @param string $realm
  66. * @param boolean $allowDefault
  67. *
  68. * @return mixed
  69. */
  70. public function getClient($realm = self::REALM_DEFAULT, $allowDefault = true)
  71. {
  72. if (!isset($this->clients[$realm])) {
  73. $client = $this->createClient($realm);
  74. if (false === $client) {
  75. if (self::REALM_DEFAULT !== $realm && $allowDefault) {
  76. $this->clients[$realm] = $this->getClient(self::REALM_DEFAULT);
  77. } else {
  78. throw new InvalidArgumentException(sprintf("Could not find client for realm '%s'", $realm));
  79. }
  80. } else {
  81. $this->clients[$realm] = $client;
  82. }
  83. }
  84. return $this->clients[$realm];
  85. }
  86. /**
  87. * Build connection parameters array from current Drupal settings
  88. *
  89. * @param string $realm
  90. *
  91. * @return boolean|string[]
  92. * A key-value pairs of configuration values or false if realm is
  93. * not defined per-configuration
  94. */
  95. private function buildOptions($realm)
  96. {
  97. $info = null;
  98. if (isset($this->serverList[$realm])) {
  99. $info = $this->serverList[$realm];
  100. } else {
  101. return false;
  102. }
  103. $info += array(
  104. 'host' => self::REDIS_DEFAULT_HOST,
  105. 'port' => self::REDIS_DEFAULT_PORT,
  106. 'base' => self::REDIS_DEFAULT_BASE,
  107. 'password' => self::REDIS_DEFAULT_PASSWORD,
  108. 'socket' => self::REDIS_DEFAULT_SOCKET
  109. );
  110. return array_filter($info);
  111. }
  112. /**
  113. * Get client singleton
  114. */
  115. private function createClient($realm)
  116. {
  117. $info = $this->buildOptions($realm);
  118. if (false === $info) {
  119. return false;
  120. }
  121. return $this->factory->getClient($info);
  122. }
  123. }