Skip to content

[12.x] Allow Unit & Backed enums for cache stores #55172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/Illuminate/Cache/ApcStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Cache;

use function Illuminate\Support\enum_value;

class ApcStore extends TaggableStore
{
use RetrievesMultipleKeys;
Expand Down Expand Up @@ -35,48 +37,56 @@ public function __construct(ApcWrapper $apc, $prefix = '')
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
Copy link
Contributor

@shaedrich shaedrich Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, BackedEnum is a UnitEnum, so I'd say, technically, UnitEnum would suffice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst technically true, the use of both in docblocks is consistent with similar behaviour elsewhere in the framework.

* @return mixed
*/
public function get($key)
{
$key = enum_value($key);

return $this->apc->get($this->prefix.$key);
}

/**
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @param int $seconds
* @return bool
*/
public function put($key, $value, $seconds)
{
$key = enum_value($key);

return $this->apc->put($this->prefix.$key, $value, $seconds);
}

/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @return int|bool
*/
public function increment($key, $value = 1)
{
$key = enum_value($key);

return $this->apc->increment($this->prefix.$key, $value);
}

/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @return int|bool
*/
public function decrement($key, $value = 1)
{
$key = enum_value($key);

return $this->apc->decrement($this->prefix.$key, $value);
}

Expand All @@ -95,11 +105,13 @@ public function forever($key, $value)
/**
* Remove an item from the cache.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @return bool
*/
public function forget($key)
{
$key = enum_value($key);

return $this->apc->delete($this->prefix.$key);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Illuminate/Contracts/Cache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Store
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @return mixed
*/
public function get($key);
Expand All @@ -25,7 +25,7 @@ public function many(array $keys);
/**
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @param int $seconds
* @return bool
Expand All @@ -44,7 +44,7 @@ public function putMany(array $values, $seconds);
/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @return int|bool
*/
Expand All @@ -53,7 +53,7 @@ public function increment($key, $value = 1);
/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @return int|bool
*/
Expand All @@ -62,7 +62,7 @@ public function decrement($key, $value = 1);
/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @param mixed $value
* @return bool
*/
Expand All @@ -71,7 +71,7 @@ public function forever($key, $value);
/**
* Remove an item from the cache.
*
* @param string $key
* @param \BackedEnum|\UnitEnum|string $key
* @return bool
*/
public function forget($key);
Expand Down
131 changes: 110 additions & 21 deletions tests/Cache/CacheApcStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
use Illuminate\Cache\ApcStore;
use Illuminate\Cache\ApcWrapper;
use Mockery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class CacheApcStoreTest extends TestCase
{
public static function resolveKeyNameDataProvider(): array
{
return [
'uses BackedEnum' => [BackedCacheKey::Foo, 'foo'],
'uses UnitEnum' => [UnitCacheKey::Foo, 'Foo'],
'uses normal string' => ['foo', 'foo'],
'uses int' => [100, '100'],
];
}

public function testGetReturnsNullWhenNotFound()
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
Expand All @@ -17,20 +28,22 @@ public function testGetReturnsNullWhenNotFound()
$this->assertNull($store->get('bar'));
}

public function testAPCValueIsReturned()
#[DataProvider('resolveKeyNameDataProvider')]
public function testAPCValueIsReturned(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
$apc->expects($this->once())->method('get')->willReturn('bar');
$apc->expects($this->once())->method('get')->with($this->equalTo($expected))->willReturn('bar');
$store = new ApcStore($apc);
$this->assertSame('bar', $store->get('foo'));
$this->assertSame('bar', $store->get($key));
}

public function testAPCFalseValueIsReturned()
#[DataProvider('resolveKeyNameDataProvider')]
public function testAPCFalseValueIsReturned(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
$apc->expects($this->once())->method('get')->willReturn(false);
$apc->expects($this->once())->method('get')->with($this->equalTo($expected))->willReturn(false);
$store = new ApcStore($apc);
$this->assertFalse($store->get('foo'));
$this->assertFalse($store->get($key));
}

public function testGetMultipleReturnsNullWhenNotFoundAndValueWhenFound()
Expand All @@ -49,14 +62,15 @@ public function testGetMultipleReturnsNullWhenNotFoundAndValueWhenFound()
], $store->many(['foo', 'bar', 'baz']));
}

public function testSetMethodProperlyCallsAPC()
#[DataProvider('resolveKeyNameDataProvider')]
public function testSetMethodProperlyCallsAPC(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
$apc->expects($this->once())
->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(60))
->method('put')->with($this->equalTo($expected), $this->equalTo('bar'), $this->equalTo(60))
->willReturn(true);
$store = new ApcStore($apc);
$result = $store->put('foo', 'bar', 60);
$result = $store->put($key, 'bar', 60);
$this->assertTrue($result);
}

