1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum\Capsules; |
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
|
|
|
/** @var float|int|bool */ |
84
|
1 |
|
return Cache::increment($this->key, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Decrement the value of the key in the cache. |
89
|
|
|
*/ |
90
|
1 |
|
public function decrement(float|int $value = 1): float|int|bool |
91
|
|
|
{ |
92
|
|
|
/** @var float|int|bool */ |
93
|
1 |
|
return Cache::decrement($this->key, $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Store the value of the key in the cache indefinitely. |
98
|
|
|
*/ |
99
|
1 |
|
public function forever(mixed $value): bool |
100
|
|
|
{ |
101
|
1 |
|
return Cache::forever($this->key, $value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieve or store the value of the key. |
106
|
|
|
*/ |
107
|
1 |
|
public function remember(Closure|DateTimeInterface|DateInterval|int|null $ttl, Closure $callback): mixed |
108
|
|
|
{ |
109
|
1 |
|
return Cache::remember($this->key, $ttl, $callback); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve or store indefinitely the value of the key. |
114
|
|
|
*/ |
115
|
1 |
|
public function rememberForever(Closure $callback): mixed |
116
|
|
|
{ |
117
|
1 |
|
return Cache::rememberForever($this->key, $callback); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Remove the key from the cache. |
122
|
|
|
*/ |
123
|
1 |
|
public function forget(): bool |
124
|
|
|
{ |
125
|
1 |
|
return Cache::forget($this->key); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|