class ContentCache (View source)

A wrapper around a Neos Flow cache which provides additional functionality for caching partial content (segments) rendered by the Fusion Runtime.

The cache build process generally follows these steps:

  • render the whole document as usual (for example a page) but insert special markers before and after the rendered segments
  • parse the rendered document and extract segments by the previously added markers

This results in two artifacts:

  • an array of content segments which are later stored as cache entries (if they may be cached)
  • a string called "output" which is the originally rendered output but without the markers

We use non-visible ASCII characters as markers / tokens in order to minimize potential conflicts with the actual content.

Note: If you choose a different cache backend for this content cache, make sure that it is one implementing TaggableBackendInterface.

Constants

CACHE_SEGMENT_START_TOKEN

CACHE_SEGMENT_END_TOKEN

CACHE_SEGMENT_SEPARATOR_TOKEN

CACHE_SEGMENT_MARKER

CACHE_PLACEHOLDER_REGEX

EVAL_PLACEHOLDER_REGEX

MAXIMUM_NESTING_LEVEL

TAG_EVERYTHING

A cache entry tag that will be used by default to flush an entry on "every" change - whatever that means to the application.

SEGMENT_TYPE_CACHED

SEGMENT_TYPE_UNCACHED

SEGMENT_TYPE_DYNAMICCACHED

Properties

protected StringFrontend $cache
protected PropertyMapper $propertyMapper
protected Context $securityContext
protected string $randomCacheMarker

Methods

__construct()

ContentCache constructor

string
createCacheSegment(string $content, string $fusionPath, array $cacheIdentifierValues, array $tags = [], int $lifetime = null)

Takes the given content and adds markers for later use as a cached content segment.

string
createUncachedSegment(string $content, string $fusionPath, array $contextVariables)

Similar to createCacheSegment() creates a content segment with markers added, but in contrast to that function this method is used for rendering a segment which is not supposed to be cached.

string
createDynamicCachedSegment(string $content, string $fusionPath, array $contextVariables, array $cacheIdentifierValues, array $tags, int $lifetime, string $cacheDiscriminator)

Similar to createUncachedSegment() creates a content segment with markers added, but in contrast to that function this method is used for rendering a segment which will be evaluated at runtime but can still be cached.

string
renderContentCacheEntryIdentifier(string $fusionPath, array $cacheIdentifierValues)

Renders an identifier for a content cache entry

string
processCacheSegments(string $content, bool $storeCacheEntries = true)

Takes a string of content which includes cache segment markers, extracts the marked segments, writes those segments which can be cached to the actual cache and returns the cleaned up original content without markers.

string|bool
getCachedSegment(Closure $uncachedCommandCallback, string $fusionPath, array $cacheIdentifierValues, bool $addCacheSegmentMarkersToPlaceholders = false, string|bool $cacheDiscriminator = null)

Tries to retrieve the specified content segment from the cache – further nested inline segments are retrieved as well and segments which were not cacheable are rendered.

int|bool
replaceCachePlaceholders(string $content, bool $addCacheSegmentMarkersToPlaceholders)

Find cache placeholders in a cached segment and return the identifiers

int
replaceUncachedPlaceholders(Closure $uncachedCommandCallback, string $content)

Replace segments which are marked as not-cacheable by their actual content by invoking the Fusion Runtime.

array
serializeContext(array $contextVariables)

Generates an array of strings from the given array of context variables

string
getTypeForContextValue(mixed $contextValue)

TODO: Adapt to Flow change https://review.typo3.org/#/c/33138/

int
flushByTag(string $tag)

Flush content cache entries by tag

int
flushByTags(array $tags)

Flush content cache entries by tags

void
flush()

Flush all content cache entries

string
sanitizeTag(string $tag)

Sanitizes the given tag for use with the cache framework

array
sanitizeTags(array $tags)

Sanitizes multiple tags with sanitizeTag()

Details

__construct()

ContentCache constructor

string createCacheSegment(string $content, string $fusionPath, array $cacheIdentifierValues, array $tags = [], int $lifetime = null)

Takes the given content and adds markers for later use as a cached content segment.

This function will add a start and an end token to the beginning and end of the content and generate a cache identifier based on the current Fusion path and additional values which were defined in the Fusion configuration by the site integrator.

The whole cache segment (START TOKEN + IDENTIFIER + SEPARATOR TOKEN + original content + END TOKEN) is returned as a string.

This method is called by the Fusion Runtime while rendering a Fusion object.

Parameters

string $content

The (partial) content which should potentially be cached later on

string $fusionPath

The Fusion path that rendered the content, for example "page/body/parts/breadcrumbMenu"

array $cacheIdentifierValues

The values (simple type or implementing CacheAwareInterface) that should be used to create a cache identifier, will be sorted by keys for consistent ordering

array $tags

Tags to add to the cache entry

int $lifetime

Lifetime of the cache segment in seconds. NULL for the default lifetime and 0 for unlimited lifetime.

Return Value

string

The original content, but with additional markers and a cache identifier added

string createUncachedSegment(string $content, string $fusionPath, array $contextVariables)

Similar to createCacheSegment() creates a content segment with markers added, but in contrast to that function this method is used for rendering a segment which is not supposed to be cached.

This method is called by the Fusion Runtime while rendering a Fusion object.

Parameters

string $content

