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

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

commit da97c790e5ab0b9f4e8cc1cc98a205fcf6cb360e
Author: G-XD <[email protected]>
AuthorDate: Tue Dec 5 00:07:04 2023 +0800

    refactor(service/moka): Add MokaConfig to implement ConfigDeserializer
---
 core/src/services/mod.rs          |  2 +
 core/src/services/moka/backend.rs | 84 ++++++++++++++++++++++-----------------
 core/src/services/moka/mod.rs     |  1 +
 3 files changed, 50 insertions(+), 37 deletions(-)

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 62fd55c8d..afcfbdd2d 100644
--- a/core/src/services/moka/backend.rs
+++ b/core/src/services/moka/backend.rs
@@ -17,45 +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>,
+    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
     }
@@ -65,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
     }
@@ -75,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
     }
@@ -85,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
     }
@@ -95,7 +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.config.num_segments = Some(v);
         self
     }
 }
@@ -105,42 +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)));
-
-        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));
+            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)
         }
 
@@ -160,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/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