From 7ad3accf82249a81ab7dc8bf585e488ce01a576d Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 3 Nov 2022 16:50:40 +0100 Subject: [PATCH] redis_store: expire posts --- src/main.rs | 2 +- src/redis_store.rs | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6a9f847..885286e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ async fn main() { for host in config.hosts.into_iter() { scheduler.introduce(host); } - + let client = reqwest::Client::builder() .timeout(Duration::from_secs(30)) .user_agent(concat!( diff --git a/src/redis_store.rs b/src/redis_store.rs index e10b9eb..e28b6f8 100644 --- a/src/redis_store.rs +++ b/src/redis_store.rs @@ -1,13 +1,20 @@ use crate::feed::Post; +const POST_EXPIRE: usize = 86400; + pub async fn save_post(man: &mut redis::aio::ConnectionManager, host: &str, post: Post) -> bool { - if redis::Cmd::getset(&format!("p:{}", post.url), "1") - .query_async::<_, Option>>(man) - .await.unwrap() - .is_some() { - // post is not new - return false; - } + let post_key = format!("p:{}", post.url); + let check = redis::pipe() + .getset(&post_key, "1") + .expire(post_key, POST_EXPIRE) + .ignore() + .query_async::<_, redis::Value>(man) + .await + .unwrap(); + if check != redis::Value::Bulk(vec![redis::Value::Nil]) { + // post is not new + return false; + } if let Some(author_host) = post.account.host() { if author_host == host && post.url_host().map(|s| s == host).unwrap_or(false) {