pub struct TemplateCache<S = RandomState>where
S: BuildHasher,{ /* private fields */ }Expand description
A template compilation cache parameterised over a BuildHasher
for content-addressed invalidation.
The default hasher is RandomState (SipHash-1-3). Supply a
different BuildHasher via with_hasher if
you need a faster or more collision-resistant hash.
§Examples
use md_tmpl_core::TemplateCache;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("greeting.tmpl.md");
std::fs::write(
&path,
r#"---
params:
- name = str
---
Hi {{ name }}!"#,
)
.unwrap();
let cache = TemplateCache::new();
let tmpl = cache.load(&path).unwrap();
// Second load of same unchanged file — returns cached, zero re-parsing.
let tmpl2 = cache.load(&path).unwrap();
assert_eq!(tmpl.source_hash(), tmpl2.source_hash());Implementations§
Source§impl TemplateCache
impl TemplateCache
Sourcepub fn new() -> TemplateCache
pub fn new() -> TemplateCache
Create a new empty cache using the default content hasher (SipHash-1-3).
Source§impl<S> TemplateCache<S>where
S: BuildHasher,
impl<S> TemplateCache<S>where
S: BuildHasher,
Sourcepub fn with_hasher(hasher: S) -> TemplateCache<S>
pub fn with_hasher(hasher: S) -> TemplateCache<S>
Create a new empty cache with a custom BuildHasher.
The default uses RandomState (SipHash-1-3). Supply a
different BuildHasher if you need stronger collision resistance
or faster hashing (e.g. xxHash, FxHash, AHasher).
§Examples
use std::{collections::hash_map::DefaultHasher, hash::BuildHasherDefault};
use md_tmpl_core::TemplateCache;
let cache = TemplateCache::with_hasher(BuildHasherDefault::<DefaultHasher>::default());
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.tmpl.md");
std::fs::write(
&path,
r#"---
params: [x = str]
---
{{ x }}"#,
)
.unwrap();
let tmpl = cache.load(&path).unwrap();
let mut ctx = md_tmpl_core::Context::new();
ctx.set("x", "works");
assert_eq!(tmpl.render_ctx(&ctx).unwrap(), "works");Sourcepub fn with_max_entries(self, max: usize) -> TemplateCache<S>
pub fn with_max_entries(self, max: usize) -> TemplateCache<S>
Set the maximum number of entries per cache map.
When a new entry is inserted and the cache exceeds this limit,
the least-recently-used entry is evicted. None (the default)
disables eviction.
§Examples
use md_tmpl_core::TemplateCache;
let cache = TemplateCache::new().with_max_entries(128);Sourcepub fn load(&self, path: &Path) -> Result<Template, TemplateError>
pub fn load(&self, path: &Path) -> Result<Template, TemplateError>
Load a template from file, using the cache if the source is unchanged.
§Errors
Returns TemplateError::Io if the file cannot be read, or
TemplateError::Syntax if compilation fails.
Sourcepub fn load_with_frontmatter(
&self,
path: &Path,
) -> Result<(Template, Frontmatter), TemplateError>
pub fn load_with_frontmatter( &self, path: &Path, ) -> Result<(Template, Frontmatter), TemplateError>
Load a template and return frontmatter too, using the cache.
§Errors
Returns TemplateError on I/O or syntax errors.
Sourcepub fn template_count(&self) -> usize
pub fn template_count(&self) -> usize
Number of cached main templates.
Sourcepub fn include_count(&self) -> usize
pub fn include_count(&self) -> usize
Number of cached include templates.
Trait Implementations§
Source§impl<S> Clone for TemplateCache<S>where
S: Clone + BuildHasher,
impl<S> Clone for TemplateCache<S>where
S: Clone + BuildHasher,
Source§fn clone(&self) -> TemplateCache<S>
fn clone(&self) -> TemplateCache<S>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more