55namespace Hypervel \Cache ;
66
77use ArrayAccess ;
8+ use BackedEnum ;
89use BadMethodCallException ;
910use Carbon \Carbon ;
1011use Closure ;
2930use Hypervel \Cache \Events \WritingKey ;
3031use Hypervel \Cache \Events \WritingManyKeys ;
3132use 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 {
0 commit comments