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
use crate::config::Config;

use humphrey::http::mime::MimeType;
use std::{collections::VecDeque, time::SystemTime};

/// Represents the server's cache.
#[derive(Default)]
pub struct Cache {
    pub cache_limit: usize,
    cache_time_limit: u64,
    cache_size: usize,
    data: VecDeque<CachedItem>,
}

/// Represents a cached item.
pub struct CachedItem {
    pub route: String,
    pub mime_type: MimeType,
    pub cache_time: u64,
    pub data: Vec<u8>,
}

impl Cache {
    /// Attempts to get an item from the cache.
    /// If the item is not present, or it is stale, returns `None`.
    pub fn get(&self, route: &str) -> Option<&CachedItem> {
        let time = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let index = self.data.iter().position(|item| item.route == route);

        if let Some(index) = index {
            let item = &self.data[index];
            if time - item.cache_time > self.cache_time_limit {
                None
            } else {
                Some(item)
            }
        } else {
            None
        }
    }

    /// Sets an item in the cache.
    /// Overwrites older versions if needed.
    pub fn set(&mut self, route: &str, value: Vec<u8>, mime_type: MimeType) {
        while self.cache_size + value.len() > self.cache_limit {
            self.cache_size -= self.data[0].data.len();
            self.data.pop_front();
        }

        if let Some(existing_item) = self.data.iter().position(|item| item.route == route) {
            self.cache_size -= self.data[existing_item].data.len();
            self.data.remove(existing_item);
        }

        self.cache_size += value.len();

        self.data.push_back(CachedItem {
            route: route.into(),
            data: value,
            mime_type,
            cache_time: SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        });
    }
}

impl From<&Config> for Cache {
    fn from(config: &Config) -> Self {
        Self {
            cache_limit: config.cache_limit,
            cache_time_limit: config.cache_time_limit,
            cache_size: 0,
            data: VecDeque::new(),
        }
    }
}