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;

/// 0.1.1 This code is there just to expose the interface.
/// the implementation is not complete.


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>>,
}

// TODO: check existing implementation for binary tree and doubly linked list
// TODO: if they can be fit for purpose.
// TODO: add the primitives in the data structures module

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)
        // set this item as most recently used on the double linked leaf of the time binary tree
        // set this item as most recently used on the double linked leaf of the priority binary tree
    }
    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");
    }
}