From 91d8810402ae8e1173a3c064519c9f1b03e13224 Mon Sep 17 00:00:00 2001 From: "Carpenter, Adam (CORP)" Date: Fri, 26 Nov 2021 16:48:35 -0500 Subject: refactor: simplify by eliminating needless cached for smaller once_cell --- angelsharkd/src/routes/extensions/mod.rs | 2 +- angelsharkd/src/routes/extensions/simple_search.rs | 58 +++++++++++++--------- 2 files changed, 36 insertions(+), 24 deletions(-) (limited to 'angelsharkd/src') diff --git a/angelsharkd/src/routes/extensions/mod.rs b/angelsharkd/src/routes/extensions/mod.rs index defbca9..5628588 100644 --- a/angelsharkd/src/routes/extensions/mod.rs +++ b/angelsharkd/src/routes/extensions/mod.rs @@ -15,7 +15,7 @@ pub fn filter(config: &Config) -> impl Filter = Lazy::new(Haystack::new); -type Terms = Vec; +/// Collection of search terms +type Needle = Vec; pub fn search(config: &Config) -> impl Filter + Clone { let runner = config.runner.clone(); @@ -19,9 +22,9 @@ pub fn search(config: &Config) -> impl Filter()) + .and(json::()) .and(with_runner(runner)) - .and_then(handle_simple_search) + .and_then(handle_search) } pub fn refresh(config: &Config) -> impl Filter + Clone { @@ -32,32 +35,41 @@ pub fn refresh(config: &Config) -> impl Filter Result { + HAYSTACK_CACHE.search(Vec::new()); + Ok("") +} + async fn handle_refresh(runner: AcmRunner) -> Result { - get_extensions_cached(true); + HAYSTACK_CACHE.refresh(); Ok("Refresh scheduled") } -async fn handle_simple_search(terms: Terms, runner: AcmRunner) -> Result { - Ok(get_extensions_cached(false)) +/// A lazy-loaded, asynchronously-refreshed exension-type haystack cache. +struct Haystack { + inner: Arc>, } -#[cached] -fn get_extensions_cached(refresh: bool) -> String { - if refresh { +impl Haystack { + fn new() -> Self { + Self { + inner: Arc::new(Mutex::new(String::with_capacity(0))), + } + } + + pub fn search(&self, needle: Needle) -> String { + (*self.inner.lock().unwrap()).clone() + } + + pub fn refresh(&self) { + let inner = self.inner.clone(); tokio::spawn(async move { - std::thread::sleep_ms(10000); + std::thread::sleep_ms(10000); // slow generation here - if let Ok(mut handle) = GET_EXTENSIONS_CACHED.lock() { - handle.cache_clear(); - handle.cache_set(false, String::from("fresh")); + if let Ok(mut handle) = inner.lock() { + *handle = String::from("fresh"); eprintln!("evicted"); } }); } - - String::from("stale") -} - -fn get_extensions(runner: AcmRunner) { - todo!() } -- cgit v1.2.3