Skip to main content

TemplateCache

Struct TemplateCache 

Source
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

Source

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,

Source

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");
Source

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);
Source

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.

Source

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.

Source

pub fn clear(&self)

Invalidate all cached entries (e.g. after a bulk file update).

Source

pub fn template_count(&self) -> usize

Number of cached main templates.

Source

pub fn include_count(&self) -> usize

Number of cached include templates.

Trait Implementations§

Source§

impl<S> Clone for TemplateCache<S>
where S: Clone + BuildHasher,

Source§

fn clone(&self) -> TemplateCache<S>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Debug for TemplateCache<S>
where S: BuildHasher,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for TemplateCache

Source§

fn default() -> TemplateCache

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S> Freeze for TemplateCache<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for TemplateCache<S>
where S: RefUnwindSafe,

§

impl<S> Send for TemplateCache<S>
where S: Send,

§

impl<S> Sync for TemplateCache<S>
where S: Sync,

§

impl<S> Unpin for TemplateCache<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for TemplateCache<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for TemplateCache<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.