From fa253d103c0f11e1a403342b0de7ef9c14bbcbf2 Mon Sep 17 00:00:00 2001 From: shouya <526598+shouya@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:03:43 +0900 Subject: [PATCH] move TimedLruCache to util module --- src/cache.rs | 47 ---------------------------------------- src/client/cache.rs | 4 +--- src/filter/full_text.rs | 2 +- src/main.rs | 1 - src/util.rs | 48 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 52 deletions(-) delete mode 100644 src/cache.rs diff --git a/src/cache.rs b/src/cache.rs deleted file mode 100644 index fb32e4a..0000000 --- a/src/cache.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::{ - hash::Hash, - num::NonZeroUsize, - sync::RwLock, - time::{Duration, Instant}, -}; - -use lru::LruCache; - -pub struct Timed { - value: T, - created: Instant, -} - -pub struct TimedLruCache { - map: RwLock>>, - timeout: Duration, -} - -impl TimedLruCache { - pub fn new(max_entries: usize, timeout: Duration) -> Self { - let max_entries = max_entries.try_into().unwrap_or(NonZeroUsize::MIN); - Self { - map: RwLock::new(LruCache::new(max_entries)), - timeout, - } - } - - pub fn get_cached(&self, key: &K) -> Option { - let mut map = self.map.write().ok()?; - let entry = map.get(key)?; - if entry.created.elapsed() > self.timeout { - map.pop(key); - return None; - } - Some(entry.value.clone()) - } - - pub fn insert(&self, key: K, value: V) -> Option<()> { - let timed = Timed { - value, - created: Instant::now(), - }; - self.map.write().ok()?.push(key, timed); - Some(()) - } -} diff --git a/src/client/cache.rs b/src/client/cache.rs index 64d0120..ba5bc9c 100644 --- a/src/client/cache.rs +++ b/src/client/cache.rs @@ -1,12 +1,10 @@ use std::sync::Arc; -use crate::cache::TimedLruCache; - use mime::Mime; use reqwest::header::HeaderMap; use url::Url; -use crate::{Error, Result}; +use crate::{util::TimedLruCache, Error, Result}; pub type ResponseCache = TimedLruCache; diff --git a/src/filter/full_text.rs b/src/filter/full_text.rs index f533276..52f9999 100644 --- a/src/filter/full_text.rs +++ b/src/filter/full_text.rs @@ -7,10 +7,10 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use url::Url; -use crate::cache::TimedLruCache; use crate::client::{self, Client}; use crate::feed::{Feed, Post}; use crate::html::convert_relative_url; +use crate::util::TimedLruCache; use crate::{ConfigError, Error, Result}; use super::html::{KeepElement, KeepElementConfig}; diff --git a/src/main.rs b/src/main.rs index d23e320..d4268b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -mod cache; mod cli; mod client; mod config; diff --git a/src/util.rs b/src/util.rs index 01b703e..54d6c8a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -77,3 +77,51 @@ impl<'a, T> Iterator for SingleOrVecIter<'a, T> { } } } + +use std::{ + hash::Hash, + num::NonZeroUsize, + sync::RwLock, + time::{Duration, Instant}, +}; + +use lru::LruCache; + +pub struct Timed { + value: T, + created: Instant, +} + +pub struct TimedLruCache { + map: RwLock>>, + timeout: Duration, +} + +impl TimedLruCache { + pub fn new(max_entries: usize, timeout: Duration) -> Self { + let max_entries = max_entries.try_into().unwrap_or(NonZeroUsize::MIN); + Self { + map: RwLock::new(LruCache::new(max_entries)), + timeout, + } + } + + pub fn get_cached(&self, key: &K) -> Option { + let mut map = self.map.write().ok()?; + let entry = map.get(key)?; + if entry.created.elapsed() > self.timeout { + map.pop(key); + return None; + } + Some(entry.value.clone()) + } + + pub fn insert(&self, key: K, value: V) -> Option<()> { + let timed = Timed { + value, + created: Instant::now(), + }; + self.map.write().ok()?.push(key, timed); + Some(()) + } +}