Besides the functions built into the framework that allow embedding standard templates, there is the possibility of placing template content in the cache.
Caching can both speed up some parts of the application and slow them down if those parts already operate quickly. Given that a template should only deliver data and not perform complex calculations, caching should be done at a higher level. However, for strictly specialized cases, especially when multiple templates are embedded within another, leading to increased resource consumption, it makes sense to cache the template for a short period.
Template caching is not suitable for dynamically changing and internal site pages that require authorization, since during the cache lifetime, a user might log out, but this won't be reflected on the page. It is best used for static site pages, where changes are infrequent and in areas where security-critical conditions (such as authorization) are not present.
This function is similar to insertTemplate(), but includes an additional argument sec, where you can specify the duration in seconds to set caching. After this period expires, the next request to the template will refresh it in the cache for the same number of seconds (one minute in the example).
Care should be taken with the data that enters the cached template and also with data that might be obtained within the template from external sources.
In the first case, a new cache will be created based on the hash of changed data, leading to increased disk space usage by cached data.
In the second case, the data will not change and will remain in the cache from the moment it was refreshed.
In the example, a separate hash will be created for each different user ID upon request, and for the value NULL, another cache variant will be returned.
When in doubt about the appropriateness of template caching, it's better not to do it.
← Standard Templates TWIG Templating Engine →