Neos\Fusion\Core\Cache\ContentCache::processCacheSegments PHP Method

processCacheSegments() public method

This method is called by the TypoScript Runtime while rendering a TypoScript object.
public processCacheSegments ( string $content, boolean $storeCacheEntries = true ) : string
$content string The content with an outer cache segment
$storeCacheEntries boolean Whether to store extracted cache segments in the cache
return string The (pure) content without cache segment markers
    public function processCacheSegments($content, $storeCacheEntries = true)
    {
        $this->parser->extractRenderedSegments($content, $this->randomCacheMarker);
        if ($storeCacheEntries) {
            $segments = $this->parser->getCacheSegments();
            foreach ($segments as $segment) {
                $metadata = explode(';', $segment['metadata']);
                $tagsValue = $metadata[0] === '' ? [] : ($metadata[0] === '*' ? false : explode(',', $metadata[0]));
                // FALSE means we do not need to store the cache entry again (because it was previously fetched)
                if ($tagsValue !== false) {
                    $lifetime = isset($metadata[1]) ? (int) $metadata[1] : null;
                    $this->cache->set($segment['identifier'], $segment['content'], $this->sanitizeTags($tagsValue), $lifetime);
                }
            }
        }
        return $this->parser->getOutput();
    }

Usage Example

 /**
  * Post process output for caching information
  *
  * The content cache stores cache segments with markers inside the generated content. This method creates cache
  * segments and will process the final outer result (currentPathIsEntryPoint) to remove all cache markers and
  * store cache entries.
  *
  * @param array $evaluateContext The current evaluation context
  * @param object $tsObject The current TypoScript object (for "this" in evaluations)
  * @param mixed $output The generated output after caching information was removed
  * @return mixed The post-processed output with cache segment markers or cleaned for the entry point
  */
 public function postProcess(array $evaluateContext, $tsObject, $output)
 {
     if ($this->enableContentCache && $evaluateContext['cacheForPathEnabled'] && $evaluateContext['cacheForPathDisabled']) {
         $contextArray = $this->runtime->getCurrentContext();
         if (isset($evaluateContext['configuration']['context'])) {
             $contextVariables = [];
             foreach ($evaluateContext['configuration']['context'] as $contextVariableName) {
                 $contextVariables[$contextVariableName] = $contextArray[$contextVariableName];
             }
         } else {
             $contextVariables = $contextArray;
         }
         $cacheTags = $this->buildCacheTags($evaluateContext['configuration'], $evaluateContext['typoScriptPath'], $tsObject);
         $cacheMetadata = array_pop($this->cacheMetadata);
         $output = $this->contentCache->createDynamicCachedSegment($output, $evaluateContext['typoScriptPath'], $contextVariables, $evaluateContext['cacheIdentifierValues'], $cacheTags, $cacheMetadata['lifetime'], $evaluateContext['cacheDiscriminator']);
     } elseif ($this->enableContentCache && $evaluateContext['cacheForPathEnabled']) {
         $cacheTags = $this->buildCacheTags($evaluateContext['configuration'], $evaluateContext['typoScriptPath'], $tsObject);
         $cacheMetadata = array_pop($this->cacheMetadata);
         $output = $this->contentCache->createCacheSegment($output, $evaluateContext['typoScriptPath'], $evaluateContext['cacheIdentifierValues'], $cacheTags, $cacheMetadata['lifetime']);
     } elseif ($this->enableContentCache && $evaluateContext['cacheForPathDisabled'] && $this->inCacheEntryPoint) {
         $contextArray = $this->runtime->getCurrentContext();
         if (isset($evaluateContext['configuration']['context'])) {
             $contextVariables = [];
             foreach ($evaluateContext['configuration']['context'] as $contextVariableName) {
                 if (isset($contextArray[$contextVariableName])) {
                     $contextVariables[$contextVariableName] = $contextArray[$contextVariableName];
                 } else {
                     $contextVariables[$contextVariableName] = null;
                 }
             }
         } else {
             $contextVariables = $contextArray;
         }
         $output = $this->contentCache->createUncachedSegment($output, $evaluateContext['typoScriptPath'], $contextVariables);
     }
     if ($evaluateContext['cacheForPathEnabled'] && $evaluateContext['currentPathIsEntryPoint']) {
         $output = $this->contentCache->processCacheSegments($output, $this->enableContentCache);
         $this->inCacheEntryPoint = null;
         $this->addCacheSegmentMarkersToPlaceholders = false;
     }
     return $output;
 }