-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegative_cache.rs
More file actions
49 lines (44 loc) · 1.23 KB
/
negative_cache.rs
File metadata and controls
49 lines (44 loc) · 1.23 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
use std::collections::HashMap;
use std::time::{Duration, Instant};
#[derive(Debug, Clone)]
pub struct NegativeCacheEntry {
pub provider: String,
pub reason: String,
pub expires_at: Instant,
pub metadata: HashMap<String, String>,
}
#[derive(Default)]
pub struct NegativeCache {
entries: HashMap<String, NegativeCacheEntry>,
}
impl NegativeCache {
pub fn make_key(target: &str, provider: &str) -> String {
format!("{provider}::{target}")
}
pub fn should_skip(&self, target: &str, provider: &str) -> bool {
let key = Self::make_key(target, provider);
self.entries
.get(&key)
.map(|entry| entry.expires_at > Instant::now())
.unwrap_or(false)
}
pub fn insert(
&mut self,
target: &str,
provider: &str,
reason: impl Into<String>,
ttl: Duration,
metadata: HashMap<String, String>,
) {
let key = Self::make_key(target, provider);
self.entries.insert(
key,
NegativeCacheEntry {
provider: provider.to_string(),
reason: reason.into(),
expires_at: Instant::now() + ttl,
metadata,
},
);
}
}