ARedisConnection.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Represents a redis connection.
  4. *
  5. * @author Charles Pick
  6. * @package packages.redis
  7. */
  8. class ARedisConnection extends CApplicationComponent {
  9. /**
  10. * The redis client
  11. * @var Redis
  12. */
  13. protected $_client;
  14. /**
  15. * The redis server name
  16. * @var string
  17. */
  18. public $hostname = "localhost";
  19. /**
  20. * Redis default prefix
  21. * @var string
  22. */
  23. public $prefix = "Yii.redis.";
  24. /**
  25. * The redis server port
  26. * @var integer
  27. */
  28. public $port=6379;
  29. /**
  30. * The database to use, defaults to 1
  31. * @var integer
  32. */
  33. public $database=1;
  34. /**
  35. * The redis server password
  36. * @var password
  37. */
  38. public $password=null;
  39. /**
  40. * Sets the redis client to use with this connection
  41. * @param Redis $client the redis client instance
  42. */
  43. public function setClient(Redis $client)
  44. {
  45. $this->_client = $client;
  46. }
  47. /**
  48. * Gets the redis client
  49. * @return Redis the redis client
  50. */
  51. public function getClient()
  52. {
  53. if ($this->_client === null) {
  54. $this->_client = new Redis;
  55. $this->_client->connect($this->hostname, $this->port);
  56. if (isset($this->password)) {
  57. if ($this->_client->auth($this->password) === false) {
  58. throw new CException('Redis authentication failed!');
  59. }
  60. }
  61. $this->_client->setOption(Redis::OPT_PREFIX, $this->prefix);
  62. $this->_client->select($this->database);
  63. }
  64. return $this->_client;
  65. }
  66. /**
  67. * Returns a property value based on its name.
  68. * Do not call this method. This is a PHP magic method that we override
  69. * to allow using the following syntax to read a property
  70. * <pre>
  71. * $value=$component->propertyName;
  72. * </pre>
  73. * @param string $name the property name
  74. * @return mixed the property value
  75. * @throws CException if the property is not defined
  76. * @see __set
  77. */
  78. public function __get($name) {
  79. $getter='get'.$name;
  80. if (property_exists($this->getClient(),$name)) {
  81. return $this->getClient()->{$name};
  82. }
  83. elseif(method_exists($this->getClient(),$getter)) {
  84. return $this->$getter();
  85. }
  86. return parent::__get($name);
  87. }
  88. /**
  89. * Sets value of a component property.
  90. * Do not call this method. This is a PHP magic method that we override
  91. * to allow using the following syntax to set a property
  92. * <pre>
  93. * $this->propertyName=$value;
  94. * </pre>
  95. * @param string $name the property name
  96. * @param mixed $value the property value
  97. * @return mixed
  98. * @throws CException if the property is not defined or the property is read only.
  99. * @see __get
  100. */
  101. public function __set($name,$value)
  102. {
  103. $setter='set'.$name;
  104. if (property_exists($this->getClient(),$name)) {
  105. return $this->getClient()->{$name} = $value;
  106. }
  107. elseif(method_exists($this->getClient(),$setter)) {
  108. return $this->getClient()->{$setter}($value);
  109. }
  110. return parent::__set($name,$value);
  111. }
  112. /**
  113. * Checks if a property value is null.
  114. * Do not call this method. This is a PHP magic method that we override
  115. * to allow using isset() to detect if a component property is set or not.
  116. * @param string $name the property name
  117. * @return boolean
  118. */
  119. public function __isset($name)
  120. {
  121. $getter='get'.$name;
  122. if (property_exists($this->getClient(),$name)) {
  123. return true;
  124. }
  125. elseif (method_exists($this->getClient(),$getter)) {
  126. return true;
  127. }
  128. return parent::__isset($name);
  129. }
  130. /**
  131. * Sets a component property to be null.
  132. * Do not call this method. This is a PHP magic method that we override
  133. * to allow using unset() to set a component property to be null.
  134. * @param string $name the property name or the event name
  135. * @throws CException if the property is read only.
  136. * @return mixed
  137. */
  138. public function __unset($name)
  139. {
  140. $setter='set'.$name;
  141. if (property_exists($this->getClient(),$name)) {
  142. $this->getClient()->{$name} = null;
  143. }
  144. elseif(method_exists($this,$setter)) {
  145. $this->$setter(null);
  146. }
  147. else {
  148. parent::__unset($name);
  149. }
  150. }
  151. /**
  152. * Calls a method on the redis client with the given name.
  153. * Do not call this method. This is a PHP magic method that we override to
  154. * allow a facade in front of the redis object.
  155. * @param string $name the name of the method to call
  156. * @param array $parameters the parameters to pass to the method
  157. * @return mixed the response from the redis client
  158. */
  159. public function __call($name, $parameters) {
  160. return call_user_func_array(array($this->getClient(),$name),$parameters);
  161. }
  162. }