Skip to content

Commit 0da0753

Browse files
committed
refactor: add ResponseCache::$ttl and use it
1 parent 5a950f0 commit 0da0753

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

system/Cache/ResponseCache.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class ResponseCache
3737
*/
3838
protected $cacheQueryString = false;
3939

40+
/**
41+
* Cache time to live.
42+
*
43+
* @var int seconds
44+
*/
45+
protected int $ttl = 0;
46+
4047
protected CacheInterface $cache;
4148

4249
public function __construct(CacheConfig $config, CacheInterface $cache)
@@ -45,6 +52,21 @@ public function __construct(CacheConfig $config, CacheInterface $cache)
4552
$this->cache = $cache;
4653
}
4754

55+
public function getTtl(): int
56+
{
57+
return $this->ttl;
58+
}
59+
60+
/**
61+
* @return $this
62+
*/
63+
public function setTtl(int $ttl)
64+
{
65+
$this->ttl = $ttl;
66+
67+
return $this;
68+
}
69+
4870
/**
4971
* Generates the cache key to use from the current request.
5072
*
@@ -71,10 +93,8 @@ public function generateCacheKey($request): string
7193
* Caches the full response from the current request.
7294
*
7395
* @param CLIRequest|IncomingRequest $request
74-
*
75-
* @params int $ttl time to live in seconds.
7696
*/
77-
public function make($request, ResponseInterface $response, int $ttl): bool
97+
public function make($request, ResponseInterface $response): bool
7898
{
7999
$headers = [];
80100

@@ -85,7 +105,7 @@ public function make($request, ResponseInterface $response, int $ttl): bool
85105
return $this->cache->save(
86106
$this->generateCacheKey($request),
87107
serialize(['headers' => $headers, 'output' => $response->getBody()]),
88-
$ttl
108+
$this->ttl
89109
);
90110
}
91111

system/CodeIgniter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class CodeIgniter
128128
* Cache expiration time
129129
*
130130
* @var int seconds
131+
*
132+
* @deprecated 4.4.0 Moved to ResponseCache::$ttl. No longer used.
131133
*/
132134
protected static $cacheTTL = 0;
133135

@@ -338,7 +340,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
338340
);
339341
}
340342

341-
static::$cacheTTL = 0;
343+
$this->pageCache->setTtl(0);
342344
$this->bufferLevel = ob_get_level();
343345

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

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

700702
/**
701703
* Tells the app that the final output should be cached.
704+
*
705+
* @deprecated 4.4.0 Moved to ResponseCache::setTtl(). to No longer used.
702706
*/
703707
public static function cache(int $time)
704708
{

system/Controller.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ protected function forceHTTPS(int $duration = 31_536_000)
104104
}
105105

106106
/**
107-
* Provides a simple way to tie into the main CodeIgniter class and
108-
* tell it how long to cache the current page for.
107+
* How long to cache the current page for.
108+
*
109+
* @params int $time time to live in seconds.
109110
*/
110111
protected function cachePage(int $time)
111112
{
112-
CodeIgniter::cache($time);
113+
Services::responsecache()->setTtl($time);
113114
}
114115

115116
/**

tests/system/Cache/ResponseCacheTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private function createResponseCache(?CacheConfig $cacheConfig = null): Response
8888

8989
$cacheConfig ??= new CacheConfig();
9090

91-
return new ResponseCache($cacheConfig, $cache);
91+
return (new ResponseCache($cacheConfig, $cache))->setTtl(300);
9292
}
9393

9494
public function testCachePageIncomingRequest()
@@ -101,7 +101,7 @@ public function testCachePageIncomingRequest()
101101
$response->setHeader('ETag', 'abcd1234');
102102
$response->setBody('The response body.');
103103

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

106106
$this->assertTrue($return);
107107

@@ -141,7 +141,7 @@ public function testCachePageIncomingRequestWithCacheQueryString()
141141
$response->setHeader('ETag', 'abcd1234');
142142
$response->setBody('The response body.');
143143

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

146146
$this->assertTrue($return);
147147

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

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

181181
$this->assertTrue($return);
182182

@@ -210,7 +210,7 @@ public function testUnserializeError()
210210
$response->setHeader('ETag', 'abcd1234');
211211
$response->setBody('The response body.');
212212

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

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

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

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

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

0 commit comments

Comments
 (0)