Skip to content

Commit 725d124

Browse files
committed
TASK: Move public caching into requestCache Middleware
1 parent 3bd1180 commit 725d124

4 files changed

Lines changed: 31 additions & 72 deletions

File tree

Classes/Middleware/PublicCacheHeaderMiddleware.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

Classes/Middleware/RequestCacheMiddleware.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class RequestCacheMiddleware implements MiddlewareInterface
5252
*/
5353
protected $ignoredCookieParams;
5454

55+
/**
56+
* @var boolean
57+
* @Flow\InjectConfiguration(path="maxPublicCacheTime")
58+
*/
59+
protected $maxPublicCacheTime;
60+
5561
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
5662
{
5763
if (!$this->enabled) {
@@ -82,6 +88,23 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8288
->withoutHeader(self::HEADER_LIFTIME)
8389
->withoutHeader(self::HEADER_TAGS);
8490

91+
$publicLifetime = 0;
92+
if ($this->maxPublicCacheTime > 0) {
93+
if ($lifetime > 0 && $lifetime < $this->maxPublicCacheTime) {
94+
$publicLifetime = $lifetime;
95+
} else {
96+
$publicLifetime = $this->maxPublicCacheTime;
97+
}
98+
}
99+
100+
if ($publicLifetime > 0) {
101+
$entryContentHash = md5($response->getBody()->getContents());
102+
$response->getBody()->rewind();
103+
$response = $response
104+
->withHeader('ETag', '"' . $entryContentHash . '"')
105+
->withHeader('Cache-Control', 'public, max-age=' . $publicLifetime);
106+
}
107+
85108
$this->cacheFrontend->set($entryIdentifier,[ 'timestamp' => time(), 'response' => str($response) ], $tags, $lifetime);
86109
$response->getBody()->rewind();
87110
return $response->withHeader(self::HEADER_INFO, 'MISS: ' . $entryIdentifier);

Configuration/Settings.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@ Neos:
3838
'fullPageRequestCache':
3939
middleware: 'Flowpack\FullPageCache\Middleware\RequestCacheMiddleware'
4040
position: 'after trustedProxies'
41-
'fullPageCachePublicHeader':
42-
middleware: 'Flowpack\FullPageCache\Middleware\PublicCacheHeaderMiddleware'
43-
position: 'after fullPageRequestCache 20'
4441
'fullPageCacheFusionAutoconfiguration':
4542
middleware: 'Flowpack\FullPageCache\Middleware\FusionAutoconfigurationMiddleware'
46-
position: 'after fullPageRequestCache 10'
43+
position: 'after fullPageRequestCache'
4744
Neos:
4845
fusion:
4946
autoInclude:

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,18 @@ You can also move the cache backend to something faster if available, to improve
5454
How it works
5555
------------
5656

57-
The package defines three http middlewares:
57+
The package defines two http middlewares:
5858

59-
- `fullPageRequestCache`: If a request is cacheble the cache is asked first and only if no response is found the
59+
- `RequestCacheMiddleware`: If a request is cacheable the cache is asked first and only if no response is found the
6060
request is passed down the middleware chain. The cache lifetime and tags are determined from the
6161
`X-FullPageCache-Enabled`, `X-FullPageCache-Lifetime` and `X-FullPageCache-Tags` that are set by upstream middlewares
62-
or controllers.
62+
or controllers. Additionally the middleware adds `ETag` and `CacheControl` Headers taking the lifetime and setting
63+
`maxPublicCacheTime` into account.
6364

64-
- `fullPageCachePublicHeader` : Set `ETag` and `CacheControl` Headers based on the `X-FullPageCache-Enabled` and the
65-
`X-FullPageCache-Lifetime` headers taking the `maxPublicCacheTime` into account.
66-
67-
- `fullPageCacheFusionAutoconfiguration`: Connects to the fusion cache and extracts tags plus the allowed lifetime which is then
65+
- `FusionAutoconfigurationMiddleware`: Connects to the fusion cache and extracts tags plus the allowed lifetime which is then
6866
stored in the response headers `X-FullPageCache-Enabled`, `X-FullPageCache-Lifetime` and `X-FullPageCache-Tags`.
69-
This component expects a header `X-FullPageCache-EnableFusionAutoconfiguration` which is set automatically for `Neos.Neos:Page`.
67+
This component is only active if the header `X-FullPageCache-EnableFusionAutoconfiguration` is present in the response
68+
which is set automatically for `Neos.Neos:Page`.
7069

7170
Custom controllers that want to control the caching behavior directly can set the headers `X-FullPageCache-Enabled`,
7271
`X-FullPageCache-Lifetime` and `X-FullPageCache-Tags` directly while fusion based controllers can enable the autoconfiguration

0 commit comments

Comments
 (0)