1 | <?php |
||
5 | trait Hashes |
||
6 | { |
||
7 | /** |
||
8 | * Removes a value from the hash stored at key. If the hash table doesn't |
||
9 | * exist, or the key doesn't exist, FALSE is returned. |
||
10 | * See: https://redis.io/commands/hdel. |
||
11 | * |
||
12 | * @param string $key |
||
13 | * @param splat $fields |
||
14 | * |
||
15 | * @return int LONG the number of deleted keys, 0 if the key doesn't |
||
16 | * exist, FALSE if the key isn't a hash. |
||
17 | */ |
||
18 | public function hDel(string $key, ...$fields): int |
||
22 | |||
23 | /** |
||
24 | * Verify if the specified member exists in a key. |
||
25 | * See: https://redis.io/commands/hexists. |
||
26 | * |
||
27 | * @param string $key |
||
28 | * @param string $field |
||
29 | * |
||
30 | * @return bool If the member exists in the hash table, return |
||
31 | * TRUE, otherwise return FALSE. |
||
32 | */ |
||
33 | public function hExists(string $key, string $field): bool |
||
37 | |||
38 | /** |
||
39 | * Gets a value from the hash stored at key. If the hash table doesn't |
||
40 | * exist, or the key doesn't exist, FALSE is returned. |
||
41 | * See: https://redis.io/commands/hget. |
||
42 | * |
||
43 | * @param string $key |
||
44 | * @param string $field |
||
45 | * |
||
46 | * @return mixed STRING The value, if the command executed successfully |
||
47 | * BOOL FALSE in case of failure |
||
48 | */ |
||
49 | public function hGet(string $key, string $field) |
||
53 | |||
54 | /** |
||
55 | * Returns the whole hash, as an array of strings indexed by strings. |
||
56 | * The order is random and corresponds to redis' own internal |
||
57 | * representation of the set structure. |
||
58 | * See: https://redis.io/commands/hgetall. |
||
59 | * |
||
60 | * @param string $key |
||
61 | * |
||
62 | * @return array An array of elements, the contents of the hash. |
||
63 | */ |
||
64 | public function hGetAll(string $key): array |
||
68 | |||
69 | /** |
||
70 | * Increments the value of a member from a hash by a given amount. |
||
71 | * See: https://redis.io/commands/hincrby. |
||
72 | * |
||
73 | * @param string $key |
||
74 | * @param string $field (integer) value that will be added to |
||
75 | * the member's value |
||
76 | * @param int|integer $increment |
||
77 | * |
||
78 | * @return int LONG the new value |
||
79 | */ |
||
80 | public function hIncrBy(string $key, string $field, int $increment = 1): int |
||
84 | |||
85 | /** |
||
86 | * Increments the value of a hash member by the provided float value. |
||
87 | * See: https://redis.io/commands/hincrbyfloat. |
||
88 | * |
||
89 | * @param string $key |
||
90 | * @param string $field |
||
91 | * @param float $increment (float) value that will be added to the |
||
92 | * member's value |
||
93 | * |
||
94 | * @return float the new value |
||
95 | */ |
||
96 | public function hIncrByFloat(string $key, string $field, float $increment = 1.0): float |
||
100 | |||
101 | /** |
||
102 | * Returns the keys in a hash, as an array of strings. |
||
103 | * The order is random and corresponds to redis' own internal |
||
104 | * representation of the set structure. |
||
105 | * See: https://redis.io/commands/hkeys. |
||
106 | * |
||
107 | * @param string $key |
||
108 | * |
||
109 | * @return array An array of elements, the keys of the hash. |
||
110 | * This works like PHP's array_keys(). |
||
111 | */ |
||
112 | public function hKeys(string $key): array |
||
116 | |||
117 | /** |
||
118 | * Returns the length of a hash, in number of items. |
||
119 | * See: https://redis.io/commands/hlen. |
||
120 | * |
||
121 | * @param string $key |
||
122 | * |
||
123 | * @return int LONG the number of items in a hash, FALSE if the |
||
124 | * key doesn't exist or isn't a hash. |
||
125 | */ |
||
126 | public function hLen(string $key): int |
||
130 | |||
131 | /** |
||
132 | * Retrieve the values associated to the specified fields in the hash. |
||
133 | * See: https://redis.io/commands/hmget. |
||
134 | * |
||
135 | * @param string $key |
||
136 | * @param array $fields |
||
137 | * |
||
138 | * @return array An array of elements, the values of the |
||
139 | * specified fields in the hash, with the hash |
||
140 | * keys as array keys. |
||
141 | */ |
||
142 | public function hMGet(string $key, array $fields): array |
||
146 | |||
147 | /** |
||
148 | * Fills in a whole hash. Non-string values are converted to string, using |
||
149 | * the standard (string) cast. NULL values are stored as empty strings. |
||
150 | * See: https://redis.io/commands/hmset. |
||
151 | * |
||
152 | * @param string $key |
||
153 | * @param array $hash key → value array |
||
154 | * |
||
155 | * @return bool TRUE if the field was set, FALSE if it was |
||
156 | * already present. |
||
157 | */ |
||
158 | public function hMSet(string $key, array $hash): bool |
||
162 | |||
163 | /** |
||
164 | * Adds a value to the hash stored at key. |
||
165 | * See: https://redis.io/commands/hset. |
||
166 | * |
||
167 | * @param string $key |
||
168 | * @param string $field |
||
169 | * @param mixed $value |
||
170 | * |
||
171 | * @return int LONG 1 if value didn't exist and was added |
||
172 | * successfully, 0 if the value was already present |
||
173 | * and was replaced, FALSE if there was an error. |
||
174 | */ |
||
175 | public function hSet(string $key, string $field, $value): int |
||
179 | |||
180 | /** |
||
181 | * Adds a value to the hash stored at key only if this field isn't already |
||
182 | * in the hash. |
||
183 | * See: https://redis.io/commands/hsetnx. |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param string $field |
||
187 | * @param string $value |
||
188 | * |
||
189 | * @return bool TRUE if the field was set, FALSE if it was |
||
190 | * already present. |
||
191 | */ |
||
192 | public function hSetNx(string $key, string $field, string $value): bool |
||
196 | |||
197 | /** |
||
198 | * Returns the values in a hash, as an array of strings. |
||
199 | * The order is random and corresponds to redis' own internal |
||
200 | * representation of the set structure. |
||
201 | * See: https://redis.io/commands/hvals. |
||
202 | * |
||
203 | * @param string $key |
||
204 | * |
||
205 | * @return array An array of elements, the values of the hash. |
||
206 | * This works like PHP's array_values(). |
||
207 | */ |
||
208 | public function hVals(string $key): array |
||
212 | |||
213 | /** |
||
214 | * Scan a HASH value for members, with an optional pattern and count. |
||
215 | * |
||
216 | * @param string $key |
||
217 | * @param [type] $iterator Long (reference) |
||
218 | * @param string $pattern Optional pattern to match against |
||
219 | * @param int|integer $count How many keys to return in a go (only a |
||
220 | * suggestion to Redis) |
||
221 | * |
||
222 | * @return array An array of members that match our pattern |
||
223 | */ |
||
224 | public function hScan(string $key, &$iterator, string $pattern = '*', int $count = 10) |
||
228 | |||
229 | /** |
||
230 | * Get the string length of the value associated with field in the hash |
||
231 | * stored at key. |
||
232 | * See: https://redis.io/commands/hstrlen. |
||
233 | * |
||
234 | * @param string $key |
||
235 | * @param string $field |
||
236 | * |
||
237 | * @return int LONG the string length of the value associated |
||
238 | * with field, or zero when field is not present |
||
239 | * in the hash or key does not exist at all. |
||
240 | */ |
||
241 | public function hStrLen(string $key, string $field): int |
||
245 | } |
||
246 |
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: