Skip to main content

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