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