1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\View; |
4
|
|
|
use Sfneal\Helpers\Redis\RedisCache; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Get items from the cache. |
8
|
|
|
* |
9
|
|
|
* @param string $key |
10
|
|
|
* @return mixed |
11
|
|
|
*/ |
12
|
|
|
function redisGet(string $key) |
13
|
|
|
{ |
14
|
|
|
return RedisCache::get($key); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Put items in the cache with a TTL. |
19
|
|
|
* |
20
|
|
|
* Use's environment's REDIS_KEY_EXPIRATION value if $expiration is null. |
21
|
|
|
* |
22
|
|
|
* @param string $key |
23
|
|
|
* @param mixed|null $value |
24
|
|
|
* @param int|null $expiration |
25
|
|
|
* @return mixed|null $value |
26
|
|
|
*/ |
27
|
|
|
function redisSet(string $key, $value = null, $expiration = null) |
28
|
|
|
{ |
29
|
|
|
return RedisCache::set($key, $value, $expiration); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add a TTL attribute (time to live or time til expiration) to a Redis key. |
34
|
|
|
* |
35
|
|
|
* @param string $key |
36
|
|
|
* @param null $expiration |
|
|
|
|
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
function redisExpire(string $key, $expiration = null) |
40
|
|
|
{ |
41
|
|
|
return RedisCache::expire($key, $expiration); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Delete Redis key's from the Cache. |
46
|
|
|
* |
47
|
|
|
* @param $key array|string |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
function redisDelete($key): array |
51
|
|
|
{ |
52
|
|
|
return RedisCache::delete($key); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Determine if a redis key exists in the cache. |
57
|
|
|
* |
58
|
|
|
* @param string $key |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
function redisExists(string $key): bool |
62
|
|
|
{ |
63
|
|
|
return RedisCache::exists($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Determine if a redis key is missing from the cache. |
68
|
|
|
* |
69
|
|
|
* @param string $key |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
function redisMissing(string $key): bool |
73
|
|
|
{ |
74
|
|
|
return RedisCache::missing($key); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Render a view & cache its output for reuse. |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @param string $view |
82
|
|
|
* @param array $data |
83
|
|
|
* @param int|null $expiration |
84
|
|
|
* @return mixed|null |
85
|
|
|
*/ |
86
|
|
|
function redisCacheView(string $key, string $view, array $data, int $expiration = null) |
87
|
|
|
{ |
88
|
|
|
return RedisCache::set($key, View::make($view, $data)->render(), $expiration); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create a Redis Key with a null value if it is missing. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @param null $value |
|
|
|
|
96
|
|
|
* @param int|null $expiration |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
function redisCreateIfMissing(string $key, $value = null, int $expiration = null): bool |
100
|
|
|
{ |
101
|
|
|
return RedisCache::setIfMissing($key, $value, $expiration); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Increment a Redis Key's value & return the new value. |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* @param int $value |
109
|
|
|
* @param int|null $expiration |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
function redisIncrement(string $key, int $value = 1, int $expiration = null) |
113
|
|
|
{ |
114
|
|
|
return RedisCache::increment($key, $value, $expiration); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Flush the entire redis cache. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
function redisFlush(): array |
123
|
|
|
{ |
124
|
|
|
return RedisCache::flush(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Flush the redis cache of all keys with environment's prefix. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
function redisClearCache(): array |
133
|
|
|
{ |
134
|
|
|
return RedisCache::clear(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Pass a $callback function to be stored in the Cache for an amount of time. |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @param int $ttl |
142
|
|
|
* @param Closure $callback |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
function redisRemember(string $key, int $ttl, Closure $callback) |
146
|
|
|
{ |
147
|
|
|
return RedisCache::remember($key, $ttl, $callback); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Pass a $callback function to be stored in the Cache forever. |
152
|
|
|
* |
153
|
|
|
* @param string $key |
154
|
|
|
* @param Closure $callback |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
function redisRememberForever(string $key, Closure $callback) |
158
|
|
|
{ |
159
|
|
|
return RedisCache::rememberForever($key, $callback); |
160
|
|
|
} |
161
|
|
|
|