11
11
* of the License, or any later version.
12
12
*/
13
13
14
- use TYPO3 \ CMS \ Core \ Cache \ CacheManager ;
14
+ use Psr \ Http \ Message \ ServerRequestInterface ;
15
15
use TYPO3 \CMS \Core \Cache \Frontend \FrontendInterface ;
16
16
use TYPO3 \CMS \Core \Context \Context ;
17
17
use TYPO3 \CMS \Core \Context \Exception \AspectNotFoundException ;
18
18
use TYPO3 \CMS \Core \Information \Typo3Version ;
19
19
use TYPO3 \CMS \Core \SingletonInterface ;
20
+ use TYPO3 \CMS \Core \TypoScript \FrontendTypoScript ;
20
21
use TYPO3 \CMS \Core \Utility \GeneralUtility ;
22
+ use TYPO3 \CMS \Frontend \Cache \CacheLifetimeCalculator ;
21
23
use TYPO3 \CMS \Frontend \Controller \TypoScriptFrontendController ;
22
24
23
25
/**
28
30
*/
29
31
class CacheHelper implements SingletonInterface
30
32
{
31
- /**
32
- * @var FrontendInterface
33
- */
34
- protected $ cache ;
35
-
36
- protected $ disableCaching = false ;
33
+ protected FrontendInterface $ cache ;
34
+ protected bool $ disableCaching = false ;
35
+ protected Context $ context ;
37
36
38
- public function __construct (FrontendInterface $ cache = null , Context $ context = null )
37
+ public function __construct (FrontendInterface $ cache , Context $ context )
39
38
{
40
- if ((new Typo3Version ())->getMajorVersion () < 10 ) {
41
- $ this ->cache = $ cache ?? GeneralUtility::makeInstance (CacheManager::class)->getCache ('cache_hash ' );
42
- } else {
43
- $ this ->cache = $ cache ?? GeneralUtility::makeInstance (CacheManager::class)->getCache ('hash ' );
44
- }
45
- if ($ context === null ) {
46
- $ context = GeneralUtility::makeInstance (Context::class);
47
- }
39
+ $ this ->context = $ context ;
40
+ $ this ->cache = $ cache ;
48
41
try {
49
42
$ this ->disableCaching = $ context ->getPropertyFromAspect ('workspace ' , 'id ' , 0 ) > 0 ;
50
43
} catch (AspectNotFoundException $ e ) {
@@ -60,10 +53,6 @@ public function __construct(FrontendInterface $cache = null, Context $context =
60
53
/**
61
54
* Looks up the items inside the cache, if it exists, takes the cached entry, otherwise computes the data
62
55
* via the $loader().
63
- *
64
- * @param string $cacheIdentifier
65
- * @param callable $loader
66
- * @return array
67
56
*/
68
57
public function get (string $ cacheIdentifier , callable $ loader ): array
69
58
{
@@ -81,7 +70,8 @@ public function get(string $cacheIdentifier, callable $loader): array
81
70
82
71
// Calculate tags + lifetime
83
72
$ tags = $ this ->buildTagsAndAddThemToPageCache ($ pages );
84
- $ maximumLifeTime = $ this ->getMaxLifetimeOfPages ($ pages , $ this ->getFrontendController ()->get_cache_timeout ());
73
+ $ defaultMaxLifeTime = $ this ->getDefaultMaxLifeTime ();
74
+ $ maximumLifeTime = $ this ->getMaxLifetimeOfPages ($ pages , $ defaultMaxLifeTime );
85
75
$ this ->cache ->set ($ cacheIdentifier , $ pages , $ tags , $ maximumLifeTime );
86
76
return $ pages ;
87
77
}
@@ -126,15 +116,58 @@ protected function getAllPageIdsFromItems(array $pages): array
126
116
return $ pageIds ;
127
117
}
128
118
119
+ protected function getDefaultMaxLifeTime (): int
120
+ {
121
+ if (GeneralUtility::makeInstance (Typo3Version::class)->getMajorVersion () < 13 ) {
122
+ $ maxLifetime = (int )$ this ->getFrontendController ()->get_cache_timeout ();
123
+ } else {
124
+ $ request = $ this ->getServerRequest ();
125
+ $ pageInformation = $ request ->getAttribute ('frontend.page.information ' );
126
+ /** @var ?FrontendTypoScript $typoScript */
127
+ $ typoScript = $ request ->getAttribute ('frontend.typoscript ' );
128
+ if ($ typoScript === null || $ pageInformation === null ) {
129
+ return 0 ;
130
+ }
131
+ $ typoScriptConfigArray = $ typoScript ->getConfigArray ();
132
+ $ maxLifetime = GeneralUtility::makeInstance (CacheLifetimeCalculator::class)
133
+ ->calculateLifetimeForPage (
134
+ $ pageInformation ->getId (),
135
+ $ pageInformation ->getPageRecord (),
136
+ $ typoScriptConfigArray ,
137
+ 0 ,
138
+ $ this ->context
139
+ );
140
+ }
141
+ return $ maxLifetime ;
142
+ }
143
+
129
144
/**
130
145
* pages.cache_timeout is not used here, as this is supposed to be relevant for content of a page, not the
131
146
* metadata.
132
147
*/
133
- protected function getMaxLifetimeOfPages (array $ pages , int $ maxLifetime = null ): ? int
148
+ protected function getMaxLifetimeOfPages (array $ pages , int $ maxLifetime ): int
134
149
{
135
150
foreach ($ pages as $ page ) {
136
151
if (!empty ($ page ['endtime ' ])) {
137
- $ maxLifetimeOfPage = $ page ['endtime ' ] - $ GLOBALS ['EXEC_TIME ' ];
152
+ $ maxLifetimeOfPage = (int )$ page ['endtime ' ] - $ GLOBALS ['EXEC_TIME ' ];
153
+ if (GeneralUtility::makeInstance (Typo3Version::class)->getMajorVersion () === 13 ) {
154
+ $ request = $ this ->getServerRequest ();
155
+ /** @var ?FrontendTypoScript $typoScript */
156
+ $ typoScript = $ request ->getAttribute ('frontend.typoscript ' );
157
+ if ($ typoScript === null ) {
158
+ $ typoScriptConfigArray = [];
159
+ } else {
160
+ $ typoScriptConfigArray = $ typoScript ->getConfigArray ();
161
+ }
162
+ $ maxLifetimeOfPage = GeneralUtility::makeInstance (CacheLifetimeCalculator::class)
163
+ ->calculateLifetimeForPage (
164
+ $ page ['uid ' ],
165
+ $ page ,
166
+ $ typoScriptConfigArray ,
167
+ 0 ,
168
+ $ this ->context
169
+ );
170
+ }
138
171
if ($ maxLifetimeOfPage < $ maxLifetime ) {
139
172
$ maxLifetime = $ maxLifetimeOfPage ;
140
173
}
@@ -146,9 +179,11 @@ protected function getMaxLifetimeOfPages(array $pages, int $maxLifetime = null):
146
179
return $ maxLifetime ;
147
180
}
148
181
149
- /**
150
- * @return TypoScriptFrontendController
151
- */
182
+ protected function getServerRequest (): ServerRequestInterface
183
+ {
184
+ return $ GLOBALS ['TYPO3_REQUEST ' ];
185
+ }
186
+
152
187
protected function getFrontendController (): TypoScriptFrontendController
153
188
{
154
189
return $ GLOBALS ['TSFE ' ];
0 commit comments