1010use Neos \Cache \Frontend \StringFrontend ;
1111use Neos \Flow \Annotations as Flow ;
1212use Neos \Flow \ObjectManagement \ObjectManagerInterface ;
13+ use Neos \Flow \Package \Exception \UnknownPackageException ;
1314use Neos \Flow \Package \PackageManager ;
1415use Neos \Fusion \Core \Cache \ContentCache ;
1516
@@ -37,8 +38,10 @@ class RedisContentCacheReader
3738 */
3839 protected $ applicationIdentifier ;
3940
40- public function tryToExtractRenderingForEnumeratedNodeFromContentCache (DocumentNodeCacheKey $ documentNodeCacheKey ): RenderedDocumentFromContentCache
41- {
41+ protected $ redis ;
42+
43+ public function tryToExtractRenderingForEnumeratedNodeFromContentCache (DocumentNodeCacheKey $ documentNodeCacheKey
44+ ): RenderedDocumentFromContentCache {
4245 $ maxNestLevel = ContentCache::MAXIMUM_NESTING_LEVEL ;
4346 $ contentCacheStartToken = ContentCache::CACHE_SEGMENT_START_TOKEN ;
4447 $ contentCacheEndToken = ContentCache::CACHE_SEGMENT_END_TOKEN ;
@@ -48,26 +51,13 @@ public function tryToExtractRenderingForEnumeratedNodeFromContentCache(DocumentN
4851 */
4952 $ identifierPrefix = md5 ($ this ->applicationIdentifier ) . ': ' ;
5053
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 ();
5555
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- }
6856 $ serializedCacheValues = $ redis ->get ($ documentNodeCacheKey ->fullyQualifiedRedisKeyName ($ identifierPrefix ));
6957 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+ );
7161 }
7262 $ documentNodeCacheValues = DocumentNodeCacheValues::fromJsonString ($ serializedCacheValues );
7363
@@ -138,4 +128,44 @@ public function tryToExtractRenderingForEnumeratedNodeFromContentCache(DocumentN
138128 }
139129 return RenderedDocumentFromContentCache::createWithFullContent ($ content , $ documentNodeCacheValues );
140130 }
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+ }
141171}
0 commit comments