clean cache, handled warnings etc
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
use std::time::SystemTime;
|
||||
use std::time::{SystemTime};
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use crate::videos::Video_Item;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
use crate::videos::VideoItem;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VideoCache{
|
||||
cache: Arc<Mutex<std::collections::HashMap<String, (SystemTime, Vec<Video_Item>)>>>, // url -> time+Items
|
||||
cache: Arc<Mutex<std::collections::HashMap<String, (SystemTime, Vec<VideoItem>)>>>, // url -> time+Items
|
||||
}
|
||||
impl VideoCache {
|
||||
pub fn new() -> Self {
|
||||
@@ -14,21 +17,44 @@ impl VideoCache {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &str) -> Option<(SystemTime, Vec<Video_Item>)> {
|
||||
pub fn get(&self, key: &str) -> Option<(SystemTime, Vec<VideoItem>)> {
|
||||
let cache = self.cache.lock().ok()?;
|
||||
cache.get(key).cloned()
|
||||
}
|
||||
|
||||
pub fn insert(&self, key: String, value: Vec<Video_Item>) {
|
||||
pub fn insert(&self, key: String, value: Vec<VideoItem>) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
pub fn entries(&self) -> Option<Vec<(String, (SystemTime, Vec<VideoItem>))>> {
|
||||
if let Ok(cache) = self.cache.lock() {
|
||||
// Return a cloned vector of the cache entries
|
||||
return Some(cache.iter().map(|(k, v)| (k.clone(), v.clone())).collect());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn check(&self) -> Result<(), Box<dyn std::error::Error>>{
|
||||
let iter = match self.entries(){
|
||||
Some(iter) => iter,
|
||||
None => return Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Could not get entries")))
|
||||
};
|
||||
|
||||
for (key, (time, _items)) in iter {
|
||||
if let Ok(elapsed) = time.elapsed() {
|
||||
if elapsed > Duration::from_secs(60*60){
|
||||
println!("Key: {}, elapsed: {:?}", key, elapsed);
|
||||
self.remove(&key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user