The content rendered by the Fusion Runtime

string $fusionPath

The Fusion path that rendered the content, for example "page/body/parts/breadcrumbMenu"

array $contextVariables

Fusion context variables which are needed to correctly render the specified Fusion object

Return Value

string

The original content, but with additional markers added

string createDynamicCachedSegment(string $content, string $fusionPath, array $contextVariables, array $cacheIdentifierValues, array $tags, int $lifetime, string $cacheDiscriminator)

Similar to createUncachedSegment() creates a content segment with markers added, but in contrast to that function this method is used for rendering a segment which will be evaluated at runtime but can still be cached.

This method is called by the Fusion Runtime while rendering a Fusion object.

Parameters

string $content

The content rendered by the Fusion Runtime

string $fusionPath

The Fusion path that rendered the content, for example "page/body/parts/breadcrumbMenu"

array $contextVariables

Fusion context variables which are needed to correctly render the specified Fusion object

array $cacheIdentifierValues
array $tags

Tags to add to the cache entry

int $lifetime

Lifetime of the cache segment in seconds. NULL for the default lifetime and 0 for unlimited lifetime.

string $cacheDiscriminator

The evaluated cache discriminator value

Return Value

string

The original content, but with additional markers added

protected string renderContentCacheEntryIdentifier(string $fusionPath, array $cacheIdentifierValues)

Renders an identifier for a content cache entry

Parameters

string $fusionPath
array $cacheIdentifierValues

Return Value

string

An MD5 hash built from the fusionPath and certain elements of the given identifier values

Exceptions

CacheException

If an invalid entry identifier value is given

string processCacheSegments(string $content, bool $storeCacheEntries = true)

Takes a string of content which includes cache segment markers, extracts the marked segments, writes those segments which can be cached to the actual cache and returns the cleaned up original content without markers.

This method is called by the Fusion Runtime while rendering a Fusion object.

Parameters

string $content

The content with an outer cache segment

bool $storeCacheEntries

Whether to store extracted cache segments in the cache

Return Value

string

The (pure) content without cache segment markers

string|bool getCachedSegment(Closure $uncachedCommandCallback, string $fusionPath, array $cacheIdentifierValues, bool $addCacheSegmentMarkersToPlaceholders = false, string|bool $cacheDiscriminator = null)

Tries to retrieve the specified content segment from the cache – further nested inline segments are retrieved as well and segments which were not cacheable are rendered.

Parameters

Closure $uncachedCommandCallback

A callback to process commands in uncached segments

string $fusionPath

Fusion path identifying the Fusion object to retrieve from the content cache

array $cacheIdentifierValues

Further values which play into the cache identifier hash, must be the same as the ones specified while the cache entry was written

bool $addCacheSegmentMarkersToPlaceholders

If cache segment markers should be added – this makes sense if the cached segment is about to be included in a not-yet-cached segment

string|bool $cacheDiscriminator

The evaluated cache discriminator value, if any and false if the cache discriminator is disabled for the current context

Return Value

string|bool

The segment with replaced cache placeholders, or false if a segment was missing in the cache

Exceptions

Exception

protected int|bool replaceCachePlaceholders(string $content, bool $addCacheSegmentMarkersToPlaceholders)

Find cache placeholders in a cached segment and return the identifiers

Parameters

string $content
bool $addCacheSegmentMarkersToPlaceholders

Return Value

int|bool

Number of replaced placeholders or false if a placeholder couldn't be found

protected int replaceUncachedPlaceholders(Closure $uncachedCommandCallback, string $content)

Replace segments which are marked as not-cacheable by their actual content by invoking the Fusion Runtime.

Parameters

Closure $uncachedCommandCallback
string $content

The content potentially containing not cacheable segments marked by the respective tokens

Return Value

int

Number of replaced placeholders

protected array serializeContext(array $contextVariables)

Generates an array of strings from the given array of context variables

Parameters

array $contextVariables

Return Value

array

Exceptions

InvalidArgumentException

protected string getTypeForContextValue(mixed $contextValue)

TODO: Adapt to Flow change https://review.typo3.org/#/c/33138/

Parameters

mixed $contextValue

Return Value

string

int flushByTag(string $tag)

Flush content cache entries by tag

Parameters

string $tag

A tag value that was assigned to a cache entry in Fusion, for example "Everything", "Node[…]", "NodeType[…]", "DescendantOf_[…]" whereas "…" is the node identifier or node type respectively

Return Value

int

The number of cache entries which actually have been flushed

int flushByTags(array $tags)

Flush content cache entries by tags

Parameters

array $tags

values that were assigned to a cache entry in Fusion, for example "Everything", "Node[…]", "NodeType[…]", "DescendantOf_[…]" whereas "…" is the node identifier or node type respectively

Return Value

int

The number of cache entries which actually have been flushed

void flush()

Flush all content cache entries

Return Value

void

protected string sanitizeTag(string $tag)

Sanitizes the given tag for use with the cache framework

Parameters

string $tag

A tag which possibly contains non-allowed characters, for example "NodeType_Acme.Com:Page"

Return Value

string

A cleaned up tag, for example "NodeType_Acme_Com-Page"

protected array sanitizeTags(array $tags)

Sanitizes multiple tags with sanitizeTag()

Parameters

array $tags

Multiple tags

Return Value

array

The sanitized tags