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 bool $value |
26
|
|
|
*/ |
27
|
|
|
function redisSet(string $key, $value = null, $expiration = null): bool |
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 bool |
38
|
|
|
*/ |
39
|
|
|
function redisExpire(string $key, $expiration = null): bool |
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 bool |
85
|
|
|
*/ |
86
|
|
|
function redisCacheView(string $key, string $view, array $data, int $expiration = null): bool |
87
|
|
|
{ |
88
|
|
|
return RedisCache::set($key, View::make($view, $data)->render(), $expiration); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Increment a Redis Key's value & return the new value. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @param int $value |
96
|
|
|
* @param int|null $expiration |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
|
|
function redisIncrement(string $key, int $value = 1, int $expiration = null): int |
100
|
|
|
{ |
101
|
|
|
return RedisCache::increment($key, $value, $expiration); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Flush the redis cache of all keys with environment's prefix. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
function redisClearCache(): array |
110
|
|
|
{ |
111
|
|
|
return RedisCache::clear(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Pass a $callback function to be stored in the Cache for an amount of time. |
116
|
|
|
* |
117
|
|
|
* @param string $key |
118
|
|
|
* @param Closure $callback |
119
|
|
|
* @param int|null $ttl |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
function redisRemember(string $key, Closure $callback, int $ttl = null) |
123
|
|
|
{ |
124
|
|
|
return RedisCache::remember($key, $callback, $ttl); |
125
|
|
|
} |
126
|
|
|
|