Adds a no_cache
tag to support dynamic content within cached pages
#5575
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This feature was inspired by work started here: #4893
This PR introduces a
no_cache
tag that interacts closely with the existing half-cache static caching strategy. This tag can be used like so:The following considerations were made with this implementation:
no_cache
tags (such as those within included partials) should "just work" without completely breaking things. This is achieved by disabling the tag for all nestedno_cache
tag pairs, and re-enabling it again to support multiple dynamic regions outside the tag pairno_cache
tag pairs should remain unaffectedno_cache
tag pairsBecause this does not leverage anything specifically provided by the Runtime parser, this implementation will work on both the Regex and Runtime Antlers parsers.
Implementation Details
This implementation works by introducing a cache layer on top of the existing half-cache. If a
no_cache
tag is encountered while producing the response, the no cache manager will handle the response and subsequent requests instead of the existing cache middleware logic.The no cache maintains a few items for each URL that is cached:
now
, csrf token, etc.). This is a serialized file containing an array with the state of each dynamic section present in the manifest file. Each stored context is diff'd against the original context to reduce the final file size (this missing information is added back later on subsequent requests).The modified template will have the original
no_cache
tag pair removed, and will be replaced with a tag similar to the following:The
evaluate
method will locate the dynamic content from the restored manifest file, and load in the contextual data that was available at the time the response was originally generated. This allows state data to be referenced within the extracted dynamic regions, without having to reload everything from tag calls/etc. (if theno_cache
tag pair is inside another tag pair).As an example, the following usage of
no_cache
would remember the results of the collection tag call, without calling the collection tag again on the next request:The modified template that is stored within the cache may look something like this (assuming five entries were returned originally). The extracted dynamic regions are inserted back into the static output (as
no_cache:evaluate
calls):In contrast, if we had supplied the following template instead:
The collection tag itself will become part of the dynamic output, and the cached template file would instead look similar to the following:
Cache Invalidation
The
no_cache
cache can be completely cleared by invoking thephp artisan cache:clear
Artisan command. Additionally, the existing invalidator will call the no cache'sinvalidateUrl
method.