wick_wasm_engine/
store.rs

1use std::collections::HashMap;
2use std::path::{Path, PathBuf};
3
4use parking_lot::Mutex;
5use wasmtime::component::Component;
6use wasmtime::Error;
7
8static MODULE_CACHE: once_cell::sync::Lazy<Mutex<HashMap<PathBuf, Component>>> =
9  once_cell::sync::Lazy::new(|| Mutex::new(HashMap::new()));
10
11fn get_cached_component(path: &Path) -> Option<Component> {
12  let cache = MODULE_CACHE.lock();
13  cache.get(path).cloned()
14}
15
16pub async fn fetch_component(path: &Path) -> Result<Component, Error> {
17  if let Some(lock) = get_cached_component(path) {
18    return Ok(lock);
19  }
20
21  let module_bytes = tokio::fs::read(path.clone()).await?;
22
23  let component = Component::from_binary(crate::wasm_engine(), &module_bytes)?;
24
25  let mut cache = MODULE_CACHE.lock();
26  cache.insert(path.to_path_buf(), component.clone());
27  Ok(component)
28}