Skip to main content

micromegas_object_cache/
memory_backend.rs

1use async_trait::async_trait;
2use bytes::Bytes;
3use std::collections::HashMap;
4use std::sync::Mutex;
5
6use super::backend::{FillHint, RangeCacheBackend};
7
8pub struct MemoryBackend {
9    data: Mutex<HashMap<String, Bytes>>,
10}
11
12impl MemoryBackend {
13    pub fn new() -> Self {
14        Self {
15            data: Mutex::new(HashMap::new()),
16        }
17    }
18}
19
20impl Default for MemoryBackend {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26#[async_trait]
27impl RangeCacheBackend for MemoryBackend {
28    async fn get(&self, key: &str) -> Option<Bytes> {
29        self.data
30            .lock()
31            .expect("memory backend lock")
32            .get(key)
33            .cloned()
34    }
35
36    async fn put(&self, key: String, value: Bytes, _hint: FillHint) {
37        self.data
38            .lock()
39            .expect("memory backend lock")
40            .insert(key, value);
41    }
42}