Performance/Caching section of the docs isn’t clear enough. It says that the default caching mode is TimberLoader::CACHE_TRANSIENT
, but there’s no explanation on what does it really mean.
I know that I can disable it or change caching mode using the timber/cache/mode
filter, but what does default caching mode actually mean?
Does it mean that all queries are cached using transients by default? It would be weird if all queries were cached in DB.
2
Answers
Every time you render a .twig file, Twig compiles all the HTML tags and variables into the corresponding function calls and echo statements that actually gets run by PHP. It is this blob of data which is stored in the transient, by default, to save processor cycles on page views.
https://github.com/timber/timber/blob/75bb47223e32b787fd74e92f692f9fc34dec7e74/lib/Loader.php#L69
I was actually wondering the same thing myself, so I did some digging into the actual classes and this is what I found.
In
TimberLoader
, the methodset_cache
calls built in WordPress caching functions depending on the mode:set_transient
, but works across multi-site.WP_Object_Cache
to save to memory and is non-persistent.So it appears it’s usually best to just leave it as the default. Your best performance improvements will actually come by caching the results of expensive queries using
TimberHelper::transient
(see example). The default template caches produced by Twig are actually pretty performant as-is.