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

createDynamicCachedSegment() public method

This method is called by the TypoScript Runtime while rendering a TypoScript object.
public createDynamicCachedSegment ( string $content, string $typoScriptPath, array $contextVariables, array $cacheIdentifierValues, array $tags = [], integer $lifetime = null, string $cacheDiscriminator ) : string
$content string The content rendered by the TypoScript Runtime
$typoScriptPath string The TypoScript path that rendered the content, for example "page/body/parts/breadcrumbMenu"
$contextVariables array TypoScript context variables which are needed to correctly render the specified TypoScript object
$cacheIdentifierValues array
$tags array Tags to add to the cache entry
$lifetime integer Lifetime of the cache segment in seconds. NULL for the default lifetime and 0 for unlimited lifetime.
$cacheDiscriminator string The evaluated cache discriminator value
return string The original content, but with additional markers added
    public function createDynamicCachedSegment($content, $typoScriptPath, array $contextVariables, array $cacheIdentifierValues, array $tags = [], $lifetime = null, $cacheDiscriminator)
    {
        $metadata = implode(',', $tags);
        if ($lifetime !== null) {
            $metadata .= ';' . $lifetime;
        }
        $cacheDiscriminator = md5($cacheDiscriminator);
        $identifier = $this->renderContentCacheEntryIdentifier($typoScriptPath, $cacheIdentifierValues) . '_' . $cacheDiscriminator;
        $segmentData = ['path' => $typoScriptPath, 'metadata' => $metadata, 'context' => $this->serializeContext($contextVariables)];
        return self::CACHE_SEGMENT_START_TOKEN . $this->randomCacheMarker . 'evalCached=' . $identifier . self::CACHE_SEGMENT_SEPARATOR_TOKEN . $this->randomCacheMarker . json_encode($segmentData) . self::CACHE_SEGMENT_SEPARATOR_TOKEN . $this->randomCacheMarker . $content . self::CACHE_SEGMENT_END_TOKEN . $this->randomCacheMarker;
    }

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;
 }