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\Contracts\Cache\Lock; |
11
|
|
|
use Illuminate\Support\Facades\Cache; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The key dealing with the Laravel cache. |
15
|
|
|
*/ |
16
|
|
|
final class CacheKey |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Instantiate the class. |
20
|
|
|
*/ |
21
|
18 |
|
public function __construct(private readonly string $key) {} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Determine whether the key exists in the cache. |
25
|
|
|
*/ |
26
|
1 |
|
public function exists(): bool |
27
|
|
|
{ |
28
|
1 |
|
return Cache::has($this->key); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Determine whether the key doesn't exist in the cache. |
33
|
|
|
*/ |
34
|
1 |
|
public function missing(): bool |
35
|
|
|
{ |
36
|
1 |
|
return Cache::missing($this->key); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Determine whether the key is present and not null. |
41
|
|
|
*/ |
42
|
1 |
|
public function hasValue(): bool |
43
|
|
|
{ |
44
|
1 |
|
return Cache::has($this->key); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve the value of the key from the cache. |
49
|
|
|
*/ |
50
|
1 |
|
public function get(mixed $default = null): mixed |
51
|
|
|
{ |
52
|
1 |
|
return Cache::get($this->key, $default); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the value of the key from the cache and delete it. |
57
|
|
|
*/ |
58
|
1 |
|
public function pull(mixed $default = null): mixed |
59
|
|
|
{ |
60
|
1 |
|
return Cache::pull($this->key, $default); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Store the value of the key in the cache. |
65
|
|
|
*/ |
66
|
1 |
|
public function put(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool |
67
|
|
|
{ |
68
|
1 |
|
return Cache::put($this->key, $value, $ttl); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Store the value of the key in the cache. |
73
|
|
|
*/ |
74
|
1 |
|
public function set(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool |
75
|
|
|
{ |
76
|
|
|
/** @phpstan-ignore argument.type */ |
77
|
1 |
|
return Cache::set($this->key, $value, $ttl); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Store the value of the key in the cache if the key does not exist. |
82
|
|
|
*/ |
83
|
1 |
|
public function add(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool |
84
|
|
|
{ |
85
|
1 |
|
return Cache::add($this->key, $value, $ttl); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Increment the value of the key in the cache. |
90
|
|
|
*/ |
91
|
1 |
|
public function increment(float|int $value = 1): float|int|bool |
92
|
|
|
{ |
93
|
|
|
/** @var float|int|bool */ |
94
|
1 |
|
return Cache::increment($this->key, $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Decrement the value of the key in the cache. |
99
|
|
|
*/ |
100
|
1 |
|
public function decrement(float|int $value = 1): float|int|bool |
101
|
|
|
{ |
102
|
|
|
/** @var float|int|bool */ |
103
|
1 |
|
return Cache::decrement($this->key, $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Store the value of the key in the cache indefinitely. |
108
|
|
|
*/ |
109
|
1 |
|
public function forever(mixed $value): bool |
110
|
|
|
{ |
111
|
1 |
|
return Cache::forever($this->key, $value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retrieve or store the value of the key. |
116
|
|
|
*/ |
117
|
1 |
|
public function remember(Closure|DateTimeInterface|DateInterval|int|null $ttl, Closure $callback): mixed |
118
|
|
|
{ |
119
|
1 |
|
return Cache::remember($this->key, $ttl, $callback); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Retrieve or store indefinitely the value of the key. |
124
|
|
|
*/ |
125
|
1 |
|
public function rememberForever(Closure $callback): mixed |
126
|
|
|
{ |
127
|
1 |
|
return Cache::rememberForever($this->key, $callback); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Retrieve or store indefinitely the value of the key. |
132
|
|
|
*/ |
133
|
1 |
|
public function sear(Closure $callback): mixed |
134
|
|
|
{ |
135
|
1 |
|
return Cache::sear($this->key, $callback); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Remove the key from the cache. |
140
|
|
|
*/ |
141
|
1 |
|
public function forget(): bool |
142
|
|
|
{ |
143
|
1 |
|
return Cache::forget($this->key); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Delete the key from the cache. |
148
|
|
|
*/ |
149
|
1 |
|
public function delete(): bool |
150
|
|
|
{ |
151
|
1 |
|
return Cache::delete($this->key); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Retrieve the lock instance. |
156
|
|
|
*/ |
157
|
1 |
|
public function lock(int $seconds = 0, ?string $owner = null): Lock |
158
|
|
|
{ |
159
|
1 |
|
return Cache::lock($this->key, $seconds, $owner); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Restore the lock instance using the owner identifier. |
164
|
|
|
*/ |
165
|
1 |
|
public function restoreLock(string $owner): Lock |
166
|
|
|
{ |
167
|
1 |
|
return Cache::restoreLock($this->key, $owner); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|