use std::collections::HashMap;
use std::sync::Mutex;
pub struct ThumbnailCache {
entries: Mutex<HashMap<String, Vec<u8>>>,
}
impl ThumbnailCache {
pub fn new() -> Self {
Self {
entries: Mutex::new(HashMap::new()),
}
}
fn key(mxc_uri: &str, width: u32, height: u32) -> String {
format!("{mxc_uri}_{width}x{height}")
}
pub fn get(&self, mxc_uri: &str, width: u32, height: u32) -> Option<Vec<u8>> {
let key = Self::key(mxc_uri, width, height);
self.entries.lock().ok()?.get(&key).cloned()
}
pub fn insert(&self, mxc_uri: &str, width: u32, height: u32, data: Vec<u8>) {
let key = Self::key(mxc_uri, width, height);
if let Ok(mut entries) = self.entries.lock() {
entries.insert(key, data);
}
}
}
impl Default for ThumbnailCache {
fn default() -> Self {
Self::new()
}
}