Skip to content

Commit bedda64

Browse files
wip: update phpdoc, implement enum key resolution on APC
1 parent 46ac782 commit bedda64

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/Illuminate/Cache/ApcStore.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Cache;
44

5+
use function Illuminate\Support\enum_value;
6+
57
class ApcStore extends TaggableStore
68
{
79
use RetrievesMultipleKeys;
@@ -35,48 +37,56 @@ public function __construct(ApcWrapper $apc, $prefix = '')
3537
/**
3638
* Retrieve an item from the cache by key.
3739
*
38-
* @param string $key
40+
* @param \BackedEnum|\UnitEnum|string $key
3941
* @return mixed
4042
*/
4143
public function get($key)
4244
{
45+
$key = enum_value($key);
46+
4347
return $this->apc->get($this->prefix.$key);
4448
}
4549

4650
/**
4751
* Store an item in the cache for a given number of seconds.
4852
*
49-
* @param string $key
53+
* @param \BackedEnum|\UnitEnum|string $key
5054
* @param mixed $value
5155
* @param int $seconds
5256
* @return bool
5357
*/
5458
public function put($key, $value, $seconds)
5559
{
60+
$key = enum_value($key);
61+
5662
return $this->apc->put($this->prefix.$key, $value, $seconds);
5763
}
5864

5965
/**
6066
* Increment the value of an item in the cache.
6167
*
62-
* @param string $key
68+
* @param \BackedEnum|\UnitEnum|string $key
6369
* @param mixed $value
6470
* @return int|bool
6571
*/
6672
public function increment($key, $value = 1)
6773
{
74+
$key = enum_value($key);
75+
6876
return $this->apc->increment($this->prefix.$key, $value);
6977
}
7078

7179
/**
7280
* Decrement the value of an item in the cache.
7381
*
74-
* @param string $key
82+
* @param \BackedEnum|\UnitEnum|string $key
7583
* @param mixed $value
7684
* @return int|bool
7785
*/
7886
public function decrement($key, $value = 1)
7987
{
88+
$key = enum_value($key);
89+
8090
return $this->apc->decrement($this->prefix.$key, $value);
8191
}
8292

@@ -95,11 +105,13 @@ public function forever($key, $value)
95105
/**
96106
* Remove an item from the cache.
97107
*
98-
* @param string $key
108+
* @param \BackedEnum|\UnitEnum|string $key
99109
* @return bool
100110
*/
101111
public function forget($key)
102112
{
113+
$key = enum_value($key);
114+
103115
return $this->apc->delete($this->prefix.$key);
104116
}
105117

src/Illuminate/Contracts/Cache/Store.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Store
77
/**
88
* Retrieve an item from the cache by key.
99
*
10-
* @param string $key
10+
* @param \BackedEnum|\UnitEnum|string $key
1111
* @return mixed
1212
*/
1313
public function get($key);
@@ -25,7 +25,7 @@ public function many(array $keys);
2525
/**
2626
* Store an item in the cache for a given number of seconds.
2727
*
28-
* @param string $key
28+
* @param \BackedEnum|\UnitEnum|string $key
2929
* @param mixed $value
3030
* @param int $seconds
3131
* @return bool
@@ -44,7 +44,7 @@ public function putMany(array $values, $seconds);
4444
/**
4545
* Increment the value of an item in the cache.
4646
*
47-
* @param string $key
47+
* @param \BackedEnum|\UnitEnum|string $key
4848
* @param mixed $value
4949
* @return int|bool
5050
*/
@@ -53,7 +53,7 @@ public function increment($key, $value = 1);
5353
/**
5454
* Decrement the value of an item in the cache.
5555
*
56-
* @param string $key
56+
* @param \BackedEnum|\UnitEnum|string $key
5757
* @param mixed $value
5858
* @return int|bool
5959
*/
@@ -62,7 +62,7 @@ public function decrement($key, $value = 1);
6262
/**
6363
* Store an item in the cache indefinitely.
6464
*
65-
* @param string $key
65+
* @param \BackedEnum|\UnitEnum|string $key
6666
* @param mixed $value
6767
* @return bool
6868
*/
@@ -71,7 +71,7 @@ public function forever($key, $value);
7171
/**
7272
* Remove an item from the cache.
7373
*
74-
* @param string $key
74+
* @param \BackedEnum|\UnitEnum|string $key
7575
* @return bool
7676
*/
7777
public function forget($key);

tests/Cache/CacheApcStoreTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55
use Illuminate\Cache\ApcStore;
66
use Illuminate\Cache\ApcWrapper;
77
use Mockery;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\TestCase;
910

1011
class CacheApcStoreTest extends TestCase
1112
{
13+
public static function resolveKeyNameDataProvider(): array
14+
{
15+
return [
16+
'uses BackedEnum' => [BackedCacheKey::Foo, 'foo'],
17+
'uses UnitEnum' => [UnitCacheKey::Foo, 'Foo'],
18+
'uses normal string' => ['foo', 'foo'],
19+
'uses int' => [100, '100'],
20+
];
21+
}
22+
1223
public function testGetReturnsNullWhenNotFound()
1324
{
1425
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
@@ -132,4 +143,13 @@ public function testFlushesCached()
132143
$result = $store->flush();
133144
$this->assertTrue($result);
134145
}
146+
147+
enum UnitCacheKey
148+
{
149+
case Foo;
150+
}
151+
152+
enum BackedCacheKey: string
153+
{
154+
case Foo = 'foo';
135155
}

0 commit comments

Comments
 (0)