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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::collections::VecDeque;
use std::time::Duration;
use std::time::Instant;

pub struct ExpiringMap<K, V> {
    limit: Duration,
    map: BTreeMap<K, (V, Instant)>,
    dq: VecDeque<(K, Instant)>,
}

impl<K, V> ExpiringMap<K, V>
where
    K: Clone + Ord,
{
    pub fn new(limit: Duration) -> Self {
        Self {
            limit,
            map: Default::default(),
            dq: Default::default(),
        }
    }

    fn cleanup(&mut self, now: &Instant) {
        while let Some((k, prev)) = self.dq.front() {
            if now.duration_since(*prev) < self.limit {
                return;
            }
            self.map.remove(k);
            self.dq.pop_front();
        }
    }

    #[cfg(test)]
    pub fn get(&mut self, key: &K) -> Option<&V> {
        let now = Instant::now();
        self.cleanup(&now);
        self.map.get(key).map(|(v, _)| v)
    }

    pub fn get_or_insert_with<F: FnOnce() -> std::result::Result<V, E>, E>(
        &mut self,
        key: &K,
        f: F,
    ) -> std::result::Result<&V, E> {
        let now = Instant::now();
        self.cleanup(&now);

        let (v, _) = match self.map.entry(key.clone()) {
            Entry::Occupied(o) => o.into_mut(),
            Entry::Vacant(v) => {
                let value = f()?;
                self.dq.push_back((key.clone(), now));
                v.insert((value, now))
            }
        };
        Ok(v)
    }

    pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
        let now = Instant::now();
        self.cleanup(&now);
        self.map.get_mut(key).map(|(v, _)| v)
    }

    pub fn remove(&mut self, key: &K) {
        self.map.remove(key);
        self.dq.retain(|(k, _)| k != key);
    }
}