1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum\Services; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use DateInterval; |
9
|
|
|
use DateTimeInterface; |
10
|
|
|
use Illuminate\Support\Facades\Cache; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The key dealing with the Laravel cache. |
14
|
|
|
*/ |
15
|
|
|
final class CacheKey |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Instantiate the class. |
19
|
|
|
*/ |
20
|
13 |
|
public function __construct(private readonly string $key) {} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Determine whether the key exists in the cache. |
24
|
|
|
*/ |
25
|
1 |
|
public function exists(): bool |
26
|
|
|
{ |
27
|
1 |
|
return Cache::has($this->key); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Determine whether the key doesn't exist in the cache. |
32
|
|
|
*/ |
33
|
1 |
|
public function missing(): bool |
34
|
|
|
{ |
35
|
1 |
|
return Cache::missing($this->key); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Determine whether the key is present and not null. |
40
|
|
|
*/ |
41
|
1 |
|
public function hasValue(): bool |
42
|
|
|
{ |
43
|
1 |
|
return Cache::has($this->key); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieve the value of the key from the cache. |
48
|
|
|
*/ |
49
|
1 |
|
public function get(mixed $default = null): mixed |
50
|
|
|
{ |
51
|
1 |
|
return Cache::get($this->key, $default); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve the value of the key from the cache and delete it. |
56
|
|
|
*/ |
57
|
1 |
|
public function pull(mixed $default = null): mixed |
58
|
|
|
{ |
59
|
1 |
|
return Cache::pull($this->key, $default); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Store the value of the key in the cache. |
64
|
|
|
*/ |
65
|
1 |
|
public function put(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool |
66
|
|
|
{ |
67
|
1 |
|
return Cache::put($this->key, $value, $ttl); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Store the value of the key in the cache if the key does not exist. |
72
|
|
|
*/ |
73
|
1 |
|
public function add(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool |
74
|
|
|
{ |
75
|
1 |
|
return Cache::add($this->key, $value, $ttl); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Increment the value of the key in the cache. |
80
|
|
|
*/ |
81
|
1 |
|
public function increment(float|int $value = 1): float|int|bool |
82
|
|
|
{ |
83
|
1 |
|
return Cache::increment($this->key, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Decrement the value of the key in the cache. |
88
|
|
|
*/ |
89
|
1 |
|
public function decrement(float|int $value = 1): float|int|bool |
90
|
|
|
{ |
91
|
1 |
|
return Cache::decrement($this->key, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Store the value of the key in the cache indefinitely. |
96
|
|
|
*/ |
97
|
1 |
|
public function forever(mixed $value): bool |
98
|
|
|
{ |
99
|
1 |
|
return Cache::forever($this->key, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrieve or store the value of the key. |
104
|
|
|
*/ |
105
|
1 |
|
public function remember(Closure|DateTimeInterface|DateInterval|int|null $ttl, Closure $callback): mixed |
106
|
|
|
{ |
107
|
1 |
|
return Cache::remember($this->key, $ttl, $callback); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieve or store indefinitely the value of the key. |
112
|
|
|
*/ |
113
|
1 |
|
public function rememberForever(Closure $callback): mixed |
114
|
|
|
{ |
115
|
1 |
|
return Cache::rememberForever($this->key, $callback); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Remove the key from the cache. |
120
|
|
|
*/ |
121
|
1 |
|
public function forget(): bool |
122
|
|
|
{ |
123
|
1 |
|
return Cache::forget($this->key); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|