34 lines
911 B
Rust
34 lines
911 B
Rust
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);
|
|
}
|
|
}
|
|
|
|
} |