forked from datrs/hypercore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.rs
More file actions
58 lines (53 loc) · 1.8 KB
/
cache.rs
File metadata and controls
58 lines (53 loc) · 1.8 KB
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
use moka::sync::Cache;
use std::time::Duration;
use hypercore_schema::Node;
// Default to 1 year of cache
const DEFAULT_CACHE_TTL_SEC: u64 = 31556952;
const DEFAULT_CACHE_TTI_SEC: u64 = 31556952;
// Default to 100kb of node cache
const DEFAULT_CACHE_MAX_SIZE: u64 = 100000;
const NODE_WEIGHT: u32 =
// Byte size of a Node based on the fields.
3 * 8 + 32 + 4 +
// Then 8 for key and guesstimate 8 bytes of overhead.
8 + 8;
#[derive(Debug, Clone)]
pub(crate) struct CacheOptions {
pub(crate) time_to_live: Option<Duration>,
pub(crate) time_to_idle: Option<Duration>,
pub(crate) max_capacity: Option<u64>,
}
impl CacheOptions {
pub(crate) fn new() -> Self {
Self {
time_to_live: None,
time_to_idle: None,
max_capacity: None,
}
}
pub(crate) fn to_node_cache(&self, initial_nodes: Vec<Node>) -> Cache<u64, Node> {
let cache = if self.time_to_live.is_some() || self.time_to_idle.is_some() {
Cache::builder()
.time_to_live(
self.time_to_live
.unwrap_or_else(|| Duration::from_secs(DEFAULT_CACHE_TTL_SEC)),
)
.time_to_idle(
self.time_to_idle
.unwrap_or_else(|| Duration::from_secs(DEFAULT_CACHE_TTI_SEC)),
)
.max_capacity(self.max_capacity.unwrap_or(DEFAULT_CACHE_MAX_SIZE))
.weigher(|_, _| NODE_WEIGHT)
.build()
} else {
Cache::builder()
.max_capacity(self.max_capacity.unwrap_or(DEFAULT_CACHE_MAX_SIZE))
.weigher(|_, _| NODE_WEIGHT)
.build()
};
for node in initial_nodes {
cache.insert(node.index, node);
}
cache
}
}