Skip to content

Commit

Permalink
Respect the Cache Lifetime from the Resource Type configuration and u…
Browse files Browse the repository at this point in the history
…se "lifetime" in names
  • Loading branch information
cundd committed Feb 7, 2019
1 parent 9a2bc3c commit 68b0099
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 132 deletions.
63 changes: 30 additions & 33 deletions Classes/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class Cache implements CacheInterface
*
* @var integer
*/
private $cacheLifeTime = 0;
private $cacheLifetime;

/**
* Life time defined in the expires header
*
* @var integer
*/
private $expiresHeaderLifeTime = 0;
private $expiresHeaderLifetime;

/**
* @var ResponseFactory
Expand All @@ -56,13 +56,13 @@ public function __construct(ResponseFactoryInterface $responseFactory)

public function getCachedValueForRequest(RestRequestInterface $request): ?ResponseInterface
{
$cacheLifeTime = $this->getCacheLifeTime();
$cacheLifetime = $this->getCacheLifetime();

/*
* Use caching if the cache life time configuration is not -1, an API
* path is given and the request is a read request
*/
$useCaching = ($cacheLifeTime !== -1) && $request->getPath();
$useCaching = ($cacheLifetime !== -1) && $request->getPath();
if (!$useCaching) {
return null;
}
Expand All @@ -85,29 +85,26 @@ public function getCachedValueForRequest(RestRequestInterface $request): ?Respon
return $response
->withHeader(Header::CONTENT_TYPE, $responseData[Header::CONTENT_TYPE])
->withHeader(Header::LAST_MODIFIED, $responseData[Header::LAST_MODIFIED])
->withHeader(Header::EXPIRES, $this->getHttpDate(time() + $this->getExpiresHeaderLifeTime()))
->withHeader(Header::EXPIRES, $this->getHttpDate(time() + $this->getExpiresHeaderLifetime()))
->withHeader(Header::CUNDD_REST_CACHED, 'true');
}

public function setCachedValueForRequest(
RestRequestInterface $request,
ResponseInterface $response,
ResourceConfiguration $resourceConfiguration
) {
): void {
if (false === $this->canBeCached($request, $response)) {
return;
}

$cacheLifeTime = $resourceConfiguration->getCacheLifetime();
if ($cacheLifeTime < 0) {
$cacheLifeTime = $this->getCacheLifeTime();
}
$cacheLifetime = $this->getCacheLifetime();

/*
* Use caching if the cache life time configuration is not -1, an API
* path is given and the request is a read request
*/
$useCaching = ($cacheLifeTime !== -1) && $request->getPath();
$useCaching = ($cacheLifetime !== -1) && $request->getPath();
if (!$useCaching) {
return;
}
Expand All @@ -123,7 +120,7 @@ public function setCachedValueForRequest(
Header::LAST_MODIFIED => $this->getHttpDate(time()),
],
$this->getTags($request),
$cacheLifeTime
$cacheLifetime
);
}

Expand All @@ -147,12 +144,12 @@ public function getCacheKeyForRequest(RestRequestInterface $request): string
/**
* Sets the cache life time
*
* @param int $cacheLifeTime
* @param int $cacheLifetime
* @return $this
*/
public function setCacheLifeTime(int $cacheLifeTime): CacheInterface
public function setCacheLifetime(int $cacheLifetime): CacheInterface
{
$this->cacheLifeTime = $cacheLifeTime;
$this->cacheLifetime = $cacheLifetime;

return $this;
}
Expand All @@ -162,20 +159,20 @@ public function setCacheLifeTime(int $cacheLifeTime): CacheInterface
*
* @return int
*/
public function getCacheLifeTime(): int
public function getCacheLifetime(): int
{
return $this->cacheLifeTime;
return $this->cacheLifetime;
}

/**
* Sets the life time defined in the expires header
*
* @param int $expiresHeaderLifeTime
* @param int $expiresHeaderLifetime
* @return $this
*/
public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): CacheInterface
public function setExpiresHeaderLifetime(int $expiresHeaderLifetime): CacheInterface
{
$this->expiresHeaderLifeTime = $expiresHeaderLifeTime;
$this->expiresHeaderLifetime = $expiresHeaderLifetime;

return $this;
}
Expand All @@ -185,9 +182,20 @@ public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): CacheInter
*
* @return int
*/
public function getExpiresHeaderLifeTime(): int
public function getExpiresHeaderLifetime(): int
{
return $this->expiresHeaderLifetime;
}

