1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Cache; |
4
|
|
|
use Illuminate\Support\Facades\Redis; |
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\View; |
6
|
|
|
|
7
|
|
|
// todo: create CacheService & CacheService package? |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Retrieve a formatted RedisKey with the environment prefix included. |
11
|
|
|
* |
12
|
|
|
* @param string $redis_key |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
function redisKey(string $redis_key) |
16
|
|
|
{ |
17
|
|
|
return env('REDIS_KEY_PREFIX', 'projects').":$redis_key"; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Retrieve an array of keys that begin with a prefix. |
22
|
|
|
* |
23
|
|
|
* @param string $redis_key_prefix |
24
|
|
|
* @return mixed list of keys without prefix |
25
|
|
|
*/ |
26
|
|
|
function redisKeys(string $redis_key_prefix) |
27
|
|
|
{ |
28
|
|
|
return array_map( |
29
|
|
|
// Remove prefix from each key so it is not concatenated twice |
30
|
|
|
function ($key) { |
31
|
|
|
return substr($key, strlen(env('REDIS_KEY_PREFIX')) + 1); |
32
|
|
|
}, |
33
|
|
|
|
34
|
|
|
// List of Redis key's matching pattern |
35
|
|
|
Redis::connection('default')->client()->keys(redisKey($redis_key_prefix.'*')) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get items from the cache. |
41
|
|
|
* |
42
|
|
|
* @param string $redis_key |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
function redisGet(string $redis_key) |
46
|
|
|
{ |
47
|
|
|
return Cache::get($redis_key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Put items in the cache with a TTL. |
52
|
|
|
* |
53
|
|
|
* Use's environment's REDIS_KEY_EXPIRATION value if $expiration is null. |
54
|
|
|
* |
55
|
|
|
* @param string $redis_key |
56
|
|
|
* @param mixed|null $value |
57
|
|
|
* @param int|null $expiration |
58
|
|
|
* @return mixed|null $value |
59
|
|
|
*/ |
60
|
|
|
function redisSet(string $redis_key, $value = null, $expiration = null) |
61
|
|
|
{ |
62
|
|
|
Cache::put($redis_key, $value, (isset($expiration) ? $expiration : env('REDIS_KEY_EXPIRATION'))); |
63
|
|
|
|
64
|
|
|
return $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add a TTL attribute (time to live or time til expiration) to a Redis key. |
69
|
|
|
* |
70
|
|
|
* @param string $redis_key |
71
|
|
|
* @param null $expiration |
|
|
|
|
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
function redisExpire(string $redis_key, $expiration = null) |
75
|
|
|
{ |
76
|
|
|
// Use environment REDIS_KEY_EXPIRATION value if not set |
77
|
|
|
if (! $expiration) { |
|
|
|
|
78
|
|
|
$expiration = env('REDIS_KEY_EXPIRATION'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Create a key value pair with a null value and a TTL if the key is missing |
82
|
|
|
if (redisMissing($redis_key)) { |
83
|
|
|
return redisSet($redis_key, null, $expiration); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Create a key value pair with original value and a TTL if the key exists |
87
|
|
|
else { |
88
|
|
|
return redisSet($redis_key, redisGet($redis_key), $expiration); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Delete Redis key's from the Cache. |
94
|
|
|
* |
95
|
|
|
* @param $redis_key array|string |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
function redisDelete($redis_key) |
99
|
|
|
{ |
100
|
|
|
// Empty array of keys to delete |
101
|
|
|
$keys = []; |
102
|
|
|
|
103
|
|
|
// Check if an array of keys has been passed |
104
|
|
|
if (gettype($redis_key) == 'array') { |
105
|
|
|
// Recursively merge arrays of keys found matching pattern |
106
|
|
|
foreach (array_values($redis_key) as $value) { |
107
|
|
|
$keys = array_merge($keys, redisKeys($value)); |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
|
|
// All keys matching pattern |
111
|
|
|
$keys = array_merge($keys, redisKeys($redis_key)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Remove all keys that match param patterns |
115
|
|
|
$to_remove = array_values($keys); |
116
|
|
|
foreach ($to_remove as $value) { |
117
|
|
|
Cache::forget($value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return array_values($to_remove); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Determine if a redis key exists in the cache. |
125
|
|
|
* |
126
|
|
|
* @param string $redis_key |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
function redisExists(string $redis_key) |
130
|
|
|
{ |
131
|
|
|
return Cache::has($redis_key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Determine if a redis key is missing from the cache. |
136
|
|
|
* |
137
|
|
|
* @param string $redis_key |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
function redisMissing(string $redis_key) |
141
|
|
|
{ |
142
|
|
|
return Cache::missing($redis_key); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Render a view & cache its output for reuse. |
147
|
|
|
* |
148
|
|
|
* @param string $redis_key |
149
|
|
|
* @param string $view |
150
|
|
|
* @param array $data |
151
|
|
|
* @param int|null $expiration |
152
|
|
|
* @return mixed|null |
153
|
|
|
*/ |
154
|
|
|
function redisCacheView(string $redis_key, string $view, array $data, int $expiration = null) |
155
|
|
|
{ |
156
|
|
|
return redisSet($redis_key, View::make($view, $data)->render(), $expiration); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Create a Redis Key with a null value if it is missing. |
161
|
|
|
* |
162
|
|
|
* @param string $redis_key |
163
|
|
|
* @param null $value |
|
|
|
|
164
|
|
|
* @param int|null $expiration |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
function redisCreateIfMissing(string $redis_key, $value = null, int $expiration = null): bool |
168
|
|
|
{ |
169
|
|
|
// Create the redis Key with an expiration |
170
|
|
|
if (redisMissing($redis_key)) { |
171
|
|
|
redisSet($redis_key, $value, $expiration); |
172
|
|
|
|
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Not created |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Increment a Redis Key's value & return the new value. |
182
|
|
|
* |
183
|
|
|
* @param string $redis_key |
184
|
|
|
* @param int $value |
185
|
|
|
* @param int|null $expiration |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
function redisIncrement(string $redis_key, int $value = 1, int $expiration = null) |
189
|
|
|
{ |
190
|
|
|
// Create the Key if it's missing |
191
|
|
|
redisCreateIfMissing($redis_key, 0, $expiration); |
192
|
|
|
|
193
|
|
|
// Increment the value |
194
|
|
|
Cache::increment($redis_key, $value); |
195
|
|
|
|
196
|
|
|
// Return the new value |
197
|
|
|
return Cache::get($redis_key); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Flush the entire redis cache. |
202
|
|
|
* |
203
|
|
|
* @return mixed |
204
|
|
|
*/ |
205
|
|
|
function redisFlush() |
206
|
|
|
{ |
207
|
|
|
return Redis::connection('default')->client()->flushAll(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Flush the redis cache of all keys with environment's prefix. |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
function redisClearCache() |
216
|
|
|
{ |
217
|
|
|
return redisDelete(''); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Pass a $callback function to be stored in the Cache for an amount of time |
223
|
|
|
* |
224
|
|
|
* @param string $key |
225
|
|
|
* @param int $ttl |
226
|
|
|
* @param Closure $callback |
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
|
|
function redisRemember(string $key, int $ttl, Closure $callback) |
230
|
|
|
{ |
231
|
|
|
return Cache::remember($key, $ttl, $callback); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Pass a $callback function to be stored in the Cache forever |
237
|
|
|
* |
238
|
|
|
* @param string $key |
239
|
|
|
* @param Closure $callback |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
|
|
function redisRememberForever(string $key, Closure $callback) |
243
|
|
|
{ |
244
|
|
|
return Cache::rememberForever($key, $callback); |
245
|
|
|
} |
246
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: