Struct Foyer
pub struct Foyer { /* private fields */ }services-foyer only.Expand description
foyer backend support.
§Foyer Service
foyer is a high-performance hybrid cache library that supports both in-memory and on-disk caching.
This service provides foyer as a volatile KV storage backend. Data stored may be evicted when the cache is full.
§Capabilities
This service can be used to:
- stat
- read
- write
- delete
- list (not supported)
- blocking (not supported)
§Configuration
Foyer service can be configured in two ways:
- Pre-configured cache: Pass a fully configured
HybridCacheinstance for maximum control - Auto-configured cache: Use builder methods to configure memory and disk caching
§Auto-configured Cache Options
When using the builder API without providing a pre-built cache, the following options are available:
memory: Memory cache capacity (default: 1 GiB)disk_path: Directory path for disk cache (enables hybrid caching)disk_capacity: Total disk cache capacitydisk_file_size: Individual cache file size (default: 1 MiB)recover_mode: Recovery mode on startup - “none” (default), “quiet”, or “strict”shards: Number of cache shards for concurrency (default: 1)
§Example
§Via Pre-configured Cache
use opendal::Operator;
use opendal_service_foyer::Foyer;
use opendal_service_foyer::FoyerKey;
use opendal_service_foyer::FoyerValue;
use foyer::{HybridCacheBuilder, Engine};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a foyer HybridCache with full control
let cache = HybridCacheBuilder::new()
.memory(64 * 1024 * 1024) // 64MB memory cache
.storage(Engine::Large(Default::default()))
.build()
.await?;
// Create operator
let op = Operator::new(Foyer::new().cache(cache))?
.finish();
// Use it like any other OpenDAL operator
op.write("key", "value").await?;
let value = op.read("key").await?;
Ok(())
}§Via Auto-configuration (Hybrid Cache)
use opendal::Operator;
use opendal_service_foyer::Foyer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create operator with hybrid cache (memory + disk)
let op = Operator::new(
Foyer::new()
.memory(64 * 1024 * 1024) // 64MB memory
.disk_path("/tmp/foyer_cache") // Enable disk cache
.disk_capacity(1024 * 1024 * 1024) // 1GB disk
.disk_file_size(1024 * 1024) // 1MB per file
.recover_mode("quiet") // Recover on restart
.shards(4) // 4 shards for concurrency
)?
.finish();
op.write("key", "value").await?;
let value = op.read("key").await?;
Ok(())
}§Via URI
use opendal::Operator;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Memory-only cache via URI
let op = Operator::from_uri("foyer:///?memory=67108864")?;
// Hybrid cache via URI
let op = Operator::from_uri(
"foyer:///cache?memory=67108864&disk_path=/tmp/foyer&disk_capacity=1073741824"
)?;
Ok(())
}§Notes
- Data Volatility: Foyer is a cache, not persistent storage. Data may be evicted at any time when the cache reaches its capacity limit.
- Hybrid Caching: When
disk_pathis configured, cold data automatically moves from memory to disk as the memory cache fills up. - Recovery Modes:
"none": Don’t restore data from disk on restart (volatile cache)"quiet": Restore data and skip any corrupted entries"strict": Restore data and fail on any corruption
- No List Support: Foyer does not support efficient key iteration, so the
listoperation is not available. - Async Only: Foyer is async-only, blocking operations are not supported.
Implementations§
§impl FoyerBuilder
impl FoyerBuilder
pub fn new() -> FoyerBuilder
pub fn new() -> FoyerBuilder
Create a new FoyerBuilder with default settings.
The cache will be lazily initialized when first accessed if not provided via Self::cache.
§Example
use opendal_service_foyer::Foyer;
let builder = Foyer::new()
.memory(64 * 1024 * 1024)
.root("/cache");pub fn name(self, name: &str) -> FoyerBuilder
pub fn name(self, name: &str) -> FoyerBuilder
Set the name of this cache instance.
pub fn cache(self, cache: HybridCache<FoyerKey, FoyerValue>) -> FoyerBuilder
pub fn cache(self, cache: HybridCache<FoyerKey, FoyerValue>) -> FoyerBuilder
Set a pre-built [foyer::HybridCache] instance.
If provided, this cache will be used directly. Otherwise, a cache will be lazily initialized using the configured memory size.
§Example
use opendal_service_foyer::Foyer;
use foyer::{HybridCacheBuilder, Engine};
let cache = HybridCacheBuilder::new()
.memory(64 * 1024 * 1024)
.storage(Engine::Large(Default::default()))
.build()
.await?;
let builder = Foyer::new().cache(cache);pub fn root(self, path: &str) -> FoyerBuilder
pub fn root(self, path: &str) -> FoyerBuilder
Set the root path of this backend.
All operations will be relative to this root path.
pub fn memory(self, size: usize) -> FoyerBuilder
pub fn memory(self, size: usize) -> FoyerBuilder
Set the memory capacity in bytes for the cache.
This is used when the cache is lazily initialized (i.e., when no pre-built cache
is provided via Self::cache).
Default is 1 GiB (1024 * 1024 * 1024 bytes).
pub fn disk_path(self, path: &str) -> FoyerBuilder
pub fn disk_path(self, path: &str) -> FoyerBuilder
Set the disk cache directory path.
Enables hybrid cache with disk storage. When memory cache is full, data will be persisted to this directory.
pub fn disk_capacity(self, size: usize) -> FoyerBuilder
pub fn disk_capacity(self, size: usize) -> FoyerBuilder
Set the disk cache total capacity in bytes.
Only used when disk_path is set.
pub fn disk_file_size(self, size: usize) -> FoyerBuilder
pub fn disk_file_size(self, size: usize) -> FoyerBuilder
Set the individual cache file size in bytes.
Default is 1 MiB (1024 * 1024 bytes).
Only used when disk_path is set.
pub fn recover_mode(self, mode: &str) -> FoyerBuilder
pub fn recover_mode(self, mode: &str) -> FoyerBuilder
Set the recovery mode when starting the cache.
Valid values: “none” (default), “quiet”, “strict”.
- “none”: Don’t recover from disk
- “quiet”: Recover and skip errors
- “strict”: Recover and panic on errors
pub fn shards(self, count: usize) -> FoyerBuilder
pub fn shards(self, count: usize) -> FoyerBuilder
Set the number of shards for concurrent access.
Default is 1. Higher values improve concurrency but increase overhead.
Trait Implementations§
§impl Builder for FoyerBuilder
impl Builder for FoyerBuilder
§impl Debug for FoyerBuilder
impl Debug for FoyerBuilder
§impl Default for FoyerBuilder
impl Default for FoyerBuilder
§fn default() -> FoyerBuilder
fn default() -> FoyerBuilder
Auto Trait Implementations§
impl Freeze for FoyerBuilder
impl !RefUnwindSafe for FoyerBuilder
impl Send for FoyerBuilder
impl Sync for FoyerBuilder
impl Unpin for FoyerBuilder
impl !UnwindSafe for FoyerBuilder
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> Scope for T
impl<T> Scope for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.