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