Connection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace dcb9\redis;
  3. use Redis;
  4. use Yii;
  5. use yii\base\Configurable;
  6. use RedisException;
  7. /**
  8. * Class Connection
  9. * @package dcb9\redis
  10. */
  11. class Connection extends Redis implements Configurable
  12. {
  13. /**
  14. * @var string the hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'.
  15. * If [[unixSocket]] is specified, hostname and port will be ignored.
  16. */
  17. public $hostname = 'localhost';
  18. /**
  19. * @var integer the port to use for connecting to the redis server. Default port is 6379.
  20. * If [[unixSocket]] is specified, hostname and port will be ignored.
  21. */
  22. public $port = 6379;
  23. /**
  24. * @var string the unix socket path (e.g. `/var/run/redis/redis.sock`) to use for connecting to the redis server.
  25. * This can be used instead of [[hostname]] and [[port]] to connect to the server using a unix socket.
  26. * If a unix socket path is specified, [[hostname]] and [[port]] will be ignored.
  27. */
  28. public $unixSocket;
  29. /**
  30. * @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is send.
  31. * See http://redis.io/commands/auth
  32. */
  33. public $password;
  34. /**
  35. * @var integer the redis database to use. This is an integer value starting from 0. Defaults to 0.
  36. */
  37. public $database = 0;
  38. /**
  39. * @var float value in seconds (optional, default is 0.0 meaning unlimited)
  40. */
  41. public $connectionTimeout = 0.0;
  42. /**
  43. * Constructor.
  44. * The default implementation does two things:
  45. *
  46. * - Initializes the object with the given configuration `$config`.
  47. * - Call [[init()]].
  48. *
  49. * If this method is overridden in a child class, it is recommended that
  50. *
  51. * - the last parameter of the constructor is a configuration array, like `$config` here.
  52. * - call the parent implementation at the end of the constructor.
  53. *
  54. * @param array $config name-value pairs that will be used to initialize the object properties
  55. */
  56. public function __construct($config = [])
  57. {
  58. if (!empty($config)) {
  59. Yii::configure($this, $config);
  60. }
  61. }
  62. /**
  63. * Returns the fully qualified name of this class.
  64. * @return string the fully qualified name of this class.
  65. */
  66. public static function className()
  67. {
  68. return get_called_class();
  69. }
  70. /**
  71. * Establishes a DB connection.
  72. * It does nothing if a DB connection has already been established.
  73. * @throws RedisException if connection fails
  74. */
  75. public function open($host = null, $port = null, $timeout = null, $retry_interval = 0)
  76. {
  77. if ($this->unixSocket !== null) {
  78. $isConnected = $this->connect($this->unixSocket);
  79. }
  80. else {
  81. if(is_null($host)){
  82. $host = $this->hostname;
  83. }
  84. if(is_null($port)){
  85. $port = $this->port;
  86. }
  87. if(is_null($timeout)){
  88. $timeout = $this->connectionTimeout;
  89. }
  90. $isConnected = $this->connect($host, $port, $timeout, $retry_interval);
  91. }
  92. if ($isConnected === false) {
  93. throw new RedisException('Connect to redis server error.');
  94. }
  95. if ($this->password !== null) {
  96. $this->auth($this->password);
  97. }
  98. if ($this->database !== null) {
  99. $this->select($this->database);
  100. }
  101. }
  102. /**
  103. * @return bool
  104. */
  105. public function ping()
  106. {
  107. return parent::ping() === '+PONG';
  108. }
  109. /**
  110. * @param null $async
  111. * @return bool
  112. */
  113. public function flushdb($async = NULL)
  114. {
  115. return parent::flushDB($async);
  116. }
  117. }