Skip to content

Commit 39343c7

Browse files
authored
[13.x] Add enum types to repository contract / allow enums for tagged caches (#58481)
* Update Repository.php * update extenders, so happy family * Update CacheRepositoryTest.php * Update Repository.php * cs up in this joint
1 parent 0aa20aa commit 39343c7

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

src/Illuminate/Cache/RedisTaggedCache.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99
use Illuminate\Redis\Connections\PredisClusterConnection;
1010
use Illuminate\Redis\Connections\PredisConnection;
1111

12+
use function Illuminate\Support\enum_value;
13+
1214
class RedisTaggedCache extends TaggedCache
1315
{
1416
/**
1517
* Store an item in the cache if the key does not exist.
1618
*
17-
* @param string $key
19+
* @param \BackedEnum|\UnitEnum|string $key
1820
* @param mixed $value
1921
* @param \DateTimeInterface|\DateInterval|int|null $ttl
2022
* @return bool
2123
*/
2224
public function add($key, $value, $ttl = null)
2325
{
26+
$key = enum_value($key);
27+
2428
$seconds = null;
2529

2630
if ($ttl !== null) {
@@ -40,13 +44,15 @@ public function add($key, $value, $ttl = null)
4044
/**
4145
* Store an item in the cache.
4246
*
43-
* @param string $key
47+
* @param \BackedEnum|\UnitEnum|string $key
4448
* @param mixed $value
4549
* @param \DateTimeInterface|\DateInterval|int|null $ttl
4650
* @return bool
4751
*/
4852
public function put($key, $value, $ttl = null)
4953
{
54+
$key = enum_value($key);
55+
5056
if (is_null($ttl)) {
5157
return $this->forever($key, $value);
5258
}
@@ -66,12 +72,14 @@ public function put($key, $value, $ttl = null)
6672
/**
6773
* Increment the value of an item in the cache.
6874
*
69-
* @param string $key
75+
* @param \BackedEnum|\UnitEnum|string $key
7076
* @param mixed $value
7177
* @return int|bool
7278
*/
7379
public function increment($key, $value = 1)
7480
{
81+
$key = enum_value($key);
82+
7583
$this->tags->addEntry($this->itemKey($key), updateWhen: 'NX');
7684

7785
return parent::increment($key, $value);
@@ -80,7 +88,7 @@ public function increment($key, $value = 1)
8088
/**
8189
* Decrement the value of an item in the cache.
8290
*
83-
* @param string $key
91+
* @param \BackedEnum|\UnitEnum|string $key
8492
* @param mixed $value
8593
* @return int|bool
8694
*/
@@ -94,12 +102,14 @@ public function decrement($key, $value = 1)
94102
/**
95103
* Store an item in the cache indefinitely.
96104
*
97-
* @param string $key
105+
* @param \BackedEnum|\UnitEnum|string $key
98106
* @param mixed $value
99107
* @return bool
100108
*/
101109
public function forever($key, $value)
102110
{
111+
$key = enum_value($key);
112+
103113
$this->tags->addEntry($this->itemKey($key));
104114

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

src/Illuminate/Cache/TaggedCache.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Cache\Events\CacheFlushing;
77
use Illuminate\Contracts\Cache\Store;
88

9+
use function Illuminate\Support\enum_value;
10+
911
class TaggedCache extends Repository
1012
{
1113
use RetrievesMultipleKeys {
@@ -51,25 +53,25 @@ public function putMany(array $values, $ttl = null)
5153
/**
5254
* Increment the value of an item in the cache.
5355
*
54-
* @param string $key
56+
* @param \BackedEnum|\UnitEnum|string $key
5557
* @param mixed $value
5658
* @return int|bool
5759
*/
5860
public function increment($key, $value = 1)
5961
{
60-
return $this->store->increment($this->itemKey($key), $value);
62+
return $this->store->increment($this->itemKey(enum_value($key)), $value);
6163
}
6264

6365
/**
6466
* Decrement the value of an item in the cache.
6567
*
66-
* @param string $key
68+
* @param \BackedEnum|\UnitEnum|string $key
6769
* @param mixed $value
6870
* @return int|bool
6971
*/
7072
public function decrement($key, $value = 1)
7173
{
72-
return $this->store->decrement($this->itemKey($key), $value);
74+
return $this->store->decrement($this->itemKey(enum_value($key)), $value);
7375
}
7476

7577
/**

src/Illuminate/Contracts/Cache/Repository.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface Repository extends CacheInterface
1212
*
1313
* @template TCacheValue
1414
*
15-
* @param array|string $key
15+
* @param \BackedEnum|\UnitEnum|array|string $key
1616
* @param TCacheValue|(\Closure(): TCacheValue) $default
1717
* @return (TCacheValue is null ? mixed : TCacheValue)
1818
*/
@@ -21,7 +21,7 @@ public function pull($key, $default = null);
2121
/**
2222
* Store an item in the cache.
2323
*
24-
* @param string $key
24+
* @param \BackedEnum|\UnitEnum|string $key
2525
* @param mixed $value
2626
* @param \DateTimeInterface|\DateInterval|int|null $ttl
2727
* @return bool
@@ -31,7 +31,7 @@ public function put($key, $value, $ttl = null);
3131
/**
3232
* Store an item in the cache if the key does not exist.
3333
*
34-
* @param string $key
34+
* @param \BackedEnum|\UnitEnum|string $key
3535
* @param mixed $value
3636
* @param \DateTimeInterface|\DateInterval|int|null $ttl
3737
* @return bool
@@ -41,7 +41,7 @@ public function add($key, $value, $ttl = null);
4141
/**
4242
* Increment the value of an item in the cache.
4343
*
44-
* @param string $key
44+
* @param \BackedEnum|\UnitEnum|string $key
4545
* @param mixed $value
4646
* @return int|bool
4747
*/
@@ -50,7 +50,7 @@ public function increment($key, $value = 1);
5050
/**
5151
* Decrement the value of an item in the cache.
5252
*
53-
* @param string $key
53+
* @param \BackedEnum|\UnitEnum|string $key
5454
* @param mixed $value
5555
* @return int|bool
5656
*/
@@ -59,7 +59,7 @@ public function decrement($key, $value = 1);
5959
/**
6060
* Store an item in the cache indefinitely.
6161
*
62-
* @param string $key
62+
* @param \BackedEnum|\UnitEnum|string $key
6363
* @param mixed $value
6464
* @return bool
6565
*/
@@ -70,7 +70,7 @@ public function forever($key, $value);
7070
*
7171
* @template TCacheValue
7272
*
73-
* @param string $key
73+
* @param \BackedEnum|\UnitEnum|string $key
7474
* @param \DateTimeInterface|\DateInterval|\Closure|int|null $ttl
7575
* @param \Closure(): TCacheValue $callback
7676
* @return TCacheValue
@@ -82,7 +82,7 @@ public function remember($key, $ttl, Closure $callback);
8282
*
8383
* @template TCacheValue
8484
*
85-
* @param string $key
85+
* @param \BackedEnum|\UnitEnum|string $key
8686
* @param \Closure(): TCacheValue $callback
8787
* @return TCacheValue
8888
*/
@@ -93,7 +93,7 @@ public function sear($key, Closure $callback);
9393
*
9494
* @template TCacheValue
9595
*
96-
* @param string $key
96+
* @param \BackedEnum|\UnitEnum|string $key
9797
* @param \Closure(): TCacheValue $callback
9898
* @return TCacheValue
9999
*/
@@ -102,7 +102,7 @@ public function rememberForever($key, Closure $callback);
102102
/**
103103
* Set the expiration of a cached item; null TTL will retain the item forever.
104104
*
105-
* @param string $key
105+
* @param \BackedEnum|\UnitEnum|string $key
106106
* @param \DateTimeInterface|\DateInterval|int|null $ttl
107107
* @return bool
108108
*/
@@ -111,7 +111,7 @@ public function touch($key, $ttl = null);
111111
/**
112112
* Remove an item from the cache.
113113
*
114-
* @param string $key
114+
* @param \BackedEnum|\UnitEnum|string $key
115115
* @return bool
116116
*/
117117
public function forget($key);

tests/Cache/CacheRepositoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,15 @@ public function testAtomicThrowsOnLockTimeout()
548548
}
549549
}
550550

551+
public function testTaggedCacheWorksWithEnumKey()
552+
{
553+
$cache = (new Repository(new ArrayStore()))->tags('test-tag');
554+
555+
$cache->put(TestCacheKey::FOO, 5);
556+
$this->assertSame(6, $cache->increment(TestCacheKey::FOO));
557+
$this->assertSame(5, $cache->decrement(TestCacheKey::FOO));
558+
}
559+
551560
protected function getRepository()
552561
{
553562
$dispatcher = new Dispatcher(m::mock(Container::class));
@@ -563,3 +572,8 @@ protected static function getTestDate()
563572
return '2030-07-25 12:13:14 UTC';
564573
}
565574
}
575+
576+
enum TestCacheKey: string
577+
{
578+
case FOO = 'foo';
579+
}

0 commit comments

Comments
 (0)