This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new e16419952 feat(service/moka): bump moka from 0.10.4 to 0.12.1 (#3711)
e16419952 is described below

commit e16419952e94b7b4092c14f16a2c5e4d23bd656e
Author: G-XD <[email protected]>
AuthorDate: Tue Dec 5 00:37:18 2023 +0800

    feat(service/moka): bump moka from 0.10.4 to 0.12.1 (#3711)
---
 Cargo.lock                        |  8 ++--
 core/Cargo.toml                   |  2 +-
 core/src/docs/upgrade.md          |  8 ++++
 core/src/services/mod.rs          |  2 +
 core/src/services/moka/backend.rs | 99 +++++++++++++++++++--------------------
 core/src/services/moka/docs.md    |  1 -
 core/src/services/moka/mod.rs     |  1 +
 7 files changed, 62 insertions(+), 59 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 6bcad4782..6ddd4a0fb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4183,22 +4183,20 @@ dependencies = [
 
 [[package]]
 name = "moka"
-version = "0.10.4"
+version = "0.12.1"
 source = "registry+https://github.com/rust-lang/crates.io-index";
-checksum = "0be0a3dd6fe7c99233c2b1476e703147fb7516c68dce585b19b51efc08fe93d8"
+checksum = "d8017ec3548ffe7d4cef7ac0e12b044c01164a74c0f3119420faeaf13490ad8b"
 dependencies = [
- "async-io",
  "async-lock 2.8.0",
+ "async-trait",
  "crossbeam-channel",
  "crossbeam-epoch",
  "crossbeam-utils",
  "futures-util",
- "num_cpus",
  "once_cell",
  "parking_lot 0.12.1",
  "quanta 0.11.1",
  "rustc_version 0.4.0",
- "scheduled-thread-pool",
  "skeptic",
  "smallvec",
  "tagptr",
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 55541ee6c..83a1c8d42 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -255,7 +255,7 @@ md-5 = "0.10"
 metrics = { version = "0.20", optional = true }
 mini-moka = { version = "0.10", optional = true }
 minitrace = { version = "0.6", optional = true }
-moka = { version = "0.10", optional = true, features = ["future"] }
+moka = { version = "0.12", optional = true, features = ["future", "sync"] }
 mongodb = { version = "2.7.0", optional = true, features = ["tokio-runtime"] }
 mysql_async = { version = "0.32.2", default-features = false, features = 
["default-rustls"], optional = true }
 once_cell = "1"
diff --git a/core/src/docs/upgrade.md b/core/src/docs/upgrade.md
index 73fde6ddc..f769cca2f 100644
--- a/core/src/docs/upgrade.md
+++ b/core/src/docs/upgrade.md
@@ -1,3 +1,11 @@
+# Unreleased
+
+## Public API
+
+### Moka Service Configuration
+
+- The `thread_pool_enabled` option has been removed.
+
 # Upgrade to v0.43
 
 ## Public API
diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs
index edb0444e7..0ae125537 100644
--- a/core/src/services/mod.rs
+++ b/core/src/services/mod.rs
@@ -135,6 +135,8 @@ pub use self::mini_moka::MiniMoka;
 mod moka;
 #[cfg(feature = "services-moka")]
 pub use self::moka::Moka;
+#[cfg(feature = "services-moka")]
+pub use self::moka::MokaConfig;
 
 #[cfg(feature = "services-obs")]
 mod obs;
diff --git a/core/src/services/moka/backend.rs 
b/core/src/services/moka/backend.rs
index fe45d07f9..afcfbdd2d 100644
--- a/core/src/services/moka/backend.rs
+++ b/core/src/services/moka/backend.rs
@@ -17,49 +17,68 @@
 
 use std::collections::HashMap;
 use std::fmt::Debug;
+use std::fmt::Formatter;
 use std::time::Duration;
 
 use async_trait::async_trait;
 use log::debug;
 use moka::sync::CacheBuilder;
 use moka::sync::SegmentedCache;
+use serde::Deserialize;
 
 use crate::raw::adapters::typed_kv;
+use crate::raw::ConfigDeserializer;
 use crate::*;
 
-/// [moka](https://github.com/moka-rs/moka) backend support.
-#[doc = include_str!("docs.md")]
-#[derive(Default, Debug)]
-pub struct MokaBuilder {
+/// Config for Mokaservices support.
+#[derive(Default, Deserialize)]
+#[serde(default)]
+#[non_exhaustive]
+pub struct MokaConfig {
     /// Name for this cache instance.
-    name: Option<String>,
+    pub name: Option<String>,
     /// Sets the max capacity of the cache.
     ///
     /// Refer to 
[`moka::sync::CacheBuilder::max_capacity`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.max_capacity)
-    max_capacity: Option<u64>,
+    pub max_capacity: Option<u64>,
     /// Sets the time to live of the cache.
     ///
     /// Refer to 
[`moka::sync::CacheBuilder::time_to_live`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_live)
-    time_to_live: Option<Duration>,
+    pub time_to_live: Option<Duration>,
     /// Sets the time to idle of the cache.
     ///
     /// Refer to 
[`moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_idle)
-    time_to_idle: Option<Duration>,
+    pub time_to_idle: Option<Duration>,
     /// Sets the segments number of the cache.
     ///
     /// Refer to 
[`moka::sync::CacheBuilder::segments`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.segments)
-    num_segments: Option<usize>,
-    /// Decides whether to enable thread pool of the cache.
-    ///
-    /// Refer to 
[`moka::sync::CacheBuilder::thread_pool_enabled`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.thread_pool_enabled)
-    thread_pool_enabled: Option<bool>,
+    pub num_segments: Option<usize>,
+}
+
+impl Debug for MokaConfig {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("MokaConfig")
+            .field("name", &self.name)
+            .field("max_capacity", &self.max_capacity)
+            .field("time_to_live", &self.time_to_live)
+            .field("time_to_idle", &self.time_to_idle)
+            .field("num_segments", &self.num_segments)
+            .finish_non_exhaustive()
+    }
+}
+
+/// [moka](https://github.com/moka-rs/moka) backend support.
+#[doc = include_str!("docs.md")]
+#[derive(Default, Debug)]
+pub struct MokaBuilder {
+    config: MokaConfig,
 }
 
 impl MokaBuilder {
     /// Name for this cache instance.
     pub fn name(&mut self, v: &str) -> &mut Self {
         if !v.is_empty() {
-            self.name = Some(v.to_owned());
+            self.config.name = Some(v.to_owned());
         }
         self
     }
@@ -69,7 +88,7 @@ impl MokaBuilder {
     /// Refer to 
[`moka::sync::CacheBuilder::max_capacity`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.max_capacity)
     pub fn max_capacity(&mut self, v: u64) -> &mut Self {
         if v != 0 {
-            self.max_capacity = Some(v);
+            self.config.max_capacity = Some(v);
         }
         self
     }
@@ -79,7 +98,7 @@ impl MokaBuilder {
     /// Refer to 
[`moka::sync::CacheBuilder::time_to_live`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_live)
     pub fn time_to_live(&mut self, v: Duration) -> &mut Self {
         if !v.is_zero() {
-            self.time_to_live = Some(v);
+            self.config.time_to_live = Some(v);
         }
         self
     }
@@ -89,7 +108,7 @@ impl MokaBuilder {
     /// Refer to 
[`moka::sync::CacheBuilder::time_to_idle`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.time_to_idle)
     pub fn time_to_idle(&mut self, v: Duration) -> &mut Self {
         if !v.is_zero() {
-            self.time_to_idle = Some(v);
+            self.config.time_to_idle = Some(v);
         }
         self
     }
@@ -99,15 +118,7 @@ impl MokaBuilder {
     /// Refer to 
[`moka::sync::CacheBuilder::segments`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.segments)
     pub fn segments(&mut self, v: usize) -> &mut Self {
         assert!(v != 0);
-        self.num_segments = Some(v);
-        self
-    }
-
-    /// Decides whether to enable thread pool of the cache.
-    ///
-    /// Refer to 
[`moka::sync::CacheBuilder::thread_pool_enabled`](https://docs.rs/moka/latest/moka/sync/struct.CacheBuilder.html#method.thread_pool_enabled)
-    pub fn thread_pool_enabled(&mut self, v: bool) -> &mut Self {
-        self.thread_pool_enabled = Some(v);
+        self.config.num_segments = Some(v);
         self
     }
 }
@@ -117,45 +128,29 @@ impl Builder for MokaBuilder {
     type Accessor = MokaBackend;
 
     fn from_map(map: HashMap<String, String>) -> Self {
-        let mut builder = MokaBuilder::default();
-
-        map.get("name").map(|v| builder.name(v));
-        map.get("max_capacity")
-            .map(|v| v.parse::<u64>().map(|v| builder.max_capacity(v)));
-        map.get("time_to_live").map(|v| {
-            v.parse::<u64>()
-                .map(|v| builder.time_to_live(Duration::from_secs(v)))
-        });
-        map.get("time_to_idle").map(|v| {
-            v.parse::<u64>()
-                .map(|v| builder.time_to_idle(Duration::from_secs(v)))
-        });
-        map.get("num_segments")
-            .map(|v| v.parse::<usize>().map(|v| builder.segments(v)));
-        map.get("thread_pool_enabled")
-            .map(|v| v.parse::<bool>().map(|v| 
builder.thread_pool_enabled(v)));
-
-        builder
+        MokaBuilder {
+            config: MokaConfig::deserialize(ConfigDeserializer::new(map))
+                .expect("config deserialize must succeed"),
+        }
     }
 
     fn build(&mut self) -> Result<Self::Accessor> {
         debug!("backend build started: {:?}", &self);
 
         let mut builder: CacheBuilder<String, typed_kv::Value, _> =
-            SegmentedCache::builder(self.num_segments.unwrap_or(1))
-                
.thread_pool_enabled(self.thread_pool_enabled.unwrap_or(false));
+            SegmentedCache::builder(self.config.num_segments.unwrap_or(1));
         // Use entries' bytes as capacity weigher.
         builder = builder.weigher(|k, v| (k.len() + v.size()) as u32);
-        if let Some(v) = &self.name {
+        if let Some(v) = &self.config.name {
             builder = builder.name(v);
         }
-        if let Some(v) = self.max_capacity {
+        if let Some(v) = self.config.max_capacity {
             builder = builder.max_capacity(v)
         }
-        if let Some(v) = self.time_to_live {
+        if let Some(v) = self.config.time_to_live {
             builder = builder.time_to_live(v)
         }
-        if let Some(v) = self.time_to_idle {
+        if let Some(v) = self.config.time_to_idle {
             builder = builder.time_to_idle(v)
         }
 
@@ -175,7 +170,7 @@ pub struct Adapter {
 }
 
 impl Debug for Adapter {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("Adapter")
             .field("size", &self.inner.weighted_size())
             .field("count", &self.inner.entry_count())
diff --git a/core/src/services/moka/docs.md b/core/src/services/moka/docs.md
index 1c5e3e827..975693805 100644
--- a/core/src/services/moka/docs.md
+++ b/core/src/services/moka/docs.md
@@ -21,7 +21,6 @@ This service can be used to:
 - `time_to_live`: Set the time to live of the cache.
 - `time_to_idle`: Set the time to idle of the cache.
 - `num_segments`: Set the segments number of the cache.
-- `thread_pool_enabled`: Decides whether to enable thread pool of the cache.
 
 You can refer to [`MokaBuilder`]'s docs for more information
 
diff --git a/core/src/services/moka/mod.rs b/core/src/services/moka/mod.rs
index 21216413a..0d79a37be 100644
--- a/core/src/services/moka/mod.rs
+++ b/core/src/services/moka/mod.rs
@@ -17,3 +17,4 @@
 
 mod backend;
 pub use backend::MokaBuilder as Moka;
+pub use backend::MokaConfig;

Reply via email to