caching
This commit is contained in:
34
src/util/cache.rs
Normal file
34
src/util/cache.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user