diff options
Diffstat (limited to 'plugins/warp/src/cache/container.rs')
| -rw-r--r-- | plugins/warp/src/cache/container.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/plugins/warp/src/cache/container.rs b/plugins/warp/src/cache/container.rs new file mode 100644 index 00000000..59b31bad --- /dev/null +++ b/plugins/warp/src/cache/container.rs @@ -0,0 +1,28 @@ +use crate::container::Container; +use dashmap::DashMap; +use std::ops::Deref; +use std::sync::{Arc, OnceLock, RwLock}; + +pub static CONTAINER_CACHE: OnceLock<DashMap<String, Arc<RwLock<Box<dyn Container>>>>> = + OnceLock::new(); + +pub fn for_cached_containers(f: impl Fn(&dyn Container)) { + let containers_cache = CONTAINER_CACHE.get_or_init(Default::default); + for container in containers_cache.iter() { + if let Ok(guarded_container) = container.read() { + f(guarded_container.deref().as_ref()); + } + } +} + +// TODO: The static lifetime here is a little wierd... (we need it to Box) +pub fn add_cached_container(container: impl Container + 'static) { + let containers_cache = CONTAINER_CACHE.get_or_init(Default::default); + let container_name = container.to_string(); + containers_cache.insert(container_name, Arc::new(RwLock::new(Box::new(container)))); +} + +pub fn cached_containers() -> Vec<Arc<RwLock<Box<dyn Container>>>> { + let containers_cache = CONTAINER_CACHE.get_or_init(Default::default); + containers_cache.iter().map(|c| c.clone()).collect() +} |
