Skip to main content

opendal_service_mini_moka/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use opendal_core::Configurator;
19use opendal_core::OperatorUri;
20use opendal_core::Result;
21use opendal_core::raw::SignedDuration;
22use serde::Deserialize;
23use serde::Serialize;
24
25use super::backend::MiniMokaBuilder;
26
27/// Config for mini-moka support.
28#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
29#[serde(default)]
30#[non_exhaustive]
31pub struct MiniMokaConfig {
32    /// Sets the max capacity of the cache.
33    ///
34    /// Refer to [`mini-moka::sync::CacheBuilder::max_capacity`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.max_capacity)
35    pub max_capacity: Option<u64>,
36    /// Sets the time to live of the cache.
37    ///
38    /// Refer to [`mini-moka::sync::CacheBuilder::time_to_live`](https://docs.rs/mini-moka/latest/mini_moka/sync/struct.CacheBuilder.html#method.time_to_live)
39    pub time_to_live: Option<SignedDuration>,
40    /// Sets the time to idle of the cache.
41    ///
42    /// 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)
43    pub time_to_idle: Option<SignedDuration>,
44
45    /// root path of this backend
46    pub root: Option<String>,
47}
48
49impl Configurator for MiniMokaConfig {
50    type Builder = MiniMokaBuilder;
51
52    fn from_uri(uri: &OperatorUri) -> Result<Self> {
53        let mut map = uri.options().clone();
54
55        if let Some(root) = uri.root()
56            && !root.is_empty()
57        {
58            map.insert("root".to_string(), root.to_string());
59        }
60
61        Self::from_iter(map)
62    }
63
64    fn into_builder(self) -> Self::Builder {
65        MiniMokaBuilder {
66            config: self,
67            ..Default::default()
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn from_uri_sets_root_and_preserves_ttl() -> Result<()> {
78        let uri = OperatorUri::new(
79            "mini-moka:///session",
80            vec![("time_to_live".to_string(), "300s".to_string())],
81        )?;
82
83        let cfg = MiniMokaConfig::from_uri(&uri)?;
84        assert_eq!(cfg.root.as_deref(), Some("session"));
85        assert_eq!(cfg.time_to_live, Some(SignedDuration::from_secs(300)));
86        Ok(())
87    }
88
89    #[test]
90    fn from_iter_parses_cache_durations() -> Result<()> {
91        let cfg = MiniMokaConfig::from_iter([
92            ("time_to_live".to_string(), "1500ms".to_string()),
93            ("time_to_idle".to_string(), "2s".to_string()),
94        ])?;
95
96        assert_eq!(cfg.time_to_live, Some(SignedDuration::from_millis(1500)));
97        assert_eq!(cfg.time_to_idle, Some(SignedDuration::from_secs(2)));
98
99        let cfg = MiniMokaConfig::from_iter([
100            ("time_to_live".to_string(), "PT1.500000001S".to_string()),
101            ("time_to_idle".to_string(), "PT2S".to_string()),
102        ])?;
103        assert_eq!(cfg.time_to_live, Some(SignedDuration::new(1, 500_000_001)));
104        assert_eq!(cfg.time_to_idle, Some(SignedDuration::from_secs(2)));
105        Ok(())
106    }
107}