1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
use Webdcg\Redis\Exceptions\NotAssociativeArrayException; |
6
|
|
|
|
7
|
|
|
trait Strings |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Append specified string to the string stored in specified key. |
11
|
|
|
* See: https://redis.io/commands/append. |
12
|
|
|
* |
13
|
|
|
* @param string $key |
14
|
|
|
* @param string $value |
15
|
|
|
* |
16
|
|
|
* @return int Size of the value after the append. |
17
|
|
|
*/ |
18
|
|
|
public function append(string $key, string $value): int |
19
|
|
|
{ |
20
|
|
|
return $this->redis->append($key, $value); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Decrement the number stored at key by one. |
25
|
|
|
* See: https://redis.io/commands/decr. |
26
|
|
|
* |
27
|
|
|
* @param string $key |
28
|
|
|
* |
29
|
|
|
* @return int the new value |
30
|
|
|
*/ |
31
|
|
|
public function decr(string $key): int |
32
|
|
|
{ |
33
|
|
|
return $this->redis->decr($key); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Decrement the number stored at key by the given decrement. |
38
|
|
|
* See: https://redis.io/commands/decrby. |
39
|
|
|
* |
40
|
|
|
* @param string $key [description] |
41
|
|
|
* @param int $decrement [description] |
42
|
|
|
* |
43
|
|
|
* @return int The new value |
44
|
|
|
*/ |
45
|
|
|
public function decrBy(string $key, int $decrement): int |
46
|
|
|
{ |
47
|
|
|
return $this->redis->decrBy($key, $decrement); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the value related to the specified key. |
52
|
|
|
* See: https://redis.io/commands/get. |
53
|
|
|
* |
54
|
|
|
* @param string $key [description] |
55
|
|
|
* |
56
|
|
|
* @return mixed|string|bool If key didn't exist, FALSE is returned. |
57
|
|
|
* Otherwise, the value related to this key |
58
|
|
|
* is returned. |
59
|
|
|
*/ |
60
|
|
|
public function get(string $key) |
61
|
|
|
{ |
62
|
|
|
return $this->redis->get($key); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return a substring of a larger string. |
67
|
|
|
* See: https://redis.io/commands/getrange. |
68
|
|
|
* |
69
|
|
|
* @param string $key [description] |
70
|
|
|
* @param int $start [description] |
71
|
|
|
* @param int $end [description] |
72
|
|
|
* |
73
|
|
|
* @return string the substring |
74
|
|
|
*/ |
75
|
|
|
public function getRange(string $key, int $start, int $end): string |
76
|
|
|
{ |
77
|
|
|
return $this->redis->getRange($key, $start, $end); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Atomically sets key to value and returns the old value stored at key. |
82
|
|
|
* Returns an error when key exists but does not hold a string value. |
83
|
|
|
* See: https://redis.io/commands/getset. |
84
|
|
|
* |
85
|
|
|
* @param string $key [description] |
86
|
|
|
* @param string $value [description] |
87
|
|
|
* |
88
|
|
|
* @return string Bulk string reply: the old value stored at key, |
89
|
|
|
* or nil when key did not exist. |
90
|
|
|
*/ |
91
|
|
|
public function getSet(string $key, string $value): string |
92
|
|
|
{ |
93
|
|
|
return $this->redis->getSet($key, $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Increment the number stored at key by one. |
98
|
|
|
* See: https://redis.io/commands/incr. |
99
|
|
|
* |
100
|
|
|
* @param string $key [description] |
101
|
|
|
* |
102
|
|
|
* @return int The new value. |
103
|
|
|
*/ |
104
|
|
|
public function incr(string $key): int |
105
|
|
|
{ |
106
|
|
|
return $this->redis->incr($key); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Increment the number stored at key by the given increment. |
111
|
|
|
* See: https://redis.io/commands/incrby. |
112
|
|
|
* |
113
|
|
|
* @param string $key [description] |
114
|
|
|
* @param int $increment [description] |
115
|
|
|
* |
116
|
|
|
* @return int The new value |
117
|
|
|
*/ |
118
|
|
|
public function incrBy(string $key, int $increment): int |
119
|
|
|
{ |
120
|
|
|
return $this->redis->incrBy($key, $increment); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Increment the number stored at key by the given increment. |
125
|
|
|
* See: https://redis.io/commands/incrbyfloat. |
126
|
|
|
* |
127
|
|
|
* @param string $key [description] |
128
|
|
|
* @param float $increment [description] |
129
|
|
|
* |
130
|
|
|
* @return float The new value. |
131
|
|
|
*/ |
132
|
|
|
public function incrByFloat(string $key, float $increment): float |
133
|
|
|
{ |
134
|
|
|
return $this->redis->incrByFloat($key, $increment); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the values of all the specified keys. If one or more keys don't |
139
|
|
|
* exist, the array will contain FALSE at the position of the key. |
140
|
|
|
* See: https://redis.io/commands/mget. |
141
|
|
|
* |
142
|
|
|
* @param array $keys [description] |
143
|
|
|
* |
144
|
|
|
* @return array Array reply: list of values at the specified keys. |
145
|
|
|
*/ |
146
|
|
|
public function mGet(array $keys): array |
147
|
|
|
{ |
148
|
|
|
return $this->redis->mGet($keys); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the values of all the specified keys. If one or more keys don't |
153
|
|
|
* exist, the array will contain FALSE at the position of the key. |
154
|
|
|
* See: https://redis.io/commands/mget. |
155
|
|
|
* |
156
|
|
|
* @param array $keys [description] |
157
|
|
|
* |
158
|
|
|
* @return array Array reply: list of values at the specified keys. |
159
|
|
|
*/ |
160
|
|
|
public function getMultiple(array $keys): array |
161
|
|
|
{ |
162
|
|
|
return $this->redis->mGet($keys); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets multiple key-value pairs in one atomic command. |
167
|
|
|
* See: https://redis.io/commands/mset. |
168
|
|
|
* |
169
|
|
|
* @param array $pairs [description] |
170
|
|
|
* |
171
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
172
|
|
|
*/ |
173
|
|
|
public function mSet(array $pairs): bool |
174
|
|
|
{ |
175
|
|
|
if (!$this->is_associative($pairs)) { |
|
|
|
|
176
|
|
|
throw new NotAssociativeArrayException('The array provided is not associative.', 1); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->redis->mSet($pairs); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Sets multiple key-value pairs in one atomic command. MSETNX only returns |
184
|
|
|
* TRUE if all the keys were set (see SETNX). |
185
|
|
|
* See: https://redis.io/commands/msetnx. |
186
|
|
|
* |
187
|
|
|
* @param array $pairs [description] |
188
|
|
|
* |
189
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
190
|
|
|
*/ |
191
|
|
|
public function mSetNX(array $pairs): bool |
192
|
|
|
{ |
193
|
|
|
if (!$this->is_associative($pairs)) { |
|
|
|
|
194
|
|
|
throw new NotAssociativeArrayException('The array provided is not associative.', 1); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->redis->mSetNX($pairs); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the string value in argument as value of the key. If you're using |
202
|
|
|
* Redis >= 2.6.12, you can pass extended options as explained below. |
203
|
|
|
* See: https://redis.io/commands/set. |
204
|
|
|
* |
205
|
|
|
* @param string $key |
206
|
|
|
* @param mixed $value |
207
|
|
|
* @param mixed $args Timeout or Options Array (optional). If you pass |
208
|
|
|
* an integer, phpredis will redirect to SETEX, and |
209
|
|
|
* will try to use Redis >= 2.6.12 extended options |
210
|
|
|
* if you pass an array with valid values. |
211
|
|
|
* |
212
|
|
|
* @return bool TRUE if the command is successful. |
213
|
|
|
*/ |
214
|
|
|
public function set(string $key, $value, ...$args): bool |
215
|
|
|
{ |
216
|
|
|
if (empty($args)) { |
217
|
|
|
return $this->redis->set($key, $value); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this->redis->set($key, $value, $args[0]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set the string value in argument as value of the key, with a time to |
225
|
|
|
* live. PSETEX uses a TTL in milliseconds. |
226
|
|
|
* See: https://redis.io/commands/setex. |
227
|
|
|
* |
228
|
|
|
* @param string $key Key |
229
|
|
|
* @param int $ttl Time To Live (seconds) |
230
|
|
|
* @param string $value Value |
231
|
|
|
* |
232
|
|
|
* @return bool TRUE if the command is successful. |
233
|
|
|
*/ |
234
|
|
|
public function setEx(string $key, int $ttl, string $value): bool |
235
|
|
|
{ |
236
|
|
|
return $this->redis->setEx($key, $ttl, $value); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Set the string value in argument as value of the key, with a time to |
241
|
|
|
* live. PSETEX uses a TTL in milliseconds. |
242
|
|
|
* See: https://redis.io/commands/setex. |
243
|
|
|
* |
244
|
|
|
* @param string $key Key |
245
|
|
|
* @param int $ttl Time To Live (milliseconds) |
246
|
|
|
* @param string $value Value |
247
|
|
|
* |
248
|
|
|
* @return bool TRUE if the command is successful. |
249
|
|
|
*/ |
250
|
|
|
public function pSetEx(string $key, int $ttl, string $value): bool |
251
|
|
|
{ |
252
|
|
|
return $this->redis->pSetEx($key, $ttl, $value); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Set the string value in argument as value of the key if the key doesn't |
257
|
|
|
* already exist in the database. |
258
|
|
|
* See: https://redis.io/commands/setnx. |
259
|
|
|
* |
260
|
|
|
* @param string $key [description] |
261
|
|
|
* @param string $value [description] |
262
|
|
|
* |
263
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
264
|
|
|
*/ |
265
|
|
|
public function setNx(string $key, string $value): bool |
266
|
|
|
{ |
267
|
|
|
return $this->redis->setNx($key, $value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Changes a substring of a larger string. |
272
|
|
|
* See: https://redis.io/commands/setrange. |
273
|
|
|
* |
274
|
|
|
* @param string $key [description] |
275
|
|
|
* @param int $offset [description] |
276
|
|
|
* @param string $value [description] |
277
|
|
|
* |
278
|
|
|
* @return int the length of the string after it was modified. |
279
|
|
|
*/ |
280
|
|
|
public function setRange(string $key, int $offset, string $value): int |
281
|
|
|
{ |
282
|
|
|
return $this->redis->setRange($key, $offset, $value); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Returns the length of the string value stored at key. An error is |
287
|
|
|
* returned when key holds a non-string value. |
288
|
|
|
* See: https://redis.io/commands/strlen. |
289
|
|
|
* |
290
|
|
|
* @param string $key [description] |
291
|
|
|
* |
292
|
|
|
* @return int The length of the string at key, or 0 when key does |
293
|
|
|
* not exist. |
294
|
|
|
*/ |
295
|
|
|
public function strLen(string $key): int |
296
|
|
|
{ |
297
|
|
|
return $this->redis->strLen($key); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
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: