max size of 100k for fast cache

This commit is contained in:
Simon
2025-12-08 07:12:20 +00:00
parent b986faa1d4
commit 9789afb12b
2 changed files with 37 additions and 19 deletions

View File

@@ -49,7 +49,7 @@ async fn main() -> std::io::Result<()> {
let mut requester = util::requester::Requester::new(); let mut requester = util::requester::Requester::new();
requester.set_proxy(env::var("PROXY").unwrap_or("0".to_string()) != "0".to_string()); requester.set_proxy(env::var("PROXY").unwrap_or("0".to_string()) != "0".to_string());
let cache: util::cache::VideoCache = crate::util::cache::VideoCache::new(); let cache: util::cache::VideoCache = crate::util::cache::VideoCache::new().max_size(100_000).to_owned();
thread::spawn(move || { thread::spawn(move || {
// Create a tiny runtime just for these async tasks // Create a tiny runtime just for these async tasks

View File

@@ -1,22 +1,28 @@
use std::time::{SystemTime}; use std::time::SystemTime;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use crate::videos::VideoItem; use crate::videos::VideoItem;
#[derive(Clone)] #[derive(Clone)]
pub struct VideoCache { pub struct VideoCache {
cache: Arc<Mutex<std::collections::HashMap<String, (SystemTime, Vec<VideoItem>)>>>, // url -> time+Items cache: Arc<Mutex<std::collections::HashMap<String, (SystemTime, Vec<VideoItem>)>>>, // url -> time+Items
max_size: usize,
} }
impl VideoCache { impl VideoCache {
pub fn new() -> Self { pub fn new() -> Self {
VideoCache { VideoCache {
cache: Arc::new(Mutex::new(std::collections::HashMap::new())), cache: Arc::new(Mutex::new(std::collections::HashMap::new())),
max_size: 100,
} }
} }
pub fn max_size(&mut self, size: usize) -> &mut Self {
self.max_size = size;
self
}
pub fn get(&self, key: &str) -> Option<(SystemTime, Vec<VideoItem>)> { pub fn get(&self, key: &str) -> Option<(SystemTime, Vec<VideoItem>)> {
let cache = self.cache.lock().ok()?; let cache = self.cache.lock().ok()?;
cache.get(key).cloned() cache.get(key).cloned()
@@ -24,14 +30,22 @@ impl VideoCache {
pub fn insert(&self, key: String, value: Vec<VideoItem>) { pub fn insert(&self, key: String, value: Vec<VideoItem>) {
if let Ok(mut cache) = self.cache.lock() { if let Ok(mut cache) = self.cache.lock() {
if cache.len() >= self.max_size {
// Simple eviction policy: remove a random entry
if let Some(first_key) = cache.keys().next().cloned() {
cache.remove(&first_key);
}
}
cache.insert(key.clone(), (SystemTime::now(), value.clone())); cache.insert(key.clone(), (SystemTime::now(), value.clone()));
} }
} }
pub fn remove(&self, key: &str) { pub fn remove(&self, key: &str) {
if let Ok(mut cache) = self.cache.lock() { if let Ok(mut cache) = self.cache.lock() {
cache.remove(key); cache.remove(key);
} }
} }
pub fn entries(&self) -> Option<Vec<(String, (SystemTime, Vec<VideoItem>))>> { pub fn entries(&self) -> Option<Vec<(String, (SystemTime, Vec<VideoItem>))>> {
if let Ok(cache) = self.cache.lock() { if let Ok(cache) = self.cache.lock() {
// Return a cloned vector of the cache entries // Return a cloned vector of the cache entries
@@ -43,7 +57,12 @@ impl VideoCache {
pub async fn check(&self) -> Result<(), Box<dyn std::error::Error>> { pub async fn check(&self) -> Result<(), Box<dyn std::error::Error>> {
let iter = match self.entries() { let iter = match self.entries() {
Some(iter) => iter, Some(iter) => iter,
None => return Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Could not get entries"))) None => {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not get entries",
)));
}
}; };
for (key, (time, _items)) in iter { for (key, (time, _items)) in iter {
@@ -55,5 +74,4 @@ impl VideoCache {
} }
Ok(()) Ok(())
} }
} }