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
extern crate ahash;
use ahash::AHashMap;
pub struct Item{
pub value: String,
pub expiry: u32,
pub priority: u32,
next_expiry: Option<Box<Item>>,
next_priority: Option<Box<Item>>,
prev_expiry: Option<Box<Item>>,
prev_priority: Option<Box<Item>>,
}
pub struct Cache(AHashMap<String, Item>);
impl Cache {
pub fn new(capacity:usize) ->Self{
assert!(capacity>0);
Self(AHashMap::with_capacity(capacity))
}
pub fn get(&self, key: &str) -> Option<&Item> {
self.0.get(key)
}
pub fn set(&mut self, key: &str, value: &str, expiry:u32, priority:u32) {
self.0.insert(key.to_string(), Item{
value: value.to_string(),
expiry,
priority,
next_expiry: None,
next_priority: None,
prev_expiry: None,
prev_priority: None,
});
}
pub fn evict(&mut self) {
println!("evict");
}
}