Expand Down Expand Up @@ -88,39 +102,43 @@ public function testSetMultipleMethodProperlyCallsAPC()
$this->assertTrue($result);
}

public function testIncrementMethodProperlyCallsAPC()
#[DataProvider('resolveKeyNameDataProvider')]
public function testIncrementMethodProperlyCallsAPC(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['increment'])->getMock();
$apc->expects($this->once())->method('increment')->with($this->equalTo('foo'), $this->equalTo(5));
$apc->expects($this->once())->method('increment')->with($this->equalTo($expected), $this->equalTo(5));
$store = new ApcStore($apc);
$store->increment('foo', 5);
$store->increment($key, 5);
}

public function testDecrementMethodProperlyCallsAPC()
#[DataProvider('resolveKeyNameDataProvider')]
public function testDecrementMethodProperlyCallsAPC(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['decrement'])->getMock();
$apc->expects($this->once())->method('decrement')->with($this->equalTo('foo'), $this->equalTo(5));
$apc->expects($this->once())->method('decrement')->with($this->equalTo($expected), $this->equalTo(5));
$store = new ApcStore($apc);
$store->decrement('foo', 5);
$store->decrement($key, 5);
}

public function testStoreItemForeverProperlyCallsAPC()
#[DataProvider('resolveKeyNameDataProvider')]
public function testStoreItemForeverProperlyCallsAPC(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
$apc->expects($this->once())
->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(0))
->method('put')->with($this->equalTo($expected), $this->equalTo('bar'), $this->equalTo(0))
->willReturn(true);
$store = new ApcStore($apc);
$result = $store->forever('foo', 'bar');
$result = $store->forever($key, 'bar');
$this->assertTrue($result);
}

public function testForgetMethodProperlyCallsAPC()
#[DataProvider('resolveKeyNameDataProvider')]
public function testForgetMethodProperlyCallsAPC(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['delete'])->getMock();
$apc->expects($this->once())->method('delete')->with($this->equalTo('foo'))->willReturn(true);
$apc->expects($this->once())->method('delete')->with($this->equalTo($expected))->willReturn(true);
$store = new ApcStore($apc);
$result = $store->forget('foo');
$result = $store->forget($key);
$this->assertTrue($result);
}

Expand All @@ -132,4 +150,75 @@ public function testFlushesCached()
$result = $store->flush();
$this->assertTrue($result);
}

#[DataProvider('resolveKeyNameDataProvider')]
public function testCanGetWithEnum(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
$apc->expects($this->once())->method('get')->with($this->equalTo($expected))->willReturn('bar');
$store = new ApcStore($apc);
$this->assertSame('bar', $store->get($key));
}

#[DataProvider('resolveKeyNameDataProvider')]
public function testCanPutWithEnum(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
$apc->expects($this->once())
->method('put')->with($this->equalTo($expected), $this->equalTo('bar'), $this->equalTo(60))
->willReturn(true);
$store = new ApcStore($apc);
$result = $store->put($key, 'bar', 60);
$this->assertTrue($result);
}

#[DataProvider('resolveKeyNameDataProvider')]
public function testCanIncrementWithEnum(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['increment'])->getMock();
$apc->expects($this->once())->method('increment')->with($this->equalTo($expected), $this->equalTo(5));
$store = new ApcStore($apc);
$store->increment($key, 5);
}

#[DataProvider('resolveKeyNameDataProvider')]
public function testCanDecrementWithEnum(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['decrement'])->getMock();
$apc->expects($this->once())->method('decrement')->with($this->equalTo($expected), $this->equalTo(5));
$store = new ApcStore($apc);
$store->decrement($key, 5);
}

#[DataProvider('resolveKeyNameDataProvider')]
public function testCanStoreItemForeverWithEnum(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
$apc->expects($this->once())
->method('put')->with($this->equalTo($expected), $this->equalTo('bar'), $this->equalTo(0))
->willReturn(true);
$store = new ApcStore($apc);
$result = $store->forever($key, 'bar');
$this->assertTrue($result);
}

#[DataProvider('resolveKeyNameDataProvider')]
public function testCanForgetItemWithEnum(mixed $key, string $expected)
{
$apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['delete'])->getMock();
$apc->expects($this->once())->method('delete')->with($this->equalTo($expected))->willReturn(true);
$store = new ApcStore($apc);
$result = $store->forget($key);
$this->assertTrue($result);
}
}

enum UnitCacheKey
{
case Foo;
}

enum BackedCacheKey: string
{
case Foo = 'foo';
}
Loading