Skip to content

add some local caches where they are missing #1892

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

Merged
merged 1 commit into from
Apr 26, 2018
Merged
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
29 changes: 21 additions & 8 deletions src/Bridge/Symfony/Routing/CachedRouteNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class CachedRouteNameResolver implements RouteNameResolverInterface

private $cacheItemPool;
private $decorated;
private $localCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, RouteNameResolverInterface $decorated)
{
Expand All @@ -42,21 +43,33 @@ public function getRouteName(string $resourceClass, $operationType /**, array $c
$context = \func_num_args() > 2 ? func_get_arg(2) : [];
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $operationType, $context['subresource_resources'] ?? null]));

if (isset($this->localCache[$cacheKey])) {
return $this->localCache[$cacheKey];
}

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
} catch (CacheException $e) {
return $this->decorated->getRouteName($resourceClass, $operationType, $context);
}

if ($cacheItem->isHit()) {
return $cacheItem->get();
if ($cacheItem->isHit()) {
return $this->localCache[$cacheKey] = $cacheItem->get();
}
} catch (CacheException $e) {
//do nothing
}

$routeName = $this->decorated->getRouteName($resourceClass, $operationType, $context);

$cacheItem->set($routeName);
$this->cacheItemPool->save($cacheItem);
if (!isset($cacheItem)) {
return $this->localCache[$cacheKey] = $routeName;
}

try {
$cacheItem->set($routeName);
$this->cacheItemPool->save($cacheItem);
} catch (CacheException $e) {
// do nothing
}

return $routeName;
return $this->localCache[$cacheKey] = $routeName;
}
}
23 changes: 18 additions & 5 deletions src/Operation/Factory/CachedSubresourceOperationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class CachedSubresourceOperationFactory implements SubresourceOperationFac

private $cacheItemPool;
private $decorated;
private $localCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, SubresourceOperationFactoryInterface $decorated)
{
Expand All @@ -39,21 +40,33 @@ public function create(string $resourceClass): array
{
$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass);

if (isset($this->localCache[$cacheKey])) {
return $this->localCache[$cacheKey];
}

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $cacheItem->get();
return $this->localCache[$cacheKey] = $cacheItem->get();
}
} catch (CacheException $e) {
return $this->decorated->create($resourceClass);
// do nothing
}

$subresourceOperations = $this->decorated->create($resourceClass);

$cacheItem->set($subresourceOperations);
$this->cacheItemPool->save($cacheItem);
if (!isset($cacheItem)) {
return $this->localCache[$cacheKey] = $subresourceOperations;
}

try {
$cacheItem->set($subresourceOperations);
$this->cacheItemPool->save($cacheItem);
} catch (CacheException $e) {
// do nothing
}

return $subresourceOperations;
return $this->localCache[$cacheKey] = $subresourceOperations;
}
}
66 changes: 42 additions & 24 deletions tests/Bridge/Symfony/Routing/CachedRouteNameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ApiPlatform\Core\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

Expand Down Expand Up @@ -63,39 +64,39 @@ public function testGetRouteNameForItemRouteWithNoMatchingRoute()
public function testGetRouteNameForItemRouteOnCacheMiss()
{
$cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
$cacheItemProphecy->isHit()->willReturn(false)->shouldBeCalled();
$cacheItemProphecy->set('some_item_route')->shouldBeCalled();
$cacheItemProphecy->isHit()->willReturn(false)->shouldBeCalledTimes(1);
$cacheItemProphecy->set('some_item_route')->shouldBeCalledTimes(1);

$cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->save($cacheItemProphecy)->willReturn(true)->shouldBeCalled();
$cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->save($cacheItemProphecy)->shouldBeCalledTimes(1)->willReturn(true);

$decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
$decoratedProphecy->getRouteName('AppBundle\Entity\User', false, [])->willReturn('some_item_route')->shouldBeCalled();
$decoratedProphecy->getRouteName('AppBundle\Entity\User', false, [])->willReturn('some_item_route')->shouldBeCalledTimes(1);

$cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
$actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', false);

$this->assertSame('some_item_route', $actual);
$this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', false));
$this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', false), 'Trigger the local cache');
}

public function testGetRouteNameForItemRouteOnCacheHit()
{
$cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
$cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalled();
$cacheItemProphecy->get()->willReturn('some_item_route')->shouldBeCalled();
$cacheItemProphecy->isHit()->shouldBeCalledTimes(1)->willReturn(true);
$cacheItemProphecy->get()->shouldBeCalledTimes(1)->willReturn('some_item_route');

$cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->save($cacheItemProphecy)->shouldNotBeCalled();

$decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
$decoratedProphecy->getRouteName(Argument::cetera())->shouldNotBeCalled();

$cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
$actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM);

$this->assertSame('some_item_route', $actual);
$this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM));
$this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM), 'Trigger the local cache');
}

/**
Expand Down Expand Up @@ -123,38 +124,55 @@ public function testGetRouteNameForCollectionRouteWithNoMatchingRoute()
public function testGetRouteNameForCollectionRouteOnCacheMiss()
{
$cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
$cacheItemProphecy->isHit()->willReturn(false)->shouldBeCalled();
$cacheItemProphecy->set('some_collection_route')->shouldBeCalled();
$cacheItemProphecy->isHit()->shouldBeCalledTimes(1)->willReturn(false);
$cacheItemProphecy->set('some_collection_route')->shouldBeCalledTimes(1);

$cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->save($cacheItemProphecy)->willReturn(true)->shouldBeCalled();
$cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->save($cacheItemProphecy)->shouldBeCalledTimes(1)->willReturn(true);

$decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
$decoratedProphecy->getRouteName('AppBundle\Entity\User', true, [])->willReturn('some_collection_route')->shouldBeCalled();
$decoratedProphecy->getRouteName('AppBundle\Entity\User', true, [])->willReturn('some_collection_route')->shouldBeCalledTimes(1);

$cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
$actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', true);

$this->assertSame('some_collection_route', $actual);
$this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', true));
$this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', true), 'Trigger the local cache');
}

public function testGetRouteNameForCollectionRouteOnCacheHit()
{
$cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
$cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalled();
$cacheItemProphecy->get()->willReturn('some_collection_route')->shouldBeCalled();
$cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalledTimes(1);
$cacheItemProphecy->get()->willReturn('some_collection_route')->shouldBeCalledTimes(1);

$cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy);
$cacheItemPoolProphecy->save($cacheItemProphecy)->shouldNotBeCalled();

$decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
$decoratedProphecy->getRouteName(Argument::cetera())->shouldNotBeCalled();

$cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
$actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION);

$this->assertSame('some_collection_route', $actual);
$this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION));
$this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION), 'Trigger the local cache');
}

public function testGetRouteNameWithCacheItemThrowsCacheException()
{
$cacheException = $this->prophesize(CacheException::class);
$cacheException->willExtend(\Exception::class);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willThrow($cacheException->reveal());

$decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
$decoratedProphecy->getRouteName('AppBundle\Entity\User', OperationType::ITEM, [])->willReturn('some_item_route')->shouldBeCalledTimes(1);

$cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPool->reveal(), $decoratedProphecy->reveal());

$this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM));
$this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM), 'Trigger the local cache');
}
}
35 changes: 19 additions & 16 deletions tests/Operation/Factory/CachedSubresourceOperationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,40 @@ class CachedSubresourceOperationFactoryTest extends TestCase
public function testCreateWithItemHit()
{
$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(true)->shouldBeCalled();
$cacheItem->get()->willReturn(['foo' => 'bar'])->shouldBeCalled();
$cacheItem->isHit()->willReturn(true)->shouldBeCalledTimes(1);
$cacheItem->get()->willReturn(['foo' => 'bar'])->shouldBeCalledTimes(1);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalledTimes(1);

$decoratedSubresourceOperationFactory = $this->prophesize(SubresourceOperationFactoryInterface::class);
$decoratedSubresourceOperationFactory->create()->shouldNotBeCalled();

$cachedSubresourceOperationFactory = new CachedSubresourceOperationFactory($cacheItemPool->reveal(), $decoratedSubresourceOperationFactory->reveal());
$resultedSubresourceOperation = $cachedSubresourceOperationFactory->create(Dummy::class);

$this->assertEquals(['foo' => 'bar'], $resultedSubresourceOperation);
$expectedResult = ['foo' => 'bar'];
$this->assertEquals($expectedResult, $cachedSubresourceOperationFactory->create(Dummy::class));
$this->assertEquals($expectedResult, $cachedSubresourceOperationFactory->create(Dummy::class), 'Trigger the local cache');
}

public function testCreateWithItemNotHit()
{
$cacheItem = $this->prophesize(CacheItemInterface::class);
$cacheItem->isHit()->willReturn(false)->shouldBeCalled();
$cacheItem->set(['foo' => 'bar'])->willReturn($cacheItem->reveal())->shouldBeCalled();
$cacheItem->isHit()->willReturn(false)->shouldBeCalledTimes(1);
$cacheItem->set(['foo' => 'bar'])->willReturn($cacheItem->reveal())->shouldBeCalledTimes(1);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
$cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalled();
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalledTimes(1);
$cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalledTimes(1);

$decoratedSubresourceOperationFactory = $this->prophesize(SubresourceOperationFactoryInterface::class);
$decoratedSubresourceOperationFactory->create(Dummy::class)->shouldBeCalled()->willReturn(['foo' => 'bar']);
$decoratedSubresourceOperationFactory->create(Dummy::class)->shouldBeCalledTimes(1)->willReturn(['foo' => 'bar']);

$cachedSubresourceOperationFactory = new CachedSubresourceOperationFactory($cacheItemPool->reveal(), $decoratedSubresourceOperationFactory->reveal());
$resultedSubresourceOperation = $cachedSubresourceOperationFactory->create(Dummy::class);

$this->assertEquals(['foo' => 'bar'], $resultedSubresourceOperation);
$expectedResult = ['foo' => 'bar'];
$this->assertEquals($expectedResult, $cachedSubresourceOperationFactory->create(Dummy::class));
$this->assertEquals($expectedResult, $cachedSubresourceOperationFactory->create(Dummy::class), 'Trigger the local cache');
}

public function testCreateWithGetCacheItemThrowsCacheException()
Expand All @@ -69,15 +71,16 @@ public function testCreateWithGetCacheItemThrowsCacheException()
$cacheException->willExtend(\Exception::class);

$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
$cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();
$cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalledTimes(1);

$decoratedSubresourceOperationFactory = $this->prophesize(SubresourceOperationFactoryInterface::class);
$decoratedSubresourceOperationFactory->create(Dummy::class)->shouldBeCalled()->willReturn(['foo' => 'bar']);
$decoratedSubresourceOperationFactory->create(Dummy::class)->shouldBeCalledTimes(1)->willReturn(['foo' => 'bar']);

$cachedSubresourceOperationFactory = new CachedSubresourceOperationFactory($cacheItemPool->reveal(), $decoratedSubresourceOperationFactory->reveal());
$resultedSubresourceOperation = $cachedSubresourceOperationFactory->create(Dummy::class);

$this->assertEquals(['foo' => 'bar'], $resultedSubresourceOperation);
$expectedResult = ['foo' => 'bar'];
$this->assertEquals($expectedResult, $cachedSubresourceOperationFactory->create(Dummy::class));
$this->assertEquals($expectedResult, $cachedSubresourceOperationFactory->create(Dummy::class), 'Trigger the local cache');
}

private function generateCacheKey(string $resourceClass = Dummy::class)
Expand Down