opendal_core/services/memcached/
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 std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::MemcachedBuilder;
24use crate::raw::*;
25
26/// Config for MemCached services support
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct MemcachedConfig {
31    /// network address of the memcached service.
32    ///
33    /// For example: "tcp://localhost:11211"
34    pub endpoint: Option<String>,
35    /// the working directory of the service. Can be "/path/to/dir"
36    ///
37    /// default is "/"
38    pub root: Option<String>,
39    /// Memcached username, optional.
40    pub username: Option<String>,
41    /// Memcached password, optional.
42    pub password: Option<String>,
43    /// The default ttl for put operations.
44    pub default_ttl: Option<Duration>,
45    /// The maximum number of connections allowed.
46    ///
47    /// default is 10
48    pub connection_pool_max_size: Option<usize>,
49}
50
51impl Debug for MemcachedConfig {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_struct("MemcachedConfig")
54            .field("endpoint", &self.endpoint)
55            .field("root", &self.root)
56            .field("username", &self.username)
57            .field("default_ttl", &self.default_ttl)
58            .finish_non_exhaustive()
59    }
60}
61
62impl crate::Configurator for MemcachedConfig {
63    type Builder = MemcachedBuilder;
64
65    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
66        let mut map = uri.options().clone();
67        if let Some(authority) = uri.authority() {
68            map.insert("endpoint".to_string(), format!("tcp://{authority}"));
69        }
70
71        if let Some(root) = uri.root() {
72            if !root.is_empty() {
73                map.insert("root".to_string(), root.to_string());
74            }
75        }
76
77        Self::from_iter(map)
78    }
79
80    fn into_builder(self) -> Self::Builder {
81        MemcachedBuilder { config: self }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use crate::Configurator;
89    use crate::types::OperatorUri;
90
91    #[test]
92    fn from_uri_sets_endpoint_and_root() {
93        let uri = OperatorUri::new(
94            "memcached://cache.local:11211/app/session",
95            Vec::<(String, String)>::new(),
96        )
97        .unwrap();
98
99        let cfg = MemcachedConfig::from_uri(&uri).unwrap();
100        assert_eq!(cfg.endpoint.as_deref(), Some("tcp://cache.local:11211"));
101        assert_eq!(cfg.root.as_deref(), Some("app/session"));
102    }
103}