1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::sync::{Arc, Mutex};
use crate::EntityTag;
#[cfg(debug_assertions)]
use crate::ReloadableTera;
#[cfg(not(debug_assertions))]
use crate::Tera;
use crate::lru_time_cache::LruCache;
#[cfg(debug_assertions)]
#[derive(Educe)]
#[educe(Debug)]
#[allow(clippy::type_complexity)]
pub struct TeraContextManager {
pub tera: Mutex<ReloadableTera>,
#[educe(Debug(ignore))]
cache_table: Mutex<LruCache<String, (Arc<str>, Arc<EntityTag>)>>,
}
#[cfg(not(debug_assertions))]
#[derive(Educe)]
#[educe(Debug)]
#[allow(clippy::type_complexity)]
pub struct TeraContextManager {
pub tera: Tera,
#[educe(Debug(ignore))]
cache_table: Mutex<LruCache<String, (Arc<str>, Arc<EntityTag>)>>,
}
impl TeraContextManager {
#[cfg(debug_assertions)]
#[inline]
pub(crate) fn new(tera: Mutex<ReloadableTera>, cache_capacity: usize) -> TeraContextManager {
TeraContextManager {
tera,
cache_table: Mutex::new(LruCache::with_capacity(cache_capacity)),
}
}
#[cfg(not(debug_assertions))]
#[inline]
pub(crate) fn new(tera: Tera, cache_capacity: usize) -> TeraContextManager {
TeraContextManager {
tera,
cache_table: Mutex::new(LruCache::with_capacity(cache_capacity)),
}
}
#[inline]
pub fn clear_cache(&self) {
self.cache_table.lock().unwrap().clear();
}
#[inline]
pub fn contains_key<S: AsRef<str>>(&self, key: S) -> bool {
self.cache_table.lock().unwrap().get(key.as_ref()).is_some()
}
#[inline]
pub fn get<S: AsRef<str>>(&self, key: S) -> Option<(Arc<str>, Arc<EntityTag>)> {
self.cache_table
.lock()
.unwrap()
.get(key.as_ref())
.map(|(html, etag)| (html.clone(), etag.clone()))
}
#[inline]
pub fn insert<S: Into<String>>(
&self,
key: S,
cache: (Arc<str>, Arc<EntityTag>),
) -> Option<(Arc<str>, Arc<EntityTag>)> {
self.cache_table.lock().unwrap().insert(key.into(), cache)
}
}