-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcache.rs
More file actions
109 lines (92 loc) · 3.1 KB
/
Copy pathcache.rs
File metadata and controls
109 lines (92 loc) · 3.1 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::marker::PhantomData;
use educe::Educe;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use stackable_shared::time::Duration;
/// [`TtlCache`] with sensible defaults for a user information cache
pub type UserInformationCache = TtlCache<UserInformationCacheDefaults>;
/// Default tunings for [`UserInformationCache`].
#[derive(JsonSchema)]
pub struct UserInformationCacheDefaults;
impl TtlCacheDefaults for UserInformationCacheDefaults {
fn entry_time_to_live() -> Duration {
Duration::from_secs(30)
}
fn max_entries() -> u32 {
10_000
}
}
/// Structure to configure a TTL cache in a product.
#[derive(Deserialize, Educe, JsonSchema, Serialize)]
#[serde(
rename_all = "camelCase",
bound(deserialize = "D: TtlCacheDefaults", serialize = "D: TtlCacheDefaults")
)]
#[schemars(
description = "Least Recently Used (LRU) cache with per-entry time-to-live (TTL) value.",
// We don't care about the fields, but we also use JsonSchema to derive the name for the composite type
bound(serialize = "D: TtlCacheDefaults + JsonSchema")
)]
#[educe(
Clone(bound = false),
Debug(bound = false),
PartialEq(bound = false),
Eq
)]
pub struct TtlCache<D> {
/// Time to live per entry
#[serde(default = "D::entry_time_to_live")]
pub entry_time_to_live: Duration,
/// Maximum number of entries in the cache; If this threshold is reached then the least
/// recently used item is removed.
#[serde(default = "D::max_entries")]
pub max_entries: u32,
#[serde(skip)]
pub _defaults: PhantomData<D>,
}
impl<D: TtlCacheDefaults> Default for TtlCache<D> {
fn default() -> Self {
Self {
entry_time_to_live: D::entry_time_to_live(),
max_entries: D::max_entries(),
_defaults: PhantomData,
}
}
}
/// A set of default values for [`TtlCache`].
///
/// This is extracted to a separate trait in order to be able to provide different
/// default tunings for different use cases.
pub trait TtlCacheDefaults {
/// The default TTL the entries should have.
fn entry_time_to_live() -> Duration;
/// The default for the maximum number of entries
fn max_entries() -> u32;
}
#[cfg(test)]
mod tests {
use super::*;
type MyCache = TtlCache<UserInformationCacheDefaults>;
#[test]
fn test_defaults() {
let my_cache: MyCache = Default::default();
assert_eq!(my_cache.entry_time_to_live, Duration::from_secs(30));
assert_eq!(my_cache.max_entries, 10_000);
}
#[test]
fn test_deserialization_defaults() {
let my_cache: MyCache = serde_json::from_str("{}").unwrap();
assert_eq!(my_cache.entry_time_to_live, Duration::from_secs(30));
assert_eq!(my_cache.max_entries, 10_000);
}
#[test]
fn test_deserialization() {
let my_cache: MyCache = serde_yaml::from_str("entryTimeToLive: 13h\n").unwrap();
assert_eq!(
my_cache.entry_time_to_live,
Duration::from_hours_unchecked(13)
);
// As the field is not specified we default
assert_eq!(my_cache.max_entries, 10_000);
}
}