Skip to content

Commit

Permalink
remove legacy cache for full_text
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 27, 2024
1 parent 2f101a8 commit 635e1e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
27 changes: 1 addition & 26 deletions src/filter/full_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ use url::Url;
use crate::client::{self, Client};
use crate::feed::{Feed, Post};
use crate::util::convert_relative_url;
use crate::util::TimedLruCache;
use crate::{ConfigError, Error, Result};

use super::html::{KeepElement, KeepElementConfig};
use super::{FeedFilter, FeedFilterConfig, FilterContext};
use crate::feed::NormalizedPost;

type PostCache = TimedLruCache<NormalizedPost, Post>;

const DEFAULT_PARALLELISM: usize = 20;

Expand Down Expand Up @@ -46,7 +42,6 @@ pub struct FullTextFilter {
keep_element: Option<KeepElement>,
simplify: bool,
keep_guid: bool,
post_cache: PostCache,
}

#[async_trait::async_trait]
Expand All @@ -66,10 +61,6 @@ impl FeedFilterConfig for FullTextConfig {
None => None,
Some(c) => Some(c.build().await?),
};
let post_cache = PostCache::new(
conf_client.get_cache_size(),
conf_client.get_cache_ttl(default_cache_ttl),
);

Ok(FullTextFilter {
simplify,
Expand All @@ -78,7 +69,6 @@ impl FeedFilterConfig for FullTextConfig {
append_mode,
keep_guid,
keep_element,
post_cache,
})
}
}
Expand Down Expand Up @@ -151,24 +141,9 @@ impl FullTextFilter {
}
}

async fn fetch_full_post_cached(&self, post: Post) -> Result<Post> {
let normalized_post = post.normalize();
if let Some(result_post) = self.post_cache.get_cached(&normalized_post) {
return Ok(result_post);
};

match self.fetch_full_post(post).await {
Ok(result_post) => {
self.post_cache.insert(normalized_post, result_post.clone());
Ok(result_post)
}
Err(e) => Err(e),
}
}

async fn fetch_all_posts(&self, posts: Vec<Post>) -> Result<Vec<Post>> {
stream::iter(posts)
.map(|post| self.fetch_full_post_cached(post))
.map(|post| self.fetch_full_post(post))
.buffered(self.parallelism)
.collect::<Vec<_>>()
.await
Expand Down
27 changes: 25 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ impl<'a, T> Iterator for SingleOrVecIter<'a, T> {
use std::{
hash::Hash,
num::NonZeroUsize,
sync::RwLock,
sync::{
atomic::{AtomicUsize, Ordering},
RwLock,
},
time::{Duration, Instant},
};

Expand All @@ -98,6 +101,8 @@ pub struct Timed<T> {

pub struct TimedLruCache<K: Hash + Eq, V: Clone> {
map: RwLock<LruCache<K, Timed<V>>>,
misses: AtomicUsize,
hits: AtomicUsize,
timeout: Duration,
}

Expand All @@ -107,16 +112,25 @@ impl<K: Hash + Eq, V: Clone> TimedLruCache<K, V> {
Self {
map: RwLock::new(LruCache::new(max_entries)),
timeout,
misses: AtomicUsize::new(0),
hits: AtomicUsize::new(0),
}
}

pub fn get_cached(&self, key: &K) -> Option<V> {
let mut map = self.map.write().ok()?;
let entry = map.get(key)?;
let Some(entry) = map.get(key) else {
self.misses.fetch_add(1, Ordering::Relaxed);
return None;
};

if entry.created.elapsed() > self.timeout {
self.misses.fetch_add(1, Ordering::Relaxed);
map.pop(key);
return None;
}

self.hits.fetch_add(1, Ordering::Relaxed);
Some(entry.value.clone())
}

Expand All @@ -128,4 +142,13 @@ impl<K: Hash + Eq, V: Clone> TimedLruCache<K, V> {
self.map.write().ok()?.push(key, timed);
Some(())
}

// hit, miss
#[allow(unused)]
pub fn stats(&self) -> (usize, usize) {
(
self.hits.load(Ordering::Relaxed),
self.misses.load(Ordering::Relaxed),
)
}
}

0 comments on commit 635e1e5

Please sign in to comment.