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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Provides caching functionality.

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 {
    /// The cache's maximum size.
    pub cache_limit: usize,
    cache_time_limit: u64,
    cache_size: usize,
    data: VecDeque<CachedItem>,
}

/// Represents a cached item.
pub struct CachedItem {
    /// The route that this item was served at.
    pub route: String,
    /// The host that this item was served at.
    pub host: usize,
    /// The MIME type of the item.
    pub mime_type: MimeType,
    /// The time at which the item was cached.
    pub cache_time: u64,
    /// The item's data.
    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, host: usize) -> Option<&CachedItem> {
        let time = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();

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

        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, host: usize, 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 && item.host == host)
        {
            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(),
            host,
            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.size_limit,
            cache_time_limit: config.cache.time_limit as u64,
            cache_size: 0,
            data: VecDeque::new(),
        }
    }
}