Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions Classes/Middleware/RequestCacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class RequestCacheMiddleware implements MiddlewareInterface
*/
protected $maxSharedCacheTime;

/**
* @var bool
* @Flow\InjectConfiguration(path="publicAge")
*/
protected $publicAge;

public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
{
if (!$this->enabled) {
Expand All @@ -82,14 +88,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $next->handle($request)->withHeader(self::HEADER_INFO, 'SKIP');
}

/** @var ?CacheEntryShape $cacheEntry */
/** @var CacheEntryShape|false $cacheEntry */
$cacheEntry = $this->cacheFrontend->get($entryIdentifier);
if ($cacheEntry) {
$age = time() - $cacheEntry['timestamp'];
$response = Message::parseResponse($cacheEntry['response']);
return $response
->withHeader('Age', (string)$age)
->withHeader(self::HEADER_INFO, 'HIT: ' . $entryIdentifier);
if ($this->publicAge) {
$age = time() - $cacheEntry['timestamp'];
$response = $response->withHeader('Age', (string)$age);
}
return $response->withHeader(self::HEADER_INFO, 'HIT: ' . $entryIdentifier);
}

$response = $next->handle($request->withHeader(self::HEADER_ENABLED, ''));
Expand All @@ -111,12 +118,28 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}

if ($publicLifetime > 0) {
$sharedLifetime = 0;
if ($this->maxSharedCacheTime > 0) {
if ($lifetime > 0 && $lifetime < $this->maxSharedCacheTime) {
$sharedLifetime = $lifetime;
} else {
$sharedLifetime = $this->maxSharedCacheTime;
}
}

if ($publicLifetime > 0 || $sharedLifetime > 0) {
$cacheControlHeaderParts = ['public'];
if ($publicLifetime > 0) {
$cacheControlHeaderParts[] = 'max-age=' . $publicLifetime;
}
if ($sharedLifetime > 0) {
$cacheControlHeaderParts[] = 's-maxage=' . $sharedLifetime;
}
$entryContentHash = md5($response->getBody()->getContents());
$response->getBody()->rewind();
$response = $response
->withHeader('ETag', '"' . $entryContentHash . '"')
->withHeader('Cache-Control', 'public, max-age=' . $publicLifetime);
->withHeader('Cache-Control', implode(', ', $cacheControlHeaderParts));
}

/** @var CacheEntryShape $cacheEntry */
Expand Down
12 changes: 10 additions & 2 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ Flowpack:
# enable full page caching
enabled: true

# the maximum public cache control header sent
# set to 0 if you do not want to send public CacheControl headers
# enable adding of public age headers with the actual age of the cached record
# consider disabling this if the public max-ages or s-maxage are lower than the internally allowed ages
publicAge: true

# the maximum value for the `max-age` directive for the Cache-Control header
# set to 0 if you do not want to add the directives
maxPublicCacheTime: 86400

# the maximum value for the `s-maxage` directive for the Cache-Control header
# set to 0 if you do not want to add the directive
maxSharedCacheTime: 0

# requests have to fulfill certain conditions for beeing cached
request:
# !!! Only the http methods "GET" and "HEAD" are supported !!!
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ Flowpack:
# enable full page caching
enabled: true

# the maximum public cache control header sent
# set to 0 if you do not want to send public CacheControl headers
# enable adding of public age headers with the actual age of the cached record
# consider disabling this if the public max-ages or s-maxage are lower than the internally allowed ages
publicAge: true

# the maximum value for the `max-age` directive for the Cache-Control header
# set to 0 if you do not want to add the directives
maxPublicCacheTime: 86400

# the maximum value for the `s-maxage` directive for the Cache-Control header
# set to 0 if you do not want to add the directive
maxSharedCacheTime: 0

# requests have to fulfill certain conditions for beeing cached
request:
# !!! Only the http methods "GET" and "HEAD" are supported !!!
Expand Down
Loading