Skip to content

Commit

Permalink
Make cacheControlPreventsCaching() private and add types
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Sep 20, 2023
1 parent cbd5828 commit b5573c6
Showing 1 changed file with 19 additions and 63 deletions.
82 changes: 19 additions & 63 deletions Classes/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Cundd\Rest\DataProvider\Utility;
use Cundd\Rest\Http\Header;
use Cundd\Rest\Http\RestRequestInterface;
use Cundd\Rest\ResponseFactory;
use Cundd\Rest\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
Expand All @@ -23,35 +22,21 @@ class Cache implements CacheInterface
{
/**
* Concrete cache instance
*
* @var VariableFrontend
*/
private $cacheInstance;
private FrontendInterface $cacheInstance;

/**
* Cache life time
*
* @var integer
* Cache lifetime
*/
private $cacheLifetime;
private int $cacheLifetime;

/**
* Lifetime defined in the expires header
*
* @var integer
* Lifetime sent in the expires header
*/
private $expiresHeaderLifetime;
private int $expiresHeaderLifetime;

/**
* @var ResponseFactory
*/
private $responseFactory;
private ResponseFactoryInterface $responseFactory;

/**
* Cache constructor
*
* @param ResponseFactoryInterface $responseFactory
*/
public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
Expand All @@ -62,7 +47,7 @@ public function getCachedValueForRequest(RestRequestInterface $request): ?Respon
$cacheLifetime = $this->getCacheLifetime();

/*
* Use caching if the cache life time configuration is not -1, an API
* Use caching if the cache lifetime configuration is not -1, an API
* path is given and the request is a read request
*/
$useCaching = ($cacheLifetime !== -1) && $request->getPath();
Expand Down Expand Up @@ -110,7 +95,7 @@ public function setCachedValueForRequest(
$cacheLifetime = $this->getCacheLifetime();

/*
* Use caching if the cache life time configuration is not -1, an API
* Use caching if the cache lifetime configuration is not -1, an API
* path is given and the request is a read request
*/
$useCaching = ($cacheLifetime !== -1) && $request->getPath();
Expand All @@ -135,12 +120,6 @@ public function setCachedValueForRequest(
);
}

/**
* Returns the cache key for the given request
*
* @param RestRequestInterface $request
* @return string
*/
public function getCacheKeyForRequest(RestRequestInterface $request): string
{
$cacheKey = sha1($request->getUri() . '_' . $request->getFormat() . '_' . $request->getMethod());
Expand All @@ -152,65 +131,43 @@ public function getCacheKeyForRequest(RestRequestInterface $request): string
return $cacheKey;
}

/**
* Sets the cache life time
*
* @param int $cacheLifetime
* @return $this
*/
public function setCacheLifetime(int $cacheLifetime): CacheInterface
{
$this->cacheLifetime = $cacheLifetime;

return $this;
}

/**
* Returns the cache life time
*
* @return int
*/
public function getCacheLifetime(): int
{
return $this->cacheLifetime;
}

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

return $this;
}

/**
* Returns the life time defined in the expires header
*
* @return int
*/
public function getExpiresHeaderLifetime(): int
{
return $this->expiresHeaderLifetime;
}

/**
* Sets the concrete Cache instance
* Set the concrete Cache instance
*
* @param FrontendInterface $cacheInstance
* @internal
*/
public function setCacheInstance(FrontendInterface $cacheInstance)
public function setCacheInstance(FrontendInterface $cacheInstance): void
{
$this->cacheInstance = $cacheInstance;
}

/**
* Returns a date in the format for a HTTP header
* Return a date in the format for a HTTP header
*
* @param int $date
* @return string
Expand All @@ -221,13 +178,11 @@ private function getHttpDate(int $date): string
}

/**
* Returns the cache instance
*
* @return FrontendInterface|VariableFrontend
* Return the cache instance
*/
private function getCacheInstance()
private function getCacheInstance(): FrontendInterface
{
if (!$this->cacheInstance) {
if (!isset($this->cacheInstance)) {
/** @var CacheManager $cacheManager */
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
$this->cacheInstance = $cacheManager->getCache('cundd_rest_cache');
Expand All @@ -237,19 +192,19 @@ private function getCacheInstance()
}

/**
* Clears the cache for the current request
* Clear the cache for the current request
*
* @param RestRequestInterface $request
*/
private function clearCache(RestRequestInterface $request)
private function clearCache(RestRequestInterface $request): void
{
$allTags = $this->getTags($request);
$firstTag = $allTags[0];
$this->getCacheInstance()->flushByTag($firstTag);
}

/**
* Returns the tags for the current request
* Return the tags for the current request
*
* @param RestRequestInterface $request
* @return string[]
Expand Down Expand Up @@ -281,6 +236,7 @@ function ($tag) {
* @param RestRequestInterface $request
* @param ResponseInterface $response
* @return bool
* @internal
*/
public function canBeCached(RestRequestInterface $request, ResponseInterface $response): bool
{
Expand All @@ -304,7 +260,7 @@ public function canBeCached(RestRequestInterface $request, ResponseInterface $re
* @param ResponseInterface $response
* @return bool
*/
protected function cacheControlPreventsCaching(ResponseInterface $response): bool
private function cacheControlPreventsCaching(ResponseInterface $response): bool
{
$cacheControlHeaders = $response->getHeader(Header::CACHE_CONTROL);
$noCacheValues = [
Expand Down

0 comments on commit b5573c6

Please sign in to comment.