diff --git a/Classes/Middleware/RequestCacheMiddleware.php b/Classes/Middleware/RequestCacheMiddleware.php index 9b07c65..f742e07 100644 --- a/Classes/Middleware/RequestCacheMiddleware.php +++ b/Classes/Middleware/RequestCacheMiddleware.php @@ -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) { @@ -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, '')); @@ -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 */ diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 641dcfe..24836cb 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -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 !!! diff --git a/README.md b/README.md index 3cd6405..a3a0d33 100644 --- a/README.md +++ b/README.md @@ -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 !!!