11<?php
22namespace Flowpack \FullPageCache \Http ;
33
4- use Flowpack \FullPageCache \Aspects \ContentCacheAspect ;
5- use Flowpack \FullPageCache \Cache \MetadataAwareStringFrontend ;
64use Neos \Cache \Frontend \StringFrontend ;
75use Neos \Flow \Annotations as Flow ;
86use Neos \Flow \Http \Component \ComponentContext ;
@@ -20,95 +18,63 @@ class RequestStorageComponent implements ComponentInterface
2018 */
2119 protected $ cacheFrontend ;
2220
23- /**
24- * @Flow\Inject
25- * @var MetadataAwareStringFrontend
26- */
27- protected $ contentCache ;
28-
2921 /**
3022 * @var boolean
3123 * @Flow\InjectConfiguration(path="enabled")
3224 */
3325 protected $ enabled ;
3426
3527 /**
36- * @Flow\Inject
37- * @var ContentCacheAspect
28+ * @var boolean
29+ * @Flow\InjectConfiguration(path="maxPublicCacheTime")
3830 */
39- protected $ contentCacheAspect ;
31+ protected $ maxPublicCacheTime ;
4032
4133 /**
4234 * @inheritDoc
4335 */
4436 public function handle (ComponentContext $ componentContext )
4537 {
46- if (!$ this ->enabled ) {
47- return ;
48- }
49-
5038 $ request = $ componentContext ->getHttpRequest ();
51- if (strtoupper ($ request ->getMethod ()) !== 'GET ' ) {
52- return ;
53- }
54-
55- if (!empty ($ request ->getUri ()->getQuery ())) {
56- return ;
57- }
58-
5939 $ response = $ componentContext ->getHttpResponse ();
6040
6141 if ($ response ->hasHeader ('X-From-FullPageCache ' )) {
6242 return ;
6343 }
6444
65- if ($ this -> contentCacheAspect -> hasUncachedSegments ())
66- {
67- return ;
68- }
45+ if ($ response -> hasHeader ( ' X-CacheLifetime ' )) {
46+ $ lifetime = ( int ) $ response -> getHeaderLine ( ' X-CacheLifetime ' );
47+ $ cacheTags = $ response -> getHeader ( ' X-CacheTags ' ) ;
48+ $ entryIdentifier = md5 (( string ) $ request -> getUri ());
6949
70- if ($ response ->hasHeader ('Set-Cookie ' )) {
71- return ;
72- }
50+ $ publicLifetime = 0 ;
51+ if ($ this ->maxPublicCacheTime > 0 ) {
52+ if ($ lifetime > 0 && $ lifetime < $ this ->maxPublicCacheTime ) {
53+ $ publicLifetime = $ lifetime ;
54+ } else {
55+ $ publicLifetime = $ this ->maxPublicCacheTime ;
56+ }
57+ }
7358
74- $ entryIdentifier = md5 ((string )$ request ->getUri ());
59+ $ modifiedResponse = $ response
60+ ->withoutHeader ('X-CacheTags ' )
61+ ->withoutHeader ('X-CacheLifetime ' );
7562
76- [$ tags , $ lifetime ] = $ this ->getCacheTagsAndLifetime ();
63+ if ($ publicLifetime > 0 ) {
64+ $ entryContentHash = md5 (str ($ response ));
65+ $ modifiedResponse = $ modifiedResponse
66+ ->withAddedHeader ('ETag ' , $ entryContentHash )
67+ ->withAddedHeader ('CacheControl ' , 'max-age= ' . $ publicLifetime );
68+ }
7769
78- if (empty ($ tags )) {
79- // For now do not cache something without tags (maybe it was not a Neos page)
80- return ;
81- }
70+ $ modifiedResponseforStorage = $ modifiedResponse
71+ ->withHeader ('X-Storage-Timestamp ' , time ())
72+ ->withHeader ('X-Storage-Lifetime ' , $ lifetime );
8273
83- $ modifiedResponse = $ response ->withHeader ('X-Storage-Component ' , $ entryIdentifier );
84- $ this ->cacheFrontend ->set ($ entryIdentifier , str ($ modifiedResponse ), $ tags , $ lifetime );
85- // TODO: because stream is copied ot the modifiedResponse we would get empty output on first request
86- $ response ->getBody ()->rewind ();
87- }
74+ $ this ->cacheFrontend ->set ($ entryIdentifier , str ($ modifiedResponseforStorage ), $ cacheTags , $ lifetime );
8875
89- /**
90- * Get cache tags and lifetime from the cache metadata that was extracted by the special cache frontend for content cache
91- *
92- * @return array with first "tags" and then "lifetime"
93- */
94- protected function getCacheTagsAndLifetime (): array
95- {
96- $ lifetime = null ;
97- $ tags = [];
98- $ entriesMetadata = $ this ->contentCache ->getAllMetadata ();
99- foreach ($ entriesMetadata as $ identifier => $ metadata ) {
100- $ entryTags = isset ($ metadata ['tags ' ]) ? $ metadata ['tags ' ] : [];
101- $ entryLifetime = isset ($ metadata ['lifetime ' ]) ? $ metadata ['lifetime ' ] : null ;
102- if ($ entryLifetime !== null ) {
103- if ($ lifetime === null ) {
104- $ lifetime = $ entryLifetime ;
105- } else {
106- $ lifetime = min ($ lifetime , $ entryLifetime );
107- }
108- }
109- $ tags = array_unique (array_merge ($ tags , $ entryTags ));
76+ $ modifiedResponse ->getBody ()->rewind ();
77+ $ componentContext ->replaceHttpResponse ($ modifiedResponse );
11078 }
111-
112- return [$ tags , $ lifetime ];
11379 }
11480}
0 commit comments