Skip to content

Commit

Permalink
Merge pull request #1908 from bendavies/cache-trait
Browse files Browse the repository at this point in the history
RFC: Cache trait
  • Loading branch information
teohhanhui committed Apr 30, 2018
2 parents 592af01 + 6361a21 commit dcdea86
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 159 deletions.
37 changes: 6 additions & 31 deletions src/Bridge/Symfony/Routing/CachedRouteNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Core\Bridge\Symfony\Routing;

use Psr\Cache\CacheException;
use ApiPlatform\Core\Cache\CachedTrait;
use Psr\Cache\CacheItemPoolInterface;

/**
Expand All @@ -23,11 +23,11 @@
*/
final class CachedRouteNameResolver implements RouteNameResolverInterface
{
use CachedTrait;

const CACHE_KEY_PREFIX = 'route_name_';

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

public function __construct(CacheItemPoolInterface $cacheItemPool, RouteNameResolverInterface $decorated)
{
Expand All @@ -43,33 +43,8 @@ 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);

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

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

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

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

return $this->localCache[$cacheKey] = $routeName;
return $this->getCached($cacheKey, function () use ($resourceClass, $operationType, $context) {
return $this->decorated->getRouteName($resourceClass, $operationType, $context);
});
}
}
59 changes: 59 additions & 0 deletions src/Cache/CachedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Cache;

use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

/**
* @internal
*/
trait CachedTrait
{
/** @var CacheItemPoolInterface */
private $cacheItemPool;
private $localCache = [];

private function getCached(string $cacheKey, callable $getValue)
{
if (array_key_exists($cacheKey, $this->localCache)) {
return $this->localCache[$cacheKey];
}

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

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

$value = $getValue();

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

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

return $this->localCache[$cacheKey] = $value;
}
}
32 changes: 7 additions & 25 deletions src/Metadata/Property/Factory/CachedPropertyMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\Core\Metadata\Property\Factory;

use ApiPlatform\Core\Cache\CachedTrait;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

/**
Expand All @@ -24,11 +24,11 @@
*/
final class CachedPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
use CachedTrait;

const CACHE_KEY_PREFIX = 'property_metadata_';

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

public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetadataFactoryInterface $decorated)
{
Expand All @@ -41,28 +41,10 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetad
*/
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
{
$localCacheKey = serialize([$resourceClass, $property, $options]);
if (isset($this->localCache[$localCacheKey])) {
return $this->localCache[$localCacheKey];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($localCacheKey);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
} catch (CacheException $e) {
return $this->localCache[$localCacheKey] = $this->decorated->create($resourceClass, $property, $options);
}

if ($cacheItem->isHit()) {
return $this->localCache[$localCacheKey] = $cacheItem->get();
}

$propertyMetadata = $this->decorated->create($resourceClass, $property, $options);

$cacheItem->set($propertyMetadata);
$this->cacheItemPool->save($cacheItem);
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));

return $this->localCache[$localCacheKey] = $propertyMetadata;
return $this->getCached($cacheKey, function () use ($resourceClass, $property, $options) {
return $this->decorated->create($resourceClass, $property, $options);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\Core\Metadata\Property\Factory;

use ApiPlatform\Core\Cache\CachedTrait;
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

/**
Expand All @@ -24,11 +24,11 @@
*/
final class CachedPropertyNameCollectionFactory implements PropertyNameCollectionFactoryInterface
{
use CachedTrait;

const CACHE_KEY_PREFIX = 'property_name_collection_';

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

public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameCollectionFactoryInterface $decorated)
{
Expand All @@ -41,28 +41,10 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameC
*/
public function create(string $resourceClass, array $options = []): PropertyNameCollection
{
$localCacheKey = serialize([$resourceClass, $options]);
if (isset($this->localCache[$localCacheKey])) {
return $this->localCache[$localCacheKey];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($localCacheKey);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
} catch (CacheException $e) {
return $this->localCache[$localCacheKey] = $this->decorated->create($resourceClass, $options);
}

if ($cacheItem->isHit()) {
return $this->localCache[$localCacheKey] = $cacheItem->get();
}

$propertyNameCollection = $this->decorated->create($resourceClass, $options);

$cacheItem->set($propertyNameCollection);
$this->cacheItemPool->save($cacheItem);
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $options]));

return $this->localCache[$localCacheKey] = $propertyNameCollection;
return $this->getCached($cacheKey, function () use ($resourceClass, $options) {
return $this->decorated->create($resourceClass, $options);
});
}
}
29 changes: 6 additions & 23 deletions src/Metadata/Resource/Factory/CachedResourceMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\Core\Metadata\Resource\Factory;

use ApiPlatform\Core\Cache\CachedTrait;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

/**
Expand All @@ -24,11 +24,11 @@
*/
final class CachedResourceMetadataFactory implements ResourceMetadataFactoryInterface
{
use CachedTrait;

const CACHE_KEY_PREFIX = 'resource_metadata_';

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

public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceMetadataFactoryInterface $decorated)
{
Expand All @@ -41,27 +41,10 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceMetad
*/
public function create(string $resourceClass): ResourceMetadata
{
if (isset($this->localCache[$resourceClass])) {
return $this->localCache[$resourceClass];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
} catch (CacheException $e) {
return $this->localCache[$resourceClass] = $this->decorated->create($resourceClass);
}

if ($cacheItem->isHit()) {
return $this->localCache[$resourceClass] = $cacheItem->get();
}

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

$cacheItem->set($resourceMetadata);
$this->cacheItemPool->save($cacheItem);

return $this->localCache[$resourceClass] = $resourceMetadata;
return $this->getCached($cacheKey, function () use ($resourceClass) {
return $this->decorated->create($resourceClass);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\Core\Metadata\Resource\Factory;

use ApiPlatform\Core\Cache\CachedTrait;
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use Psr\Cache\CacheException;
use Psr\Cache\CacheItemPoolInterface;

/**
Expand All @@ -24,11 +24,11 @@
*/
final class CachedResourceNameCollectionFactory implements ResourceNameCollectionFactoryInterface
{
use CachedTrait;

const CACHE_KEY = 'resource_name_collection';

private $cacheItemPool;
private $decorated;
private $localCache;

public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceNameCollectionFactoryInterface $decorated)
{
Expand All @@ -41,25 +41,8 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceNameC
*/
public function create(): ResourceNameCollection
{
if (null !== $this->localCache) {
return $this->localCache;
}

try {
$cacheItem = $this->cacheItemPool->getItem(self::CACHE_KEY);
} catch (CacheException $e) {
return $this->localCache = $this->decorated->create();
}

if ($cacheItem->isHit()) {
return $this->localCache = $cacheItem->get();
}

$resourceNameCollection = $this->decorated->create();

$cacheItem->set($resourceNameCollection);
$this->cacheItemPool->save($cacheItem);

return $this->localCache = $resourceNameCollection;
return $this->getCached(self::CACHE_KEY, function () {
return $this->decorated->create();
});
}
}
Loading

0 comments on commit dcdea86

Please sign in to comment.