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 --- Cargo.lock | 2 +- angelsharkd/Cargo.toml | 8 ++- angelsharkd/src/routes/extensions/mod.rs | 2 +- angelsharkd/src/routes/extensions/simple_search.rs | 58 +++++++++++++--------- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 255eb5e..cbfb8b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,10 +27,10 @@ name = "angelsharkd" version = "0.2.0" dependencies = [ "anyhow", - "cached", "env_logger", "libangelshark", "log", + "once_cell", "serde", "tokio", "warp", diff --git a/angelsharkd/Cargo.toml b/angelsharkd/Cargo.toml index 8e617e7..d532bf9 100644 --- a/angelsharkd/Cargo.toml +++ b/angelsharkd/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Adam T. Carpenter "] description = "A HTTP interface into one or more Communication Managers" [features] -simple_search = ["cached"] +simple_search = ["once_cell"] simple_deprov = [] [dependencies.libangelshark] @@ -33,8 +33,6 @@ features = ["derive"] [dependencies.anyhow] version = "1" -[dependencies.cached] +[dependencies.once_cell] optional = true -version = "0.26" -default-features = false -features = ["proc_macro"] +version = "1" \ No newline at end of file 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