opendal/services/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 serde::Deserialize;
19use serde::Serialize;
20
21use super::backend::MokaBuilder;
22
23/// Config for Moka services support.
24#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
25#[serde(default)]
26#[non_exhaustive]
27pub struct MokaConfig {
28    /// Name for this cache instance.
29    pub name: Option<String>,
30    /// Sets the max capacity of the cache.
31    ///
32    /// Refer to [`moka::future::CacheBuilder::max_capacity`](https://docs.rs/moka/latest/moka/future/struct.CacheBuilder.html#method.max_capacity)
33    pub max_capacity: Option<u64>,
34    /// Sets the time to live of the cache.
35    ///
36    /// Refer to [`moka::future::CacheBuilder::time_to_live`](https://docs.rs/moka/latest/moka/future/struct.CacheBuilder.html#method.time_to_live)
37    pub time_to_live: Option<String>,
38    /// Sets the time to idle of the cache.
39    ///
40    /// Refer to [`moka::future::CacheBuilder::time_to_idle`](https://docs.rs/moka/latest/moka/future/struct.CacheBuilder.html#method.time_to_idle)
41    pub time_to_idle: Option<String>,
42
43    /// root path of this backend
44    pub root: Option<String>,
45}
46
47impl crate::Configurator for MokaConfig {
48    type Builder = MokaBuilder;
49
50    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
51        let mut map = uri.options().clone();
52
53        if let Some(name) = uri.option("name") {
54            map.insert("name".to_string(), name.to_string());
55        }
56
57        if let Some(root) = uri.root() {
58            if !root.is_empty() {
59                map.insert("root".to_string(), root.to_string());
60            }
61        }
62
63        Self::from_iter(map)
64    }
65
66    fn into_builder(self) -> Self::Builder {
67        MokaBuilder {
68            config: self,
69            ..Default::default()
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::Configurator;
78    use crate::types::OperatorUri;
79
80    #[test]
81    fn from_uri_sets_name_and_root() {
82        let uri =
83            OperatorUri::new("moka:///cache?name=session", Vec::<(String, String)>::new()).unwrap();
84
85        let cfg = MokaConfig::from_uri(&uri).unwrap();
86        assert_eq!(cfg.name.as_deref(), Some("session"));
87        assert_eq!(cfg.root.as_deref(), Some("cache"));
88    }
89}