Trait wasmer_wasix::runtime::module_cache::ModuleCache

source ·
pub trait ModuleCache: Debug {
    // Required methods
    fn load<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: ModuleHash,
        engine: &'life1 Engine
    ) -> Pin<Box<dyn Future<Output = Result<Module, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn save<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        key: ModuleHash,
        engine: &'life1 Engine,
        module: &'life2 Module
    ) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;

    // Provided method
    fn with_fallback<C>(self, other: C) -> FallbackCache<Self, C>
       where Self: Sized,
             C: ModuleCache { ... }
}
Expand description

A cache for compiled WebAssembly modules.

§Deterministic ID

Implementations are encouraged to take the Engine::deterministic_id() into account when saving and loading cached a Module.

§Assumptions

Implementations can assume that cache keys are unique and that using the same key to load or save will always result in the “same” module.

Implementations can also assume that ModuleCache::load() will be called more often than ModuleCache::save() and optimise their caching strategy accordingly.

Required Methods§

source

fn load<'life0, 'life1, 'async_trait>( &'life0 self, key: ModuleHash, engine: &'life1 Engine ) -> Pin<Box<dyn Future<Output = Result<Module, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Load a module based on its hash.

source

fn save<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: ModuleHash, engine: &'life1 Engine, module: &'life2 Module ) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Save a module so it can be retrieved with ModuleCache::load() at a later time.

§Panics

Implementations are free to assume the Module being passed in was compiled using the provided Engine, and may panic if this isn’t the case.

Provided Methods§

source

fn with_fallback<C>(self, other: C) -> FallbackCache<Self, C>
where Self: Sized, C: ModuleCache,

Chain a second ModuleCache that will be used as a fallback if lookups on the primary cache fail.

The general assumption is that each subsequent cache in the chain will be significantly slower than the previous one.

use wasmer_wasix::runtime::module_cache::{
    ModuleCache, ThreadLocalCache, FileSystemCache, SharedCache,
};
use wasmer_wasix::runtime::task_manager::tokio::{RuntimeOrHandle, TokioTaskManager};

let runtime = tokio::runtime::Runtime::new().unwrap();
let rt_handle = RuntimeOrHandle::from(runtime);
let task_manager = std::sync::Arc::new(TokioTaskManager::new(rt_handle));

let cache = SharedCache::default()
    .with_fallback(FileSystemCache::new("~/.local/cache", task_manager));

Implementors§

source§

impl ModuleCache for FileSystemCache

Available on crate feature sys-thread only.
source§

impl ModuleCache for SharedCache

source§

impl ModuleCache for ThreadLocalCache

source§

impl<D, C> ModuleCache for D
where D: Deref<Target = C> + Debug + Send + Sync, C: ModuleCache + Send + Sync + ?Sized,

source§

impl<Primary, Fallback> ModuleCache for FallbackCache<Primary, Fallback>
where Primary: ModuleCache + Send + Sync, Fallback: ModuleCache + Send + Sync,