Skip to content

Commit

Permalink
feat: support root path for moka and mini-moka (#4984)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorgan authored Aug 8, 2024
1 parent ccfef2b commit 5727752
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
17 changes: 13 additions & 4 deletions core/src/services/dashmap/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ pub struct DashmapBuilder {
impl DashmapBuilder {
/// Set the root for dashmap.
pub fn root(mut self, path: &str) -> Self {
self.config.root = Some(path.into());
self.config.root = if path.is_empty() {
None
} else {
Some(path.to_string())
};

self
}
}
Expand All @@ -60,10 +65,14 @@ impl Builder for DashmapBuilder {
type Config = DashmapConfig;

fn build(self) -> Result<impl Access> {
Ok(DashmapBackend::new(Adapter {
let mut backend = DashmapBackend::new(Adapter {
inner: DashMap::default(),
})
.with_root(self.config.root.as_deref().unwrap_or_default()))
});
if let Some(v) = self.config.root {
backend = backend.with_root(&v);
}

Ok(backend)
}
}

Expand Down
23 changes: 21 additions & 2 deletions core/src/services/mini_moka/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct MiniMokaConfig {
///
/// Refer to [`mini-moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.time_to_idle)
pub time_to_idle: Option<Duration>,

/// root path of this backend
pub root: Option<String>,
}

impl Configurator for MiniMokaConfig {
Expand Down Expand Up @@ -91,6 +94,17 @@ impl MiniMokaBuilder {
}
self
}

/// Set root path of this backend
pub fn root(mut self, path: &str) -> Self {
self.config.root = if path.is_empty() {
None
} else {
Some(path.to_string())
};

self
}
}

impl Builder for MiniMokaBuilder {
Expand All @@ -114,9 +128,14 @@ impl Builder for MiniMokaBuilder {
}

debug!("backend build finished: {:?}", &self);
Ok(MiniMokaBackend::new(Adapter {
let mut backend = MiniMokaBackend::new(Adapter {
inner: builder.build(),
}))
});
if let Some(v) = self.config.root {
backend = backend.with_root(&v);
}

Ok(backend)
}
}

Expand Down
25 changes: 23 additions & 2 deletions core/src/services/moka/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub struct MokaConfig {
///
/// Refer to [`moka::sync::CacheBuilder::segments`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.segments)
pub num_segments: Option<usize>,

/// root path of this backend
pub root: Option<String>,
}

impl Debug for MokaConfig {
Expand All @@ -62,6 +65,7 @@ impl Debug for MokaConfig {
.field("time_to_live", &self.time_to_live)
.field("time_to_idle", &self.time_to_idle)
.field("num_segments", &self.num_segments)
.field("root", &self.root)
.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -127,6 +131,17 @@ impl MokaBuilder {
self.config.num_segments = Some(v);
self
}

/// Set root path of this backend
pub fn root(mut self, path: &str) -> Self {
self.config.root = if path.is_empty() {
None
} else {
Some(path.to_string())
};

self
}
}

impl Builder for MokaBuilder {
Expand Down Expand Up @@ -154,9 +169,15 @@ impl Builder for MokaBuilder {
}

debug!("backend build finished: {:?}", &self);
Ok(MokaBackend::new(Adapter {

let mut backend = MokaBackend::new(Adapter {
inner: builder.build(),
}))
});
if let Some(v) = self.config.root {
backend = backend.with_root(&v);
}

Ok(backend)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/types/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Builder for () {
/// use std::collections::HashMap;
///
/// use opendal::services::S3Config;
/// use opendal::{Configurator, Operator};///
/// use opendal::{Configurator, Operator};
/// use opendal::raw::HttpClient;
///
/// async fn test() -> Result<()> {
Expand Down

0 comments on commit 5727752

Please sign in to comment.