1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Redis; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Illuminate\Support\Facades\Redis; |
8
|
|
|
use Sfneal\Actions\AbstractService; |
9
|
|
|
|
10
|
|
|
class RedisCache extends AbstractService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Retrieve the Redis Key TTL from the config. |
14
|
|
|
* |
15
|
|
|
* @return int |
16
|
|
|
*/ |
17
|
|
|
private static function defaultTTL(): int |
18
|
|
|
{ |
19
|
|
|
return config('redis-helpers.ttl', 3600); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Retrieve a redis key with the application prefix prepended |
24
|
|
|
* |
25
|
|
|
* @param string $key |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
private static function keyWithPrefix(string $key): string |
29
|
|
|
{ |
30
|
|
|
return config('cache.prefix').":{$key}"; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Retrieve a key's time to live in seconds |
35
|
|
|
* |
36
|
|
|
* @param string $key |
37
|
|
|
* @return int |
38
|
|
|
*/ |
39
|
|
|
public static function ttl(string $key): int |
40
|
|
|
{ |
41
|
|
|
return Redis::connection()->command('TTL', [self::keyWithPrefix($key)]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieve an array of keys that begin with a prefix. |
46
|
|
|
* |
47
|
|
|
* @param string $prefix |
48
|
|
|
* @return mixed list of keys without prefix |
49
|
|
|
*/ |
50
|
|
|
public static function keys(string $prefix = '') |
51
|
|
|
{ |
52
|
|
|
return array_map( |
53
|
|
|
// Remove prefix from each key so it is not concatenated twice |
54
|
|
|
function ($key) { |
55
|
|
|
return substr($key, strlen(config('cache.prefix')) + 1); |
56
|
|
|
}, |
57
|
|
|
|
58
|
|
|
// List of Redis key's matching pattern |
59
|
|
|
Redis::connection() |
60
|
|
|
->client() |
61
|
|
|
->keys(self::keyWithPrefix($prefix.'*')) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get items from the cache. |
67
|
|
|
* |
68
|
|
|
* @param string $key |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public static function get(string $key) |
72
|
|
|
{ |
73
|
|
|
return Cache::get($key); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Put items in the cache with a TTL. |
78
|
|
|
* |
79
|
|
|
* Use's environment's REDIS_KEY_EXPIRATION value if $expiration is null. |
80
|
|
|
* |
81
|
|
|
* @param string $key |
82
|
|
|
* @param null $value |
|
|
|
|
83
|
|
|
* @param int|null $expiration |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public static function set(string $key, $value = null, int $expiration = null): bool |
87
|
|
|
{ |
88
|
|
|
// Store the $value in the Cache |
89
|
|
|
// todo: optimize expiration call |
90
|
|
|
return Cache::put( |
91
|
|
|
$key, |
92
|
|
|
$value, |
93
|
|
|
(isset($expiration) ? $expiration : self::defaultTTL()) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Put an array of key value pairs into the cache with a TTL. |
99
|
|
|
* |
100
|
|
|
* @param array $array |
101
|
|
|
* @param int|null $expiration |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public static function setMany(array $array, int $expiration = null): array |
105
|
|
|
{ |
106
|
|
|
foreach ($array as $key => $value) { |
107
|
|
|
self::set($key, $value, $expiration); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return array_values($array); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add a TTL attribute (time to live or time til expiration) to a Redis key. |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* @param null $expiration |
|
|
|
|
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public static function expire(string $key, $expiration = null): bool |
121
|
|
|
{ |
122
|
|
|
// Use environment REDIS_KEY_EXPIRATION value if not set |
123
|
|
|
if (! $expiration) { |
|
|
|
|
124
|
|
|
$expiration = self::defaultTTL(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Create a key value pair with a null value and a TTL if the key is missing |
128
|
|
|
if (self::missing($key)) { |
129
|
|
|
return self::set($key, null, $expiration); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Create a key value pair with original value and a TTL if the key exists |
133
|
|
|
else { |
134
|
|
|
return self::set($key, self::get($key), $expiration); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Delete Redis key's from the Cache. |
140
|
|
|
* |
141
|
|
|
* @param $keys array|string |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public static function delete($keys): array |
145
|
|
|
{ |
146
|
|
|
// Returns an array of deleted keys with success values |
147
|
|
|
return collect((array) $keys) |
148
|
|
|
->flatMap(function (string $key) { |
149
|
|
|
return self::keys($key); |
150
|
|
|
}) |
151
|
|
|
->mapWithKeys(function (string $key) { |
152
|
|
|
return [$key => Cache::forget($key)]; |
153
|
|
|
}) |
154
|
|
|
->toArray(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Determine if a redis key exists in the cache. |
159
|
|
|
* |
160
|
|
|
* @param string $key |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public static function exists(string $key): bool |
164
|
|
|
{ |
165
|
|
|
return Cache::has($key); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Determine if a redis key is missing from the cache. |
170
|
|
|
* |
171
|
|
|
* @param string $key |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public static function missing(string $key): bool |
175
|
|
|
{ |
176
|
|
|
return Cache::missing($key); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create a Redis Key with a null value if it is missing. |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
* @param null $value |
|
|
|
|
184
|
|
|
* @param null $expiration |
|
|
|
|
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public static function setIfMissing(string $key, $value = null, $expiration = null): bool |
188
|
|
|
{ |
189
|
|
|
// Create the redis Key with an expiration |
190
|
|
|
if (self::missing($key)) { |
191
|
|
|
self::set($key, $value, $expiration); |
192
|
|
|
|
193
|
|
|
return true; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Not created |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Increment a Redis Key's value & return the new value. |
202
|
|
|
* |
203
|
|
|
* @param string $key |
204
|
|
|
* @param int $value |
205
|
|
|
* @param int|null $expiration |
206
|
|
|
* @return mixed |
207
|
|
|
*/ |
208
|
|
|
public static function increment(string $key, int $value = 1, int $expiration = null) |
209
|
|
|
{ |
210
|
|
|
// Create the Key if it's missing |
211
|
|
|
self::setIfMissing($key, 0, $expiration); |
|
|
|
|
212
|
|
|
|
213
|
|
|
// Increment the value & return the new value |
214
|
|
|
return Cache::increment($key, $value); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Flush the entire redis cache. |
219
|
|
|
* |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
public static function flush() |
223
|
|
|
{ |
224
|
|
|
return Cache::flush(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Flush the redis cache of all keys with environment's prefix. |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public static function clear(): array |
233
|
|
|
{ |
234
|
|
|
return self::delete(''); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Pass a $callback function to be stored in the Cache for an amount of time. |
239
|
|
|
* |
240
|
|
|
* @param string $key |
241
|
|
|
* @param int $ttl |
242
|
|
|
* @param Closure $callback |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public static function remember(string $key, int $ttl, Closure $callback) |
246
|
|
|
{ |
247
|
|
|
return Cache::remember($key, $ttl, $callback); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Pass a $callback function to be stored in the Cache forever. |
252
|
|
|
* |
253
|
|
|
* @param string $key |
254
|
|
|
* @param Closure $callback |
255
|
|
|
* @return mixed |
256
|
|
|
*/ |
257
|
|
|
public static function rememberForever(string $key, Closure $callback) |
258
|
|
|
{ |
259
|
|
|
return Cache::rememberForever($key, $callback); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|