mirror of
https://github.com/fluencelabs/wasmer
synced 2025-07-31 15:22:03 +00:00
Add versioning to cache
This commit is contained in:
@@ -207,3 +207,14 @@ pub trait Cache {
|
|||||||
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
|
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
|
||||||
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
|
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
/// Returns a unique ID generated from the version of Wasmer for use with cache versioning
|
||||||
|
pub fn cache_versioned_sub_directory() -> String {
|
||||||
|
let mut state = blake2bp::State::new();
|
||||||
|
state.update(WASMER_VERSION.as_bytes());
|
||||||
|
|
||||||
|
let hasher = state.finalize();
|
||||||
|
hasher.to_hex().as_str().to_owned()
|
||||||
|
}
|
||||||
|
@@ -6,7 +6,7 @@ use std::{
|
|||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use wasmer_runtime_core::cache::Error as CacheError;
|
use wasmer_runtime_core::cache::{Error as CacheError, cache_versioned_sub_directory};
|
||||||
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash};
|
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash};
|
||||||
|
|
||||||
/// Representation of a directory that contains compiled wasm artifacts.
|
/// Representation of a directory that contains compiled wasm artifacts.
|
||||||
@@ -36,22 +36,25 @@ pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash};
|
|||||||
/// ```
|
/// ```
|
||||||
pub struct FileSystemCache {
|
pub struct FileSystemCache {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
|
versioned_sub_directory: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileSystemCache {
|
impl FileSystemCache {
|
||||||
/// Construct a new `FileSystemCache` around the specified directory.
|
/// Construct a new `FileSystemCache` around the specified directory.
|
||||||
|
/// The contents of the cache are stored in sub-versioned directories.
|
||||||
///
|
///
|
||||||
/// # Note:
|
/// # Note:
|
||||||
/// This method is unsafe because there's no way to ensure the artifacts
|
/// This method is unsafe because there's no way to ensure the artifacts
|
||||||
/// stored in this cache haven't been corrupted or tampered with.
|
/// stored in this cache haven't been corrupted or tampered with.
|
||||||
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
|
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
|
||||||
let path: PathBuf = path.into();
|
let path: PathBuf = path.into();
|
||||||
|
let versioned_sub_directory = cache_versioned_sub_directory();
|
||||||
|
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
let metadata = path.metadata()?;
|
let metadata = path.metadata()?;
|
||||||
if metadata.is_dir() {
|
if metadata.is_dir() {
|
||||||
if !metadata.permissions().readonly() {
|
if !metadata.permissions().readonly() {
|
||||||
Ok(Self { path })
|
Ok(Self { path, versioned_sub_directory })
|
||||||
} else {
|
} else {
|
||||||
// This directory is readonly.
|
// This directory is readonly.
|
||||||
Err(io::Error::new(
|
Err(io::Error::new(
|
||||||
@@ -72,7 +75,7 @@ impl FileSystemCache {
|
|||||||
} else {
|
} else {
|
||||||
// Create the directory and any parent directories if they don't yet exist.
|
// Create the directory and any parent directories if they don't yet exist.
|
||||||
create_dir_all(&path)?;
|
create_dir_all(&path)?;
|
||||||
Ok(Self { path })
|
Ok(Self { path, versioned_sub_directory })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,6 +87,7 @@ impl Cache for FileSystemCache {
|
|||||||
fn load(&self, key: WasmHash) -> Result<Module, CacheError> {
|
fn load(&self, key: WasmHash) -> Result<Module, CacheError> {
|
||||||
let filename = key.encode();
|
let filename = key.encode();
|
||||||
let mut new_path_buf = self.path.clone();
|
let mut new_path_buf = self.path.clone();
|
||||||
|
new_path_buf.push(&self.versioned_sub_directory);
|
||||||
new_path_buf.push(filename);
|
new_path_buf.push(filename);
|
||||||
let file = File::open(new_path_buf)?;
|
let file = File::open(new_path_buf)?;
|
||||||
let mmap = unsafe { Mmap::map(&file)? };
|
let mmap = unsafe { Mmap::map(&file)? };
|
||||||
@@ -95,6 +99,7 @@ impl Cache for FileSystemCache {
|
|||||||
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> {
|
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> {
|
||||||
let filename = key.encode();
|
let filename = key.encode();
|
||||||
let mut new_path_buf = self.path.clone();
|
let mut new_path_buf = self.path.clone();
|
||||||
|
new_path_buf.push(&self.versioned_sub_directory);
|
||||||
new_path_buf.push(filename);
|
new_path_buf.push(filename);
|
||||||
|
|
||||||
let serialized_cache = module.cache()?;
|
let serialized_cache = module.cache()?;
|
||||||
|
Reference in New Issue
Block a user