1 | <?php |
||
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 |
||
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 |
||
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 |
||
41 | * @param int $decrement |
||
42 | * |
||
43 | * @return int The new value |
||
44 | */ |
||
45 | public function decrBy(string $key, int $decrement): int |
||
49 | |||
50 | /** |
||
51 | * Get the value related to the specified key. |
||
52 | * See: https://redis.io/commands/get. |
||
53 | * |
||
54 | * @param string $key |
||
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) |
||
64 | |||
65 | /** |
||
66 | * Return a substring of a larger string. |
||
67 | * See: https://redis.io/commands/getrange. |
||
68 | * |
||
69 | * @param string $key |
||
70 | * @param int $start |
||
71 | * @param int $end |
||
72 | * |
||
73 | * @return string the substring |
||
74 | */ |
||
75 | public function getRange(string $key, int $start, int $end): string |
||
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 |
||
86 | * @param string $value |
||
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 |
||
95 | |||
96 | /** |
||
97 | * Increment the number stored at key by one. |
||
98 | * See: https://redis.io/commands/incr. |
||
99 | * |
||
100 | * @param string $key |
||
101 | * |
||
102 | * @return int The new value. |
||
103 | */ |
||
104 | public function incr(string $key): int |
||
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 |
||
114 | * @param int $increment |
||
115 | * |
||
116 | * @return int The new value |
||
117 | */ |
||
118 | public function incrBy(string $key, int $increment): int |
||
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 |
||
128 | * @param float $increment |
||
129 | * |
||
130 | * @return float The new value. |
||
131 | */ |
||
132 | public function incrByFloat(string $key, float $increment): float |
||
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 |
||
143 | * |
||
144 | * @return array Array reply: list of values at the specified keys. |
||
145 | */ |
||
146 | public function mGet(array $keys): array |
||
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 |
||
157 | * |
||
158 | * @return array Array reply: list of values at the specified keys. |
||
159 | */ |
||
160 | public function getMultiple(array $keys): array |
||
164 | |||
165 | /** |
||
166 | * Sets multiple key-value pairs in one atomic command. |
||
167 | * See: https://redis.io/commands/mset. |
||
168 | * |
||
169 | * @param array $pairs |
||
170 | * |
||
171 | * @return bool TRUE in case of success, FALSE in case of failure. |
||
172 | */ |
||
173 | public function mSet(array $pairs): bool |
||
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 |
||
188 | * |
||
189 | * @return bool TRUE in case of success, FALSE in case of failure. |
||
190 | */ |
||
191 | public function mSetNX(array $pairs): bool |
||
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 |
||
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 |
||
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 |
||
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 |
||
261 | * @param string $value |
||
262 | * |
||
263 | * @return bool TRUE in case of success, FALSE in case of failure. |
||
264 | */ |
||
265 | public function setNx(string $key, string $value): bool |
||
269 | |||
270 | /** |
||
271 | * Changes a substring of a larger string. |
||
272 | * See: https://redis.io/commands/setrange. |
||
273 | * |
||
274 | * @param string $key |
||
275 | * @param int $offset |
||
276 | * @param string $value |
||
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 |
||
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 |
||
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 |
||
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: