10
10
use Neos \Cache \Frontend \StringFrontend ;
11
11
use Neos \Flow \Annotations as Flow ;
12
12
use Neos \Flow \ObjectManagement \ObjectManagerInterface ;
13
+ use Neos \Flow \Package \Exception \UnknownPackageException ;
13
14
use Neos \Flow \Package \PackageManager ;
14
15
use Neos \Fusion \Core \Cache \ContentCache ;
15
16
@@ -37,8 +38,10 @@ class RedisContentCacheReader
37
38
*/
38
39
protected $ applicationIdentifier ;
39
40
40
- public function tryToExtractRenderingForEnumeratedNodeFromContentCache (DocumentNodeCacheKey $ documentNodeCacheKey ): RenderedDocumentFromContentCache
41
- {
41
+ protected $ redis ;
42
+
43
+ public function tryToExtractRenderingForEnumeratedNodeFromContentCache (DocumentNodeCacheKey $ documentNodeCacheKey
44
+ ): RenderedDocumentFromContentCache {
42
45
$ maxNestLevel = ContentCache::MAXIMUM_NESTING_LEVEL ;
43
46
$ contentCacheStartToken = ContentCache::CACHE_SEGMENT_START_TOKEN ;
44
47
$ contentCacheEndToken = ContentCache::CACHE_SEGMENT_END_TOKEN ;
@@ -48,26 +51,13 @@ public function tryToExtractRenderingForEnumeratedNodeFromContentCache(DocumentN
48
51
*/
49
52
$ identifierPrefix = md5 ($ this ->applicationIdentifier ) . ': ' ;
50
53
51
- $ packageManager = $ this ->objectManager ->get (PackageManager::class);
52
- $ flowPackage = $ packageManager ->getPackage ('Neos.Flow ' );
53
- preg_match ('/^(\d+\.\d+)/ ' , $ flowPackage ->getInstalledVersion (), $ versionMatches );
54
- $ flowMajorVersion = (int )($ versionMatches [1 ] ?? '0 ' );
54
+ $ redis = $ this ->getRedis ();
55
55
56
- $ backend = $ this ->contentCache ->getBackend ();
57
- if ($ flowMajorVersion >= 8 && $ backend instanceof RedisBackend) {
58
- $ reflProp = new \ReflectionProperty (RedisBackend::class, 'redis ' );
59
- $ reflProp ->setAccessible (true );
60
- $ redis = $ reflProp ->getValue ($ backend );
61
- } elseif (get_class ($ backend ) === 'Sandstorm\OptimizedRedisCacheBackend\OptimizedRedisCacheBackend ' ) {
62
- $ reflProp = new \ReflectionProperty (\Sandstorm \OptimizedRedisCacheBackend \OptimizedRedisCacheBackend::class, 'redis ' );
63
- $ reflProp ->setAccessible (true );
64
- $ redis = $ reflProp ->getValue ($ backend );
65
- } else {
66
- throw new \RuntimeException ('The cache backend for "Neos_Fusion_Content" must be an OptimizedRedisCacheBackend, but is ' . get_class ($ backend ), 1622570000 );
67
- }
68
56
$ serializedCacheValues = $ redis ->get ($ documentNodeCacheKey ->fullyQualifiedRedisKeyName ($ identifierPrefix ));
69
57
if ($ serializedCacheValues === false ) {
70
- return RenderedDocumentFromContentCache::createIncomplete ('No Redis Key " ' . $ documentNodeCacheKey ->redisKeyName () . '" found. ' );
58
+ return RenderedDocumentFromContentCache::createIncomplete (
59
+ 'No Redis Key " ' . $ documentNodeCacheKey ->redisKeyName () . '" found. '
60
+ );
71
61
}
72
62
$ documentNodeCacheValues = DocumentNodeCacheValues::fromJsonString ($ serializedCacheValues );
73
63
@@ -138,4 +128,44 @@ public function tryToExtractRenderingForEnumeratedNodeFromContentCache(DocumentN
138
128
}
139
129
return RenderedDocumentFromContentCache::createWithFullContent ($ content , $ documentNodeCacheValues );
140
130
}
131
+
132
+ /**
133
+ * @throws UnknownPackageException
134
+ */
135
+ protected function getRedis ()
136
+ {
137
+ if ($ this ->redis ) {
138
+ return $ this ->redis ;
139
+ }
140
+
141
+ $ packageManager = $ this ->objectManager ->get (PackageManager::class);
142
+ $ flowPackage = $ packageManager ->getPackage ('Neos.Flow ' );
143
+ preg_match ('/^(\d+\.\d+)/ ' , $ flowPackage ->getInstalledVersion (), $ versionMatches );
144
+ $ flowMajorVersion = (int )($ versionMatches [1 ] ?? '0 ' );
145
+
146
+ $ backend = $ this ->contentCache ->getBackend ();
147
+
148
+ if ($ flowMajorVersion >= 8 && $ backend instanceof RedisBackend) {
149
+ $ reflProp = new \ReflectionProperty (RedisBackend::class, 'redis ' );
150
+ $ reflProp ->setAccessible (true );
151
+ $ this ->redis = $ reflProp ->getValue ($ backend );
152
+ return $ this ->redis ;
153
+ }
154
+
155
+ if (get_class ($ backend ) === 'Sandstorm\OptimizedRedisCacheBackend\OptimizedRedisCacheBackend ' ) {
156
+ $ reflProp = new \ReflectionProperty (
157
+ \Sandstorm \OptimizedRedisCacheBackend \OptimizedRedisCacheBackend::class,
158
+ 'redis '
159
+ );
160
+ $ reflProp ->setAccessible (true );
161
+ $ this ->redis = $ reflProp ->getValue ($ backend );
162
+ return $ this ->redis ;
163
+ }
164
+
165
+ throw new \RuntimeException (
166
+ 'The cache backend for "Neos_Fusion_Content" must be an OptimizedRedisCacheBackend, but is ' . get_class (
167
+ $ backend
168
+ ), 1622570000
169
+ );
170
+ }
141
171
}
0 commit comments