1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
trait Connection |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Connects to a Redis instance. |
9
|
|
|
* |
10
|
|
|
* @param string $host can be a host, or the path to a unix domain socket. Starting from |
11
|
|
|
* version 5.0.0 it is possible to specify |
12
|
|
|
* @param int|int $port optional defaults to 6379 |
13
|
|
|
* @param float|int $timeout value in seconds (optional, default is 0 meaning unlimited) |
14
|
|
|
* @param null $reserved should be NULL if retry_interval is specified |
15
|
|
|
* @param int|int $retry_interval value in milliseconds (optional) |
16
|
|
|
* @param float|int $read_timeout value in seconds (optional, default is 0 meaning unlimited) |
17
|
|
|
* |
18
|
|
|
* @return bool true on success, false on error |
19
|
|
|
* |
20
|
|
|
* @throws RedisException |
21
|
|
|
*/ |
22
|
|
|
public function connect( |
23
|
|
|
string $host = '127.0.0.1', |
24
|
|
|
int $port = 6379, |
25
|
|
|
float $timeout = 0.0, |
26
|
|
|
$reserved = null, |
27
|
|
|
int $retry_interval = 0, |
28
|
|
|
float $read_timeout = 0 |
29
|
|
|
): bool { |
30
|
|
|
return $this->redis->connect($host, $port, $timeout, $reserved, $retry_interval, $read_timeout); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Connects to a Redis instance. |
35
|
|
|
* |
36
|
|
|
* @param string $host can be a host, or the path to a unix domain socket. Starting from |
37
|
|
|
* version 5.0.0 it is possible to specify |
38
|
|
|
* @param int|int $port optional defaults to 6379 |
39
|
|
|
* @param float|int $timeout value in seconds (optional, default is 0 meaning unlimited) |
40
|
|
|
* @param null $reserved should be NULL if retry_interval is specified |
41
|
|
|
* @param int|int $retry_interval value in milliseconds (optional) |
42
|
|
|
* @param float|int $read_timeout value in seconds (optional, default is 0 meaning unlimited) |
43
|
|
|
* |
44
|
|
|
* @return bool true on success, false on error |
45
|
|
|
* |
46
|
|
|
* @throws RedisException |
47
|
|
|
*/ |
48
|
|
|
public function open( |
49
|
|
|
string $host = '127.0.0.1', |
50
|
|
|
int $port = 6379, |
51
|
|
|
float $timeout = 0.0, |
52
|
|
|
$reserved = null, |
53
|
|
|
int $retry_interval = 0, |
54
|
|
|
float $read_timeout = 0 |
55
|
|
|
): bool { |
56
|
|
|
return $this->connect($host, $port, $timeout, $reserved, $retry_interval, $read_timeout); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Connects to a Redis instance or reuse a connection already established with pconnect/popen. |
61
|
|
|
* |
62
|
|
|
* The connection will not be closed on end of request until the php process ends. So be prepared for too many |
63
|
|
|
* open FD's errors (specially on redis server side) when using persistent connections on many servers |
64
|
|
|
* connecting to one redis server. |
65
|
|
|
* |
66
|
|
|
* Also more than one persistent connection can be made identified by either host + port + timeout or |
67
|
|
|
* host + persistent_id or unix socket + timeout. |
68
|
|
|
* |
69
|
|
|
* Starting from version 4.2.1, it became possible to use connection pooling by setting INI variable |
70
|
|
|
* redis.pconnect.pooling_enabled to 1. |
71
|
|
|
* |
72
|
|
|
* This feature is not available in threaded versions. pconnect and popen then working like their non |
73
|
|
|
* persistent equivalents. |
74
|
|
|
* |
75
|
|
|
* @param string $host can be a host, or the path to a unix domain socket. Starting from version |
76
|
|
|
* 5.0.0 it is possible to specify |
77
|
|
|
* @param int|int $port optional defaults to 6379 |
78
|
|
|
* @param float|int $timeout value in seconds (optional, default is 0 meaning unlimited) |
79
|
|
|
* @param string $persistent_id identity for the requested persistent connection |
80
|
|
|
* @param int|int $retry_interval value in milliseconds (optional) |
81
|
|
|
* @param float|int $read_timeout value in seconds (optional, default is 0 meaning unlimited) |
82
|
|
|
* |
83
|
|
|
* @return bool true on success, false on error |
84
|
|
|
* |
85
|
|
|
* @throws RedisException |
86
|
|
|
*/ |
87
|
|
|
public function pconnect( |
88
|
|
|
string $host, |
89
|
|
|
int $port, |
90
|
|
|
float $timeout, |
91
|
|
|
string $persistent_id, |
92
|
|
|
?int $retry_interval = 0, |
93
|
|
|
?float $read_timeout = 0 |
94
|
|
|
): bool { |
95
|
|
|
return $this->redis->pconnect($host, $port, $timeout, $persistent_id, $retry_interval, $read_timeout); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Connects to a Redis instance or reuse a connection already established with pconnect/popen. |
100
|
|
|
* |
101
|
|
|
* The connection will not be closed on end of request until the php process ends. So be prepared for too many |
102
|
|
|
* open FD's errors (specially on redis server side) when using persistent connections on many servers |
103
|
|
|
* connecting to one redis server. |
104
|
|
|
* |
105
|
|
|
* Also more than one persistent connection can be made identified by either host + port + timeout or |
106
|
|
|
* host + persistent_id or unix socket + timeout. |
107
|
|
|
* |
108
|
|
|
* Starting from version 4.2.1, it became possible to use connection pooling by setting INI variable |
109
|
|
|
* redis.pconnect.pooling_enabled to 1. |
110
|
|
|
* |
111
|
|
|
* This feature is not available in threaded versions. pconnect and popen then working like their non |
112
|
|
|
* persistent equivalents. |
113
|
|
|
* |
114
|
|
|
* @param string $host can be a host, or the path to a unix domain socket. Starting from version |
115
|
|
|
* 5.0.0 it is possible to specify |
116
|
|
|
* @param int|int $port optional defaults to 6379 |
117
|
|
|
* @param float|int $timeout value in seconds (optional, default is 0 meaning unlimited) |
118
|
|
|
* @param string $persistent_id identity for the requested persistent connection |
119
|
|
|
* @param int|int $retry_interval value in milliseconds (optional) |
120
|
|
|
* @param float|int $read_timeout value in seconds (optional, default is 0 meaning unlimited) |
121
|
|
|
* |
122
|
|
|
* @return bool true on success, false on error |
123
|
|
|
* |
124
|
|
|
* @throws RedisException |
125
|
|
|
*/ |
126
|
|
|
public function popen( |
127
|
|
|
string $host, |
128
|
|
|
int $port, |
129
|
|
|
float $timeout, |
130
|
|
|
string $persistent_id, |
131
|
|
|
?int $retry_interval = 0, |
132
|
|
|
?float $read_timeout = 0 |
133
|
|
|
): bool { |
134
|
|
|
return $this->pconnect($host, $port, $timeout, $persistent_id, $retry_interval, $read_timeout); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Authenticate the connection using a password. Warning: The password is sent in plain-text over the network. |
139
|
|
|
* |
140
|
|
|
* @param string $password |
141
|
|
|
* |
142
|
|
|
* @return bool true if the connection is authenticated, false otherwise. |
143
|
|
|
*/ |
144
|
|
|
public function auth(string $password): bool |
145
|
|
|
{ |
146
|
|
|
return $this->redis->auth($password); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Change the selected database for the current connection. |
151
|
|
|
* |
152
|
|
|
* @param int $db the database number to switch to (0 - 15). |
153
|
|
|
* |
154
|
|
|
* @return bool true in case of success, false in case of failure. |
155
|
|
|
*/ |
156
|
|
|
public function select(int $db): bool |
157
|
|
|
{ |
158
|
|
|
return $this->redis->select($db); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Swap one Redis database with another atomically. |
163
|
|
|
* |
164
|
|
|
* @param int $db1 Database to Switch From (valid 0 - 15) |
165
|
|
|
* @param int $db2 Database to Switch To (valid 0 - 15) |
166
|
|
|
* |
167
|
|
|
* @return bool true in case of success, false in case of failure. |
168
|
|
|
*/ |
169
|
|
|
public function swapdb(int $db1, int $db2): bool |
170
|
|
|
{ |
171
|
|
|
return $this->redis->swapdb($db1, $db2); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Disconnects from the Redis instance. |
176
|
|
|
* |
177
|
|
|
* Note: Closing a persistent connection requires PhpRedis >= 4.2.0. |
178
|
|
|
* |
179
|
|
|
* @return bool TRUE on success, FALSE on failure. |
180
|
|
|
*/ |
181
|
|
|
public function close(): bool |
182
|
|
|
{ |
183
|
|
|
return $this->redis->close(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set client option. |
188
|
|
|
* |
189
|
|
|
* @param string $name |
190
|
|
|
* @param string $value |
191
|
|
|
* |
192
|
|
|
* @return bool true on success, false on error. |
193
|
|
|
*/ |
194
|
|
|
public function setOption(string $name, string $value): bool |
195
|
|
|
{ |
196
|
|
|
return $this->redis->setOption($name, $value); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get client option. |
201
|
|
|
* |
202
|
|
|
* @param string $name |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getOption(string $name): string |
207
|
|
|
{ |
208
|
|
|
return $this->redis->getOption($name); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Check the current connection status. |
213
|
|
|
* |
214
|
|
|
* Note: Prior to PhpRedis 5.0.0 this command simply returned the string +PONG. |
215
|
|
|
* |
216
|
|
|
* @param string|null $message |
217
|
|
|
* |
218
|
|
|
* @return Mixed: This method returns true on success, or the passed string if called with an argument. |
|
|
|
|
219
|
|
|
*/ |
220
|
|
|
public function ping(?string $message = null) |
221
|
|
|
{ |
222
|
|
|
return $message ? $this->redis->ping($message) : $this->redis->ping(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Sends a string to Redis, which replies with the same string. |
227
|
|
|
* |
228
|
|
|
* @param string $message The message to send. |
229
|
|
|
* |
230
|
|
|
* @return string the same message. |
231
|
|
|
*/ |
232
|
|
|
public function echo(string $message): string |
233
|
|
|
{ |
234
|
|
|
return $this->redis->echo($message); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: