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
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};

/// Cache interface that crate used.
///
/// Note: must implement it with multi-thread safety.
pub trait Cacher {
    fn cache_store(&self, _key: &str, _val: &str) {}

    fn cache_get(&self, _key: &str) -> Option<String> {
        None
    }
}

#[derive(Default)]
pub struct NoCacher {}

impl Cacher for NoCacher {}

/// A simple in memory cacher which remember until poweroff.
#[derive(Default)]
pub struct SimpleMemCacher {
    pub map: Mutex<HashMap<String, String>>,
}

impl Cacher for SimpleMemCacher {
    fn cache_store(&self, key: &str, val: &str) {
        self.map
            .lock()
            .expect("SimpleMemCacher poisoned")
            .insert(key.to_string(), val.to_string());
    }

    fn cache_get(&self, key: &str) -> Option<String> {
        self.map
            .lock()
            .expect("SimpleMemCacher poisoned")
            .get(key)
            .cloned()
    }
}

/// A simple in memory cacher which only remember each item in a short duration.
pub struct SimpleFishMemCacher {
    pub map: Mutex<HashMap<String, (Instant, String)>>,
    pub forgot_duration: Duration,
}

impl SimpleFishMemCacher {
    pub fn new(forgot_duration: Duration) -> Self {
        Self {
            forgot_duration,
            ..Default::default()
        }
    }
}

impl Default for SimpleFishMemCacher {
    fn default() -> Self {
        Self {
            map: Default::default(),
            forgot_duration: Duration::from_secs(120),
        }
    }
}

impl Cacher for SimpleFishMemCacher {
    fn cache_store(&self, key: &str, val: &str) {
        self.map
            .lock()
            .expect("SimpleFishMemCacher poisoned")
            .insert(key.to_string(), (Instant::now(), val.to_string()));
    }

    fn cache_get(&self, key: &str) -> Option<String> {
        self.map
            .lock()
            .expect("SimpleFishMemCacher poisoned")
            .get(key)
            .filter(|(i, _)| i.elapsed() < self.forgot_duration)
            .map(|(_, v)| v)
            .cloned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_mem_cacher() {
        let c = SimpleMemCacher::default();
        c.cache_store("abc", "fed");
        assert_eq!(c.cache_get("abc"), Some(String::from("fed")));
    }

    #[test]
    fn test_simple_fish_mem_cacher() {
        let d = Duration::from_millis(100);
        let c = SimpleFishMemCacher::new(d);
        c.cache_store("abc", "fed");
        assert_eq!(c.cache_get("abc"), Some(String::from("fed")));

        std::thread::sleep(d);
        assert_eq!(c.cache_get("abc"), None);
    }
}