forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
415 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Cache; | ||
|
||
use CodeIgniter\HTTP\CLIRequest; | ||
use CodeIgniter\HTTP\IncomingRequest; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
use Config\Cache as CacheConfig; | ||
use Exception; | ||
|
||
/** | ||
* Web Page Caching | ||
*/ | ||
class PageCache | ||
{ | ||
/** | ||
* Whether to take the URL query string into consideration when generating | ||
* output cache files. Valid options are: | ||
* | ||
* false = Disabled | ||
* true = Enabled, take all query parameters into account. | ||
* Please be aware that this may result in numerous cache | ||
* files generated for the same page over and over again. | ||
* array('q') = Enabled, but only take into account the specified list | ||
* of query parameters. | ||
* | ||
* @var bool|string[] | ||
*/ | ||
protected $cacheQueryString = false; | ||
|
||
protected CacheInterface $cache; | ||
|
||
public function __construct(CacheConfig $config, CacheInterface $cache) | ||
{ | ||
$this->cacheQueryString = $config->cacheQueryString; | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* Generates the cache key to use from the current request. | ||
* | ||
* @param CLIRequest|IncomingRequest $request | ||
* | ||
* @internal for testing purposes only | ||
*/ | ||
public function generateCacheKey($request): string | ||
{ | ||
if ($request instanceof CLIRequest) { | ||
return md5($request->getPath()); | ||
} | ||
|
||
$uri = clone $request->getUri(); | ||
|
||
$query = $this->cacheQueryString | ||
? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : []) | ||
: ''; | ||
|
||
return md5($uri->setFragment('')->setQuery($query)); | ||
} | ||
|
||
/** | ||
* Caches the full response from the current request. | ||
* | ||
* @param CLIRequest|IncomingRequest $request | ||
* | ||
* @params int $ttl time to live in seconds. | ||
*/ | ||
public function cachePage($request, ResponseInterface $response, int $ttl): bool | ||
{ | ||
$headers = []; | ||
|
||
foreach ($response->headers() as $header) { | ||
$headers[$header->getName()] = $header->getValueLine(); | ||
} | ||
|
||
return $this->cache->save( | ||
$this->generateCacheKey($request), | ||
serialize(['headers' => $headers, 'output' => $response->getBody()]), | ||
$ttl | ||
); | ||
} | ||
|
||
/** | ||
* Gets the cached response for the request. | ||
* | ||
* @param CLIRequest|IncomingRequest $request | ||
*/ | ||
public function getCachedResponse($request, ResponseInterface $response): ?ResponseInterface | ||
{ | ||
if ($cachedResponse = $this->cache->get($this->generateCacheKey($request))) { | ||
$cachedResponse = unserialize($cachedResponse); | ||
|
||
if ( | ||
! is_array($cachedResponse) | ||
|| ! isset($cachedResponse['output']) | ||
|| ! isset($cachedResponse['headers']) | ||
) { | ||
throw new Exception('Error unserializing page cache'); | ||
} | ||
|
||
$headers = $cachedResponse['headers']; | ||
$output = $cachedResponse['output']; | ||
|
||
// Clear all default headers | ||
foreach (array_keys($response->headers()) as $key) { | ||
$response->removeHeader($key); | ||
} | ||
|
||
// Set cached headers | ||
foreach ($headers as $name => $value) { | ||
$response->setHeader($name, $value); | ||
} | ||
|
||
$response->setBody($output); | ||
|
||
return $response; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.