Lesson 3.5: Caches for template developers
Exponential is fast because it caches at several levels, and template developers meet every one of them: a change
that "does not work" is often a cache that still holds the old result. This lesson explains the three caches a
template passes through, how to clear exactly the one a change needs, and how to use {cache-block} in templates of
your own.
At a glance
| Course | Developers |
| Module | Module 3: Designs, overrides and debugging |
| Duration | 45 minutes (20 presenting, 25 exercise) |
| Level | Intermediate |
| Audience | Developers and template builders |
| Prerequisites | Lesson 3.4 |
Learning objectives
After this lesson, learners can:
- describe the compiled template cache, the content view cache and cache blocks;
- clear caches by tag and by id with
php bin/php/ezcache.php; - choose the cache to clear for a template edit, a new override, a settings change and a stylesheet change;
- write a
{cache-block}withkeysandexpiry, and know whensubtree_expiryandignore_content_expiryhelp.
Trainer's notes
How to present it
Sources: Caches, settings/site.ini ([TemplateSettings], [ContentSettings])
and lib/eztemplate/classes/eztemplatecachefunction.php.
Three layers (6 minutes).
Cache Setting (default) Holds Clear with Compiled templates [TemplateSettings] TemplateCompile=enabledEach .tplcompiled to PHP--clear-tag=templateContent view cache [ContentSettings] ViewCaching=enabled,CachedViewModes=full;sitemap;pdfThe rendered result of a node view --clear-tag=content, or per node withezcontentcache.phpCache blocks [TemplateSettings] TemplateCache=enabledParts of a template wrapped in {cache-block}--clear-id=template-blockClearing by tag and id (4 minutes).
php bin/php/ezcache.php --list-tagsand--list-idsshow what exists. Tags group ids (content,template,ini...); ids are single caches (template-override,template-block,design_base,ezjscore-packer...). Avoid--clear-allas a habit: it costs the next visitors a slow page each.Which cache for which change (4 minutes).
You changed Clear An existing .tpltemplate(none whileTemplateCompile=disabled), thencontentfor cached pagesA new override rule or new template path ini,template-override,template,contentA setting iniA stylesheet or script loaded by the packer ezjscore-packerandtemplate-blockcache-block(6 minutes).{cache-block keys=array( $uri_string ) expiry=3600}...{/cache-block}stores the output once per key combination. Withoutexpirythe default is two hours. By default a cache block expires when any content is published;ignore_content_expiryturns that off,subtree_expirylimits it to one subtree.
Common questions
Should I switch compiling off on my development machine? Many developers set TemplateCompile=disabled while
editing templates, so every edit is read fresh. Never on a live site.
What does eztc.php do? It compiles all templates of a siteaccess in advance (php bin/php/eztc.php -s<sa>),
so the first visitors after a deploy do not pay for compiling.
Can the administration clear caches? Yes: Setup > Cache management, or the Clear cache box with All caches, Content, Template, Template & content, Ini settings and Static.
Pitfalls
- A cache block without keys around per-user content. Everyone then sees the first visitor's version, for example their name. Put user-specific parts outside cache blocks, or key them correctly.
- Clearing before a reload of the PHP workers. On a server that keeps PHP code in memory (Velocity, PHP-FPM), clear caches after the workers have the new code, or old pages are cached again (lesson 13.2).
- Stylesheet changes that do not show. The packed file name is cached inside a cache block of the pagelayout:
clear
template-blocktoo.
Hands-on exercise
On the development installation, with TemplateCompile=enabled (the default).
List the cache tags and ids:
php bin/php/ezcache.php --list-tagsandphp bin/php/ezcache.php --list-ids.Change the text of the course line in
mysite_article.tplfrom lesson 3.2 and reload an article. Note whether the change shows.Clear only the template cache, reload; then clear the content tag, reload. Note which step made the change visible.
Clear the cache of one node only:
php bin/php/ezcontentcache.php --clear-node=<node ID>and check the help with--help.Add a cache block to
mysite_article.tpl:cache-block keys=array( $node.node_id ) expiry=60 <p>Cached at currentdate()|l10n( 'shortdatetime' )</p> /cache-blockClear the
templatetag andcontenttag, then reload the article twice: the time stays the same.Publish a change to any item in the administration, reload: the block was renewed, because cache blocks expire on publishing by default.
Add
ignore_content_expiryto the block's parameters, clear the template and content caches, and repeat step 7: now the time stays until the 60 seconds are over.Compile the templates of your siteaccess in advance:
php bin/php/eztc.php -s<sa>.Remove the cache block again, clear the caches, and commit.
Expected result
- Step 3: the template clear alone is not always enough when the page itself is in the view cache; after the content clear the change shows.
- Step 6: two reloads show the same time; step 7 shows a new time; step 8 keeps the time until the expiry.
- Step 9 lists the compiled templates and ends without errors.
- The trainer asks each learner which caches a new override needs.
Quiz
- Which setting turns the content view cache on or off?
A.
TemplateCompileB.ViewCachingC.TemplateCacheD.DesignLocationCache - What is the difference between
--clear-tagand--clear-id? - Which caches does a new override rule with a new template need?
- When does a
{cache-block}without parameters expire? - Why must a block that shows the current user's name not be cached without the right keys?
Answers
- B.
[ContentSettings] ViewCaching. - A tag groups several caches; an id names one cache.
ini,template-override,template, andcontentfor pages already cached.- After the default two hours, or earlier when content is published.
- The first visitor's output would be shown to everyone.
Further reading
- Caches: what each one holds and how to clear it.
- Preloading caches: warming pages after a deploy.
- Operating a site: clearing by what changed.
Related pages
- Module 3: Designs, overrides and debugging
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.