Expand description
Page-keyed template-fragment caching.
§Why there is no {% rcmscache %} Tera tag
A fragment-cache tag would have to be a block tag that skips
rendering its body on a cache hit. Tera cannot express this: its grammar
is fixed — there is no API to register a custom block statement,
and the one block-shaped extension point, {% filter foo %}… {% endfilter %}, renders the body first and then hands the
string to the filter, so it can never skip the expensive render.
A template-level cache tag is therefore impossible on this engine.
§What you do instead
Cache the expensive computation in Rust — before it reaches the
template — with the framework’s
rustango::cache_fragment::cached_render (re-exported here as
cached_render). That’s exactly how crate::auto_menu caches
the menu tree. Build the key with page_fragment_key so it
auto-invalidates when the page republishes, and bypass the
cache while previewing (pass the live value instead of calling
cached_render) so editors always see their unsaved edits.
use rustango_cms::fragment_cache::{cached_render, page_fragment_key};
// In host/app code that holds a `cache: BoxedCache` (the same one
// wired into `PublicRouter::cached`) — e.g. a custom route or a
// context builder that has the cache in hand:
let body = if previewing {
render_related_panel(pool, &page).await // never cache a preview
} else {
let key = page_fragment_key(&tenant.org.slug, &page, "related");
cached_render(cache.as_ref(), &key, Some(ttl), || async {
render_related_panel(pool, &page).await // computed once per (page, publish)
})
.await
};Functions§
- cached_
render - Return the cached fragment for
key, or compute and store it. - page_
fragment_ key - Cache key for a page-scoped fragment. Encodes the page id + its
publish version stamp, so the key changes — and the old entry is
abandoned — every time the page republishes (no explicit purge
needed).
suffixdistinguishes fragments on the same page (e.g."related","toc","sidebar").