Skip to content

Commit e7b5ee6

Browse files
committed
Add BackedEnum and UnitEnum support to Cache Repository
Add enum support to Cache Repository key methods (get, put, add, increment, decrement, forever, forget, remember, etc.) and their child classes (TaggedCache, RedisTaggedCache). Also adds enum support for cache tags, allowing enums to be used as tag names - a feature Laravel doesn't have yet.
1 parent b583560 commit e7b5ee6

File tree

4 files changed

+469
-31
lines changed

4 files changed

+469
-31
lines changed

src/cache/src/RedisTaggedCache.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
namespace Hypervel\Cache;
66

7+
use BackedEnum;
78
use DateInterval;
89
use DateTimeInterface;
910
use Hypervel\Cache\Contracts\Store;
11+
use UnitEnum;
12+
13+
use function Hypervel\Support\enum_value;
1014

1115
class RedisTaggedCache extends TaggedCache
1216
{
@@ -27,8 +31,10 @@ class RedisTaggedCache extends TaggedCache
2731
/**
2832
* Store an item in the cache if the key does not exist.
2933
*/
30-
public function add(string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
34+
public function add(BackedEnum|UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
3135
{
36+
$key = enum_value($key);
37+
3238
$this->tags->addEntry(
3339
$this->itemKey($key),
3440
! is_null($ttl) ? $this->getSeconds($ttl) : 0
@@ -40,12 +46,14 @@ public function add(string $key, mixed $value, DateInterval|DateTimeInterface|in
4046
/**
4147
* Store an item in the cache.
4248
*/
43-
public function put(array|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
49+
public function put(array|BackedEnum|UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
4450
{
4551
if (is_array($key)) {
4652
return $this->putMany($key, $value);
4753
}
4854

55+
$key = enum_value($key);
56+
4957
if (is_null($ttl)) {
5058
return $this->forever($key, $value);
5159
}
@@ -61,8 +69,10 @@ public function put(array|string $key, mixed $value, DateInterval|DateTimeInterf
6169
/**
6270
* Increment the value of an item in the cache.
6371
*/
64-
public function increment(string $key, int $value = 1): bool|int
72+
public function increment(BackedEnum|UnitEnum|string $key, int $value = 1): bool|int
6573
{
74+
$key = enum_value($key);
75+
6676
$this->tags->addEntry($this->itemKey($key), updateWhen: 'NX');
6777

6878
return parent::increment($key, $value);
@@ -71,8 +81,10 @@ public function increment(string $key, int $value = 1): bool|int
7181
/**
7282
* Decrement the value of an item in the cache.
7383
*/
74-
public function decrement(string $key, int $value = 1): bool|int
84+
public function decrement(BackedEnum|UnitEnum|string $key, int $value = 1): bool|int
7585
{
86+
$key = enum_value($key);
87+
7688
$this->tags->addEntry($this->itemKey($key), updateWhen: 'NX');
7789

7890
return parent::decrement($key, $value);
@@ -81,8 +93,10 @@ public function decrement(string $key, int $value = 1): bool|int
8193
/**
8294
* Store an item in the cache indefinitely.
8395
*/
84-
public function forever(string $key, mixed $value): bool
96+
public function forever(BackedEnum|UnitEnum|string $key, mixed $value): bool
8597
{
98+
$key = enum_value($key);
99+
86100
$this->tags->addEntry($this->itemKey($key));
87101

88102
return parent::forever($key, $value);

src/cache/src/Repository.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hypervel\Cache;
66

77
use ArrayAccess;
8+
use BackedEnum;
89
use BadMethodCallException;
910
use Carbon\Carbon;
1011
use Closure;
@@ -29,6 +30,9 @@
2930
use Hypervel\Cache\Events\WritingKey;
3031
use Hypervel\Cache\Events\WritingManyKeys;
3132
use Psr\EventDispatcher\EventDispatcherInterface;
33+
use UnitEnum;
34+
35+
use function Hypervel\Support\enum_value;
3236

3337
/**
3438
* @mixin \Hypervel\Cache\Contracts\Store
@@ -92,15 +96,15 @@ public function __clone()
9296
/**
9397
* Determine if an item exists in the cache.
9498
*/
95-
public function has(array|string $key): bool
99+
public function has(array|BackedEnum|UnitEnum|string $key): bool
96100
{
97101
return ! is_null($this->get($key));
98102
}
99103

100104
/**
101105
* Determine if an item doesn't exist in the cache.
102106
*/
103-
public function missing(string $key): bool
107+
public function missing(BackedEnum|UnitEnum|string $key): bool
104108
{
105109
return ! $this->has($key);
106110
}
@@ -114,12 +118,14 @@ public function missing(string $key): bool
114118
*
115119
* @return (TCacheValue is null ? mixed : TCacheValue)
116120
*/
117-
public function get(array|string $key, mixed $default = null): mixed
121+
public function get(array|BackedEnum|UnitEnum|string $key, mixed $default = null): mixed
118122
{
119123
if (is_array($key)) {
120124
return $this->many($key);
121125
}
122126

127+
$key = enum_value($key);
128+
123129
$this->event(new RetrievingKey($this->getName(), $key));
124130

125131
$value = $this->store->get($this->itemKey($key));
@@ -177,7 +183,7 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
177183
*
178184
* @return (TCacheValue is null ? mixed : TCacheValue)
179185
*/
180-
public function pull(string $key, mixed $default = null): mixed
186+
public function pull(BackedEnum|UnitEnum|string $key, mixed $default = null): mixed
181187
{
182188
return tap($this->get($key, $default), function () use ($key) {
183189
$this->forget($key);
@@ -187,12 +193,14 @@ public function pull(string $key, mixed $default = null): mixed
187193
/**
188194
* Store an item in the cache.
189195
*/
190-
public function put(array|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
196+
public function put(array|BackedEnum|UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
191197
{
192198
if (is_array($key)) {
193199
return $this->putMany($key, $value);
194200
}
195201

202+
$key = enum_value($key);
203+
196204
if ($ttl === null) {
197205
return $this->forever($key, $value);
198206
}
@@ -218,7 +226,7 @@ public function put(array|string $key, mixed $value, DateInterval|DateTimeInterf
218226
/**
219227
* Store an item in the cache.
220228
*/
221-
public function set(string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
229+
public function set(BackedEnum|UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
222230
{
223231
return $this->put($key, $value, $ttl);
224232
}
@@ -261,8 +269,10 @@ public function setMultiple(iterable $values, DateInterval|DateTimeInterface|int
261269
/**
262270
* Store an item in the cache if the key does not exist.
263271
*/
264-
public function add(string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
272+
public function add(BackedEnum|UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
265273
{
274+
$key = enum_value($key);
275+
266276
$seconds = null;
267277

268278
if ($ttl !== null) {
@@ -297,24 +307,26 @@ public function add(string $key, mixed $value, DateInterval|DateTimeInterface|in
297307
/**
298308
* Increment the value of an item in the cache.
299309
*/
300-
public function increment(string $key, int $value = 1): bool|int
310+
public function increment(BackedEnum|UnitEnum|string $key, int $value = 1): bool|int
301311
{
302-
return $this->store->increment($key, $value);
312+
return $this->store->increment(enum_value($key), $value);
303313
}
304314

305315
/**
306316
* Decrement the value of an item in the cache.
307317
*/
308-
public function decrement(string $key, int $value = 1): bool|int
318+
public function decrement(BackedEnum|UnitEnum|string $key, int $value = 1): bool|int
309319
{
310-
return $this->store->decrement($key, $value);
320+
return $this->store->decrement(enum_value($key), $value);
311321
}
312322

313323
/**
314324
* Store an item in the cache indefinitely.
315325
*/
316-
public function forever(string $key, mixed $value): bool
326+
public function forever(BackedEnum|UnitEnum|string $key, mixed $value): bool
317327
{
328+
$key = enum_value($key);
329+
318330
$this->event(new WritingKey($this->getName(), $key, $value));
319331

320332
$result = $this->store->forever($this->itemKey($key), $value);
@@ -337,7 +349,7 @@ public function forever(string $key, mixed $value): bool
337349
*
338350
* @return TCacheValue
339351
*/
340-
public function remember(string $key, DateInterval|DateTimeInterface|int|null $ttl, Closure $callback): mixed
352+
public function remember(BackedEnum|UnitEnum|string $key, DateInterval|DateTimeInterface|int|null $ttl, Closure $callback): mixed
341353
{
342354
$value = $this->get($key);
343355

@@ -362,7 +374,7 @@ public function remember(string $key, DateInterval|DateTimeInterface|int|null $t
362374
*
363375
* @return TCacheValue
364376
*/
365-
public function sear(string $key, Closure $callback): mixed
377+
public function sear(BackedEnum|UnitEnum|string $key, Closure $callback): mixed
366378
{
367379
return $this->rememberForever($key, $callback);
368380
}
@@ -376,7 +388,7 @@ public function sear(string $key, Closure $callback): mixed
376388
*
377389
* @return TCacheValue
378390
*/
379-
public function rememberForever(string $key, Closure $callback): mixed
391+
public function rememberForever(BackedEnum|UnitEnum|string $key, Closure $callback): mixed
380392
{
381393
$value = $this->get($key);
382394

@@ -395,8 +407,10 @@ public function rememberForever(string $key, Closure $callback): mixed
395407
/**
396408
* Remove an item from the cache.
397409
*/
398-
public function forget(string $key): bool
410+
public function forget(BackedEnum|UnitEnum|string $key): bool
399411
{
412+
$key = enum_value($key);
413+
400414
$this->event(new ForgettingKey($this->getName(), $key));
401415

402416
return tap($this->store->forget($this->itemKey($key)), function ($result) use ($key) {
@@ -408,7 +422,7 @@ public function forget(string $key): bool
408422
});
409423
}
410424

411-
public function delete(string $key): bool
425+
public function delete(BackedEnum|UnitEnum|string $key): bool
412426
{
413427
return $this->forget($key);
414428
}
@@ -452,8 +466,11 @@ public function tags(mixed $names): TaggedCache
452466
throw new BadMethodCallException('This cache store does not support tagging.');
453467
}
454468

469+
$names = is_array($names) ? $names : func_get_args();
470+
$names = array_map(fn ($name) => enum_value($name), $names);
471+
455472
/* @phpstan-ignore-next-line */
456-
$cache = $this->store->tags(is_array($names) ? $names : func_get_args());
473+
$cache = $this->store->tags($names);
457474

458475
if (! is_null($this->events)) {
459476
$cache->setEventDispatcher($this->events);
@@ -523,7 +540,7 @@ public function getName(): ?string
523540
/**
524541
* Determine if a cached value exists.
525542
*
526-
* @param string $key
543+
* @param BackedEnum|string|UnitEnum $key
527544
*/
528545
public function offsetExists($key): bool
529546
{
@@ -533,7 +550,7 @@ public function offsetExists($key): bool
533550
/**
534551
* Retrieve an item from the cache by key.
535552
*
536-
* @param string $key
553+
* @param BackedEnum|string|UnitEnum $key
537554
*/
538555
public function offsetGet($key): mixed
539556
{
@@ -543,7 +560,7 @@ public function offsetGet($key): mixed
543560
/**
544561
* Store an item in the cache for the default time.
545562
*
546-
* @param string $key
563+
* @param BackedEnum|string|UnitEnum $key
547564
* @param mixed $value
548565
*/
549566
public function offsetSet($key, $value): void
@@ -554,7 +571,7 @@ public function offsetSet($key, $value): void
554571
/**
555572
* Remove an item from the cache.
556573
*
557-
* @param string $key
574+
* @param BackedEnum|string|UnitEnum $key
558575
*/
559576
public function offsetUnset($key): void
560577
{

src/cache/src/TaggedCache.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace Hypervel\Cache;
66

7+
use BackedEnum;
78
use DateInterval;
89
use DateTimeInterface;
910
use Hypervel\Cache\Contracts\Store;
1011
use Hypervel\Cache\Events\CacheFlushed;
1112
use Hypervel\Cache\Events\CacheFlushing;
13+
use UnitEnum;
14+
15+
use function Hypervel\Support\enum_value;
1216

1317
class TaggedCache extends Repository
1418
{
@@ -46,17 +50,17 @@ public function putMany(array $values, DateInterval|DateTimeInterface|int|null $
4650
/**
4751
* Increment the value of an item in the cache.
4852
*/
49-
public function increment(string $key, int $value = 1): bool|int
53+
public function increment(BackedEnum|UnitEnum|string $key, int $value = 1): bool|int
5054
{
51-
return $this->store->increment($this->itemKey($key), $value);
55+
return $this->store->increment($this->itemKey(enum_value($key)), $value);
5256
}
5357

5458
/**
5559
* Decrement the value of an item in the cache.
5660
*/
57-
public function decrement(string $key, int $value = 1): bool|int
61+
public function decrement(BackedEnum|UnitEnum|string $key, int $value = 1): bool|int
5862
{
59-
return $this->store->decrement($this->itemKey($key), $value);
63+
return $this->store->decrement($this->itemKey(enum_value($key)), $value);
6064
}
6165

6266
/**

0 commit comments

Comments
 (0)