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
|
1 |
|
return Cache::set($this->key, $value, $ttl); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Store the value of the key in the cache if the key does not exist. |
81
|
|
|
*/ |
82
|
1 |
|
public function add(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool |
83
|
|
|
{ |
84
|
1 |
|
return Cache::add($this->key, $value, $ttl); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Increment the value of the key in the cache. |
89
|
|
|
*/ |
90
|
1 |
|
public function increment(float|int $value = 1): float|int|bool |
91
|
|
|
{ |
92
|
|
|
/** @var float|int|bool */ |
93
|
1 |
|
return Cache::increment($this->key, $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Decrement the value of the key in the cache. |
98
|
|
|
*/ |
99
|
1 |
|
public function decrement(float|int $value = 1): float|int|bool |
100
|
|
|
{ |
101
|
|
|
/** @var float|int|bool */ |
102
|
1 |
|
return Cache::decrement($this->key, $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Store the value of the key in the cache indefinitely. |
107
|
|
|
*/ |
108
|
1 |
|
public function forever(mixed $value): bool |
109
|
|
|
{ |
110
|
1 |
|
return Cache::forever($this->key, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Retrieve or store the value of the key. |
115
|
|
|
*/ |
116
|
1 |
|
public function remember(Closure|DateTimeInterface|DateInterval|int|null $ttl, Closure $callback): mixed |
117
|
|
|
{ |
118
|
1 |
|
return Cache::remember($this->key, $ttl, $callback); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve or store indefinitely the value of the key. |
123
|
|
|
*/ |
124
|
1 |
|
public function rememberForever(Closure $callback): mixed |
125
|
|
|
{ |
126
|
1 |
|
return Cache::rememberForever($this->key, $callback); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieve or store indefinitely the value of the key. |
131
|
|
|
*/ |
132
|
1 |
|
public function sear(Closure $callback): mixed |
133
|
|
|
{ |
134
|
1 |
|
return Cache::sear($this->key, $callback); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove the key from the cache. |
139
|
|
|
*/ |
140
|
1 |
|
public function forget(): bool |
141
|
|
|
{ |
142
|
1 |
|
return Cache::forget($this->key); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Delete the key from the cache. |
147
|
|
|
*/ |
148
|
1 |
|
public function delete(): bool |
149
|
|
|
{ |
150
|
1 |
|
return Cache::delete($this->key); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Retrieve the lock instance. |
155
|
|
|
*/ |
156
|
1 |
|
public function lock(int $seconds = 0, ?string $owner = null): Lock |
157
|
|
|
{ |
158
|
1 |
|
return Cache::lock($this->key, $seconds, $owner); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Restore the lock instance using the owner identifier. |
163
|
|
|
*/ |
164
|
1 |
|
public function restoreLock(string $owner): Lock |
165
|
|
|
{ |
166
|
1 |
|
return Cache::restoreLock($this->key, $owner); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|