From 6dd3236069d5cbdd6cb946b408cd6efb7e91d0f9 Mon Sep 17 00:00:00 2001 From: "Carpenter, Adam (CORP)" Date: Fri, 26 Nov 2021 15:58:13 -0500 Subject: feat: impl lazy-refreshing cache for quick access to be used for haystack --- angelsharkd/src/routes/extensions/mod.rs | 4 +- angelsharkd/src/routes/extensions/simple_search.rs | 54 +++++++++++++++++++--- angelsharkd/src/routes/mod.rs | 1 - 3 files changed, 51 insertions(+), 8 deletions(-) (limited to 'angelsharkd/src/routes') diff --git a/angelsharkd/src/routes/extensions/mod.rs b/angelsharkd/src/routes/extensions/mod.rs index 5dac1cf..defbca9 100644 --- a/angelsharkd/src/routes/extensions/mod.rs +++ b/angelsharkd/src/routes/extensions/mod.rs @@ -13,7 +13,9 @@ pub fn filter(config: &Config) -> impl Filter impl Filter + Clone { - path("search").and(post()).and_then(handle_simple_search) +type Terms = Vec; + +pub fn search(config: &Config) -> impl Filter + Clone { + let runner = config.runner.clone(); + // TODO: anti-caching headers on resp? + + path("search") + .and(post()) + .and(content_length_limit(1024 * 16)) + .and(json::()) + .and(with_runner(runner)) + .and_then(handle_simple_search) } -async fn handle_simple_search() -> Result { - Ok("Search!") +pub fn refresh() -> impl Filter + Clone { + path!("search" / "refresh") + .and(get()) + .and_then(handle_refresh) +} + +async fn handle_refresh() -> Result { + fetch(String::from("test"), true); + Ok("") +} + +async fn handle_simple_search(terms: Terms, runner: AcmRunner) -> Result { + Ok(fetch(terms[0].clone(), false)) +} + +#[cached] +fn fetch(param: String, refresh: bool) -> String { + if refresh { + let param = param.clone(); + tokio::spawn(async move { + std::thread::sleep_ms(10000); + let mut handle = FETCH.lock().unwrap(); + handle.cache_clear(); + handle.cache_set((param.clone(), false), format!("blargh {}", param.clone())); + eprintln!("evicted"); + }); + } + + param } diff --git a/angelsharkd/src/routes/mod.rs b/angelsharkd/src/routes/mod.rs index 52373d6..cee0657 100644 --- a/angelsharkd/src/routes/mod.rs +++ b/angelsharkd/src/routes/mod.rs @@ -13,7 +13,6 @@ use warp::{ }; mod dtos; -#[cfg(feature = "extensions")] pub mod extensions; /// GET / -> Name and version # of app. -- cgit v1.2.3