pub struct TemplateCache { /* private fields */ }Expand description
Template cache with LRU eviction policy
QUICK WIN 3: CACHE IMPROVEMENTS
- Increased default capacity from 100 to 5000 templates
- Added cache statistics and hit/miss tracking
- Support for cache warming on startup
Provides thread-safe LRU (Least Recently Used) caching for parsed templates to improve generation performance by avoiding redundant parsing.
§Features
- Thread-safe: Uses
Arc<Mutex<...>>for concurrent access - LRU eviction: Automatically evicts least recently used templates when capacity is reached
- Automatic parsing: Parses templates on cache miss
- Statistics: Provides cache size, capacity, and hit/miss information
- Large capacity: Default 5000 templates (up from 100)
§Examples
use crate::template_cache::TemplateCache;
use std::path::Path;
// Create cache with default capacity (5000 templates)
let cache = TemplateCache::default();
// Get template (parses if not cached)
let template = cache.get_or_parse(Path::new("template.tmpl"))?;
// Check cache statistics
let stats = cache.stats()?;
println!("Cache: {}/{} templates", stats.size, stats.capacity);
println!("Hit rate: {:.1}%", stats.hit_rate());Implementations§
Source§impl TemplateCache
impl TemplateCache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new template cache with specified capacity
Creates a cache that can hold up to capacity templates. When the cache
is full, the least recently used template is evicted to make room.
§Arguments
capacity- Maximum number of templates to cache (minimum 1)
§Examples
use crate::template_cache::TemplateCache;
// Create cache with capacity of 100 templates
let cache = TemplateCache::new(100);
// Or use default (100 templates)
let default_cache = TemplateCache::default();Sourcepub fn get_or_parse(&self, path: &Path) -> Result<Arc<Template>>
pub fn get_or_parse(&self, path: &Path) -> Result<Arc<Template>>
Get template from cache or parse if not present
Returns a cached template if available, otherwise parses the template from the file system and caches it for subsequent use.
§Arguments
path- Path to the template file
§Returns
An Arc<Template> containing the parsed template. The same Arc is
returned for subsequent calls with the same path (until eviction).
§Examples
use crate::template_cache::TemplateCache;
use std::path::Path;
let cache = TemplateCache::new(10);
// First call - parses and caches
let template1 = cache.get_or_parse(Path::new("template.tmpl"))?;
// Second call - returns cached version
let template2 = cache.get_or_parse(Path::new("template.tmpl"))?;
// Same Arc (shared reference)
assert!(std::sync::Arc::ptr_eq(&template1, &template2));QUICK WIN 3: Added hit/miss tracking for cache metrics
Sourcepub fn get_tera_cached(&self, key: &str) -> Option<String>
pub fn get_tera_cached(&self, key: &str) -> Option<String>
Sourcepub fn cache_tera_template(&self, key: &str, content: String)
pub fn cache_tera_template(&self, key: &str, content: String)
Store a Tera template string in cache
OPTIMIZATION 3.2: Cache template strings for reuse
§Arguments
key- Cache key (typically file path)content- Template content to cache
Sourcepub fn clear(&self) -> Result<()>
pub fn clear(&self) -> Result<()>
Clear all cached templates
Removes all templates from the cache, freeing memory. The cache capacity remains unchanged.
§Examples
use crate::template_cache::TemplateCache;
use std::path::Path;
let cache = TemplateCache::new(10);
cache.get_or_parse(Path::new("template.tmpl"))?;
assert_eq!(cache.stats()?.size, 1);
cache.clear()?;
assert_eq!(cache.stats()?.size, 0);Sourcepub fn stats(&self) -> Result<CacheStats>
pub fn stats(&self) -> Result<CacheStats>
Get cache statistics
Returns information about the current cache state, including size (number of cached templates) and capacity (maximum cache size).
§Examples
use crate::template_cache::TemplateCache;
use std::path::Path;
let cache = TemplateCache::new(50);
let stats = cache.stats()?;
println!("Cache: {}/{} templates", stats.size, stats.capacity);QUICK WIN 3 + OPTIMIZATION 3: Enhanced statistics with hit/miss tracking and cache sizes
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TemplateCache
impl RefUnwindSafe for TemplateCache
impl Send for TemplateCache
impl Sync for TemplateCache
impl Unpin for TemplateCache
impl UnsafeUnpin for TemplateCache
impl UnwindSafe for TemplateCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> EvidenceKind for T
impl<T> EvidenceKind for T
default fn kind_label(&self) -> &'static str
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more