/**
* Sets the concrete Cache instance
*
* @param \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $cacheInstance
* @internal
*/
public function setCacheInstance($cacheInstance)
{
return $this->expiresHeaderLifeTime;
$this->cacheInstance = $cacheInstance;
}

/**
Expand Down Expand Up @@ -217,17 +225,6 @@ private function getCacheInstance()
return $this->cacheInstance;
}

/**
* Sets the concrete Cache instance
*
* @param \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $cacheInstance
* @internal
*/
public function setCacheInstance($cacheInstance)
{
$this->cacheInstance = $cacheInstance;
}

/**
* Clears the cache for the current request
*
Expand Down
66 changes: 47 additions & 19 deletions Classes/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,45 @@
namespace Cundd\Rest\Cache;

use Cundd\Rest\Configuration\ConfigurationProviderInterface;
use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\ObjectManager;

class CacheFactory
{
/**
* Return a Cache instance for the Resource Type
*
* @param ResourceType $resourceType
* @param ConfigurationProviderInterface $configurationProvider
* @param ObjectManager $objectManager
* @return CacheInterface
*/
public function buildCache(
ResourceType $resourceType,
ConfigurationProviderInterface $configurationProvider,
ObjectManager $objectManager
): CacheInterface {
$cacheInstance = $this->getCacheInstance($configurationProvider, $objectManager);

if ($cacheInstance instanceof CacheInterface) {
$cacheInstance->setCacheLifeTime($this->getCacheLifeTime($configurationProvider));
$cacheInstance->setExpiresHeaderLifeTime($this->getExpiresHeaderLifeTime($configurationProvider));
}
$cacheInstance->setCacheLifetime(
$this->getCacheLifetime($configurationProvider, $resourceType)
);
$cacheInstance->setExpiresHeaderLifetime(
$this->getExpiresHeaderLifetime($configurationProvider, $resourceType)
);

return $cacheInstance;
}

