Skip to content

Commit

Permalink
Do not instantiate our own CacheItemInterface implementation
Browse files Browse the repository at this point in the history
As per psr/cache interface:

Calling Libraries MUST NOT instantiate Item objects themselves. They
may only be requested from a Pool object via the getItem() method
  • Loading branch information
adamnicholson committed Mar 15, 2016
1 parent 20ddae7 commit ee9284c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 94 deletions.
88 changes: 0 additions & 88 deletions src/Decorators/CacheItem.php

This file was deleted.

11 changes: 5 additions & 6 deletions src/Decorators/CachingDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Chief\Command;
use Chief\CommandBus;
use Chief\Decorator;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

class CachingDecorator implements Decorator
Expand Down Expand Up @@ -71,15 +72,13 @@ public function execute(Command $command)
*
* @param CacheableCommand $command
* @param mixed $value
* @return CacheItem
* @return CacheItemInterface
*/
private function createCacheItem(CacheableCommand $command, $value)
{
return new CacheItem(
$this->createCacheKey($command),
$value,
$this->expiresAfter
);
return $this->cache->getItem($this->createCacheKey($command))
->set($value)
->expiresAfter($this->expiresAfter);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Decorators/CachingDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public function test_execution_return_value_is_cached_when_command_implements_ca

$notCachedItem = $this->prophesize(CacheItemInterface::class);
$notCachedItem->isHit()->willReturn(false);
$notCachedItem->getKey()->willReturn(md5(serialize(($command))));
$this->cache->getItem(Argument::any())->willReturn($notCachedItem->reveal());

$notCachedItem->set(7)->shouldBeCalled()->willReturn($notCachedItem->reveal());
$notCachedItem->expiresAfter(3600)->shouldBeCalled()->willReturn($notCachedItem->reveal());

$this->cache->save(Argument::that(function (CacheItemInterface $item) use ($command) {
return $item->getKey() === md5(serialize($command));
}))->shouldBeCalled();
Expand Down

0 comments on commit ee9284c

Please sign in to comment.