Skip to content

Commit

Permalink
feed-level cache
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 27, 2024
1 parent 3d7efec commit f21dd28
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/feed/norm.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use chrono::{DateTime, FixedOffset};
use serde::Serialize;

#[derive(Debug, Serialize, Hash, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Serialize, Hash, PartialEq, Eq, Default)]
pub struct NormalizedFeed {
pub title: String,
pub link: String,
pub description: Option<String>,
pub posts: Vec<NormalizedPost>,
}

#[derive(Debug, Serialize, PartialEq, Eq, Hash, Default)]
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash, Default)]
pub struct NormalizedPost {
pub title: String,
pub author: Option<String>,
Expand Down
39 changes: 21 additions & 18 deletions src/filter_pipeline/filter_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ use crate::{
};

pub struct FilterCache {
feed_cache: FeedCache,
post_cache: PostCache,
feed_cache: TimedLruCache<NormalizedFeed, Feed>,
// WIP
#[expect(unused)]
post_cache: TimedLruCache<NormalizedPost, Option<Post>>,
}

impl FilterCache {
pub fn new() -> Self {
Self {
feed_cache: FeedCache {
cache: TimedLruCache::new(1, Duration::from_secs(12 * 3600)),
},
post_cache: PostCache {
cache: TimedLruCache::new(40, Duration::from_secs(3600)),
},
feed_cache: TimedLruCache::new(1, Duration::from_secs(12 * 3600)),
post_cache: TimedLruCache::new(40, Duration::from_secs(3600)),
}
}

Expand All @@ -30,16 +28,21 @@ impl FilterCache {
F: FnOnce(Feed) -> Fut,
Fut: Future<Output = Result<Feed>>,
{
f(feed).await
}
}
let normalized_feed = feed.normalize();
if let Some(output_feed) = self.feed_cache.get_cached(&normalized_feed) {
return Ok(output_feed);
}

struct PostCache {
// input -> output
cache: TimedLruCache<NormalizedPost, Post>,
}
match f(feed).await {
Ok(output_feed) => {
self.register(normalized_feed, output_feed.clone());
Ok(output_feed)
}
Err(e) => Err(e),
}
}

struct FeedCache {
// input -> output
cache: TimedLruCache<NormalizedFeed, Feed>,
fn register(&self, input_feed: NormalizedFeed, output_feed: Feed) {
self.feed_cache.insert(input_feed, output_feed);
}
}

0 comments on commit f21dd28

Please sign in to comment.