Skip to content

Commit

Permalink
refactor: add ResponseCache::$ttl and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 3, 2023
1 parent 5a950f0 commit 0da0753
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
28 changes: 24 additions & 4 deletions system/Cache/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class ResponseCache
*/
protected $cacheQueryString = false;

/**
* Cache time to live.
*
* @var int seconds
*/
protected int $ttl = 0;

protected CacheInterface $cache;

public function __construct(CacheConfig $config, CacheInterface $cache)
Expand All @@ -45,6 +52,21 @@ public function __construct(CacheConfig $config, CacheInterface $cache)
$this->cache = $cache;
}

public function getTtl(): int
{
return $this->ttl;
}

/**
* @return $this
*/
public function setTtl(int $ttl)
{
$this->ttl = $ttl;

return $this;
}

/**
* Generates the cache key to use from the current request.
*
Expand All @@ -71,10 +93,8 @@ public function generateCacheKey($request): string
* Caches the full response from the current request.
*
* @param CLIRequest|IncomingRequest $request
*
* @params int $ttl time to live in seconds.
*/
public function make($request, ResponseInterface $response, int $ttl): bool
public function make($request, ResponseInterface $response): bool
{
$headers = [];

Expand All @@ -85,7 +105,7 @@ public function make($request, ResponseInterface $response, int $ttl): bool
return $this->cache->save(
$this->generateCacheKey($request),
serialize(['headers' => $headers, 'output' => $response->getBody()]),
$ttl
$this->ttl
);
}

Expand Down
10 changes: 7 additions & 3 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class CodeIgniter
* Cache expiration time
*
* @var int seconds
*
* @deprecated 4.4.0 Moved to ResponseCache::$ttl. No longer used.
*/
protected static $cacheTTL = 0;

Expand Down Expand Up @@ -338,7 +340,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
);
}

static::$cacheTTL = 0;
$this->pageCache->setTtl(0);
$this->bufferLevel = ob_get_level();

$this->startBenchmark();
Expand Down Expand Up @@ -525,8 +527,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
// Cache it without the performance metrics replaced
// so that we can have live speed updates along the way.
// Must be run after filters to preserve the Response headers.
if (static::$cacheTTL > 0) {
$this->pageCache->make($this->request, $this->response, static::$cacheTTL);
if ($this->pageCache->getTtl() > 0) {
$this->pageCache->make($this->request, $this->response);
}

// Update the performance metrics
Expand Down Expand Up @@ -699,6 +701,8 @@ public function displayCache(Cache $config)

/**
* Tells the app that the final output should be cached.
*
* @deprecated 4.4.0 Moved to ResponseCache::setTtl(). to No longer used.
*/
public static function cache(int $time)
{
Expand Down
7 changes: 4 additions & 3 deletions system/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ protected function forceHTTPS(int $duration = 31_536_000)
}

/**
* Provides a simple way to tie into the main CodeIgniter class and
* tell it how long to cache the current page for.
* How long to cache the current page for.
*
* @params int $time time to live in seconds.
*/
protected function cachePage(int $time)
{
CodeIgniter::cache($time);
Services::responsecache()->setTtl($time);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/system/Cache/ResponseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function createResponseCache(?CacheConfig $cacheConfig = null): Response

$cacheConfig ??= new CacheConfig();

return new ResponseCache($cacheConfig, $cache);
return (new ResponseCache($cacheConfig, $cache))->setTtl(300);
}

public function testCachePageIncomingRequest()
Expand All @@ -101,7 +101,7 @@ public function testCachePageIncomingRequest()
$response->setHeader('ETag', 'abcd1234');
$response->setBody('The response body.');

$return = $pageCache->make($request, $response, 300);
$return = $pageCache->make($request, $response);

$this->assertTrue($return);

Expand Down Expand Up @@ -141,7 +141,7 @@ public function testCachePageIncomingRequestWithCacheQueryString()
$response->setHeader('ETag', 'abcd1234');
$response->setBody('The response body.');

$return = $pageCache->make($request, $response, 300);
$return = $pageCache->make($request, $response);

$this->assertTrue($return);

Expand Down Expand Up @@ -176,7 +176,7 @@ public function testCachePageCLIRequest()
$response = new Response($this->appConfig);
$response->setBody('The response body.');

$return = $pageCache->make($request, $response, 300);
$return = $pageCache->make($request, $response);

$this->assertTrue($return);

Expand Down Expand Up @@ -210,7 +210,7 @@ public function testUnserializeError()
$response->setHeader('ETag', 'abcd1234');
$response->setBody('The response body.');

$pageCache->make($request, $response, 300);
$pageCache->make($request, $response);

$cacheKey = $pageCache->generateCacheKey($request);

Expand All @@ -236,7 +236,7 @@ public function testInvalidCacheError()
$response->setHeader('ETag', 'abcd1234');
$response->setBody('The response body.');

$pageCache->make($request, $response, 300);
$pageCache->make($request, $response);

$cacheKey = $pageCache->generateCacheKey($request);

Expand Down

0 comments on commit 0da0753

Please sign in to comment.