/**
* @param ConfigurationProviderInterface $configurationProvider
* @param ObjectManager $objectManager
* @return mixed
* @return CacheInterface
*/
private function getCacheInstance(
ConfigurationProviderInterface $configurationProvider,
ObjectManager $objectManager
) {
): CacheInterface {
$cacheImplementation = $configurationProvider->getSetting('cacheClass');
if ($cacheImplementation && $objectManager->isRegistered($cacheImplementation)) {
return $objectManager->get($cacheImplementation);
Expand All @@ -46,28 +53,49 @@ private function getCacheInstance(

/**
* @param ConfigurationProviderInterface $configurationProvider
* @return int|mixed
* @param ResourceType $resourceType
* @return int
*/
private function getCacheLifeTime(ConfigurationProviderInterface $configurationProvider): int
private function getCacheLifetime(ConfigurationProviderInterface $configurationProvider, ResourceType $resourceType)
{
$readCacheLifeTime = $configurationProvider->getSetting('cacheLifeTime');
if ($readCacheLifeTime === null) {
$readCacheLifeTime = -1;
$resourceConfiguration = $configurationProvider->getResourceConfiguration($resourceType);
$cacheLifetime = $resourceConfiguration->getCacheLifetime();
if ($cacheLifetime > -1) {
return $cacheLifetime;
}

$cacheLifetime = $configurationProvider->getSetting('cacheLifeTime');
if ($cacheLifetime !== null) {
return (int)$cacheLifetime;
}

$cacheLifetime = $configurationProvider->getSetting('cacheLifetime');
if ($cacheLifetime !== null) {
return (int)$cacheLifetime;
}

return (int)$readCacheLifeTime;
return -1;
}

/**
* @param ConfigurationProviderInterface $configurationProvider
* @return int|mixed
* @param ResourceType $resourceType
* @return int
*/
private function getExpiresHeaderLifeTime(ConfigurationProviderInterface $configurationProvider): int
{
$expiresHeaderLifeTime = $configurationProvider->getSetting('expiresHeaderLifeTime');
private function getExpiresHeaderLifetime(
ConfigurationProviderInterface $configurationProvider,
ResourceType $resourceType
) {
$expiresHeaderLifetime = $configurationProvider->getSetting('expiresHeaderLifetime');
if ($expiresHeaderLifetime !== null) {
return (int)$expiresHeaderLifetime;
}

$expiresHeaderLifetime = $configurationProvider->getSetting('expiresHeaderLifeTime');
if ($expiresHeaderLifetime !== null) {
return (int)$expiresHeaderLifetime;
}

return ($expiresHeaderLifeTime !== null)
? intval($expiresHeaderLifeTime)
: $this->getCacheLifeTime($configurationProvider);
return $this->getCacheLifetime($configurationProvider, $resourceType);
}
}
28 changes: 14 additions & 14 deletions Classes/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
interface CacheInterface
{
/**
* Returns the cached value for the given request or NULL if it is not defined
* Return the cached value for the given request or NULL if it is not defined
*
* @param RestRequestInterface $request
* @return ResponseInterface|null
*/
public function getCachedValueForRequest(RestRequestInterface $request): ?ResponseInterface;

/**
* Sets the cache value for the given request
* Set the cache value for the given request
*
* @param RestRequestInterface $request
* @param ResponseInterface $response
Expand All @@ -33,43 +33,43 @@ public function setCachedValueForRequest(
RestRequestInterface $request,
ResponseInterface $response,
ResourceConfiguration $resourceConfiguration
);
): void;

/**
* Returns the cache key for the given request
* Return the cache key for the given request
*
* @param RestRequestInterface $request
* @return string
*/
public function getCacheKeyForRequest(RestRequestInterface $request): string;

/**
* Sets the cache life time
* Set the cache lifetime
*
* @param int $cacheLifeTime
* @param int $cacheLifetime
* @return $this
*/
public function setCacheLifeTime(int $cacheLifeTime): self;
public function setCacheLifetime(int $cacheLifetime): self;

/**
* Returns the cache life time
* Return the cache lifetime
*
* @return int
*/
public function getCacheLifeTime(): int;
public function getCacheLifetime();

/**
* Sets the life time defined in the expires header
* Set the lifetime defined in the expires header
*
* @param int $expiresHeaderLifeTime
* @param int $expiresHeaderLifetime
* @return $this
*/
public function setExpiresHeaderLifeTime(int $expiresHeaderLifeTime): self;
public function setExpiresHeaderLifetime(int $expiresHeaderLifetime): self;

/**
* Returns the life time defined in the expires header
* Return the lifetime defined in the expires header
*
* @return int
*/
public function getExpiresHeaderLifeTime(): int;
public function getExpiresHeaderLifetime();
}
20 changes: 19 additions & 1 deletion Classes/Configuration/AbstractConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ public function getConfiguredResources(): array
}

$resourceType = new ResourceType($normalizeResourceType);
$cacheLifetime = $this->detectCacheLifetimeConfiguration($configuration);

$configurationCollection[$normalizeResourceType] = new ResourceConfiguration(
$resourceType,
$readAccess,
$writeAccess,
isset($configuration['cacheLifeTime']) ? intval($configuration['cacheLifeTime']) : -1,
$cacheLifetime,
isset($configuration['handlerClass']) ? $configuration['handlerClass'] : '',
$this->getAliasesForResourceType($resourceType)
);
Expand Down Expand Up @@ -221,4 +223,20 @@ private function preparePath(array $configuration, $path)

return [$configuration, $normalizeResourceType];
}

/**
* @param array $configuration
* @return int
*/
private function detectCacheLifetimeConfiguration(array $configuration): int
{
if (isset($configuration['cacheLifeTime']) && is_numeric($configuration['cacheLifeTime'])) {
return (int)$configuration['cacheLifeTime'];
}
if (isset($configuration['cacheLifetime']) && is_numeric($configuration['cacheLifetime'])) {
return (int)$configuration['cacheLifetime'];
}

return -1;
}
}
2 changes: 1 addition & 1 deletion Classes/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private function getCachedResponseOrCallHandler(
/** @noinspection PhpUnusedParameterInspection */
ResponseInterface $response
) {
$cache = $this->objectManager->getCache();
$cache = $this->objectManager->getCache($request->getResourceType());
$cachedResponse = $cache->getCachedValueForRequest($request);

// If a cached response exists return it
Expand Down
Loading

0 comments on commit 68b0099

Please sign in to comment.