Skip to content

Commit

Permalink
Expanded compatibility with psr/cache to include v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Dec 22, 2022
1 parent 98d0ab9 commit 291d172
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": "^8.1",
"async/throttle": "^4",
"psr/cache": "^1|^2",
"psr/cache": "^1|^2|^3",
"psr/container": "^1|^2",
"scriptfusion/retry": "^5",
"scriptfusion/retry-exception-handlers": "^1.2",
Expand Down
6 changes: 3 additions & 3 deletions src/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function get(): mixed
return $this->value;
}

public function set(mixed $value): self
public function set(mixed $value): static
{
$this->value = $value;

Expand All @@ -36,12 +36,12 @@ public function isHit(): bool
return $this->hit;
}

public function expiresAt($expiration): self
public function expiresAt($expiration): static
{
throw new NotImplementedException;
}

public function expiresAfter($time): self
public function expiresAfter($time): static
{
throw new NotImplementedException;
}
Expand Down
22 changes: 16 additions & 6 deletions src/Cache/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class MemoryCache extends \ArrayObject implements CacheItemPoolInterface
/**
* @param string $key
*/
public function getItem($key): mixed
public function getItem($key): CacheItemInterface
{
return \Closure::bind(
function () use ($key): self {
Expand All @@ -37,17 +37,21 @@ public function hasItem($key): bool
return isset($this[$key]);
}

public function clear(): void
public function clear(): bool
{
$this->exchangeArray([]);

return true;
}

public function deleteItem($key): void
public function deleteItem($key): bool
{
unset($this[$key]);

return true;
}

public function deleteItems(array $keys): void
public function deleteItems(array $keys): bool
{
foreach ($keys as $key) {
if (!$this->hasItem($key)) {
Expand All @@ -56,16 +60,22 @@ public function deleteItems(array $keys): void

$this->deleteItem($key);
}

return true;
}

public function save(CacheItemInterface $item): void
public function save(CacheItemInterface $item): bool
{
$this[$item->getKey()] = $item->get();

return true;
}

public function saveDeferred(CacheItemInterface $item): void
public function saveDeferred(CacheItemInterface $item): bool
{
$this->save($item);

return true;
}

public function commit(): bool
Expand Down
13 changes: 8 additions & 5 deletions test/Unit/Cache/MemoryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use ScriptFUSION\Porter\Cache\InvalidArgumentException;
use ScriptFUSION\Porter\Cache\MemoryCache;

/**
* @see MemoryCache
*/
final class MemoryCacheTest extends TestCase
{
private MemoryCache $cache;
Expand Down Expand Up @@ -50,21 +53,21 @@ public function testHasItem(): void

public function testClear(): void
{
$this->cache->clear();
self::assertTrue($this->cache->clear());

self::assertEmpty($this->cache->getArrayCopy());
}

public function testDeleteItem(): void
{
$this->cache->deleteItem('foo');
self::assertTrue($this->cache->deleteItem('foo'));

self::assertFalse($this->cache->hasItem('foo'));
}

public function testDeleteItems(): void
{
$this->cache->deleteItems(['foo']);
self::assertTrue($this->cache->deleteItems(['foo']));

self::assertEmpty($this->cache->getArrayCopy());
}
Expand All @@ -78,14 +81,14 @@ public function testDeleteInvalidItem(): void

public function testSave(): void
{
$this->cache->save($this->cache->getItem('bar')->set('baz'));
self::assertTrue($this->cache->save($this->cache->getItem('bar')->set('baz')));

self::assertSame('baz', $this->cache->getItem('bar')->get());
}

public function testSaveDeferred(): void
{
$this->cache->saveDeferred($this->cache->getItem('bar')->set('baz'));
self::assertTrue($this->cache->saveDeferred($this->cache->getItem('bar')->set('baz')));

self::assertSame('baz', $this->cache->getItem('bar')->get());
}
Expand Down

0 comments on commit 291d172

Please sign in to comment.