This commit is contained in:
Simon
2025-06-04 07:35:55 +00:00
parent 8d5da3a4dc
commit 3150e57411
7 changed files with 105 additions and 12 deletions

34
src/util/cache.rs Normal file
View File

@@ -0,0 +1,34 @@
use std::time::SystemTime;
use std::sync::{Arc, Mutex};
use crate::videos::Video_Item;
#[derive(Clone)]
pub struct VideoCache{
cache: Arc<Mutex<std::collections::HashMap<String, (SystemTime, Vec<Video_Item>)>>>, // url -> time+Items
}
impl VideoCache {
pub fn new() -> Self {
VideoCache {
cache: Arc::new(Mutex::new(std::collections::HashMap::new())),
}
}
pub fn get(&self, key: &str) -> Option<(SystemTime, Vec<Video_Item>)> {
let cache = self.cache.lock().ok()?;
cache.get(key).cloned()
}
pub fn insert(&self, key: String, value: Vec<Video_Item>) {
if let Ok(mut cache) = self.cache.lock() {
cache.insert(key.clone(), (SystemTime::now(), value.clone()));
}
}
pub fn remove(&self, key: &str) {
if let Ok(mut cache) = self.cache.lock() {
cache.remove(key);
}
}
}