Skip to content

Commit

Permalink
feat(service/memcached): Add MemCachedConfig (#3827)
Browse files Browse the repository at this point in the history
* feat(service/memcached): add MemCachedConfig to implement ConfigDeserializer

* fix: a typo bug

* fix(service/memcached): fix fmt error

* fix: adapter bug

* fix: add doc for struct
  • Loading branch information
ankit-pn authored Dec 27, 2023
1 parent da4f7cc commit e24206e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
48 changes: 27 additions & 21 deletions core/src/services/memcached/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
use std::collections::HashMap;
use std::time::Duration;

use async_trait::async_trait;
use bb8::RunError;
use tokio::net::TcpStream;
use tokio::sync::OnceCell;

use super::ascii;
use crate::raw::adapters::kv;
use crate::raw::*;
use crate::*;
use async_trait::async_trait;
use bb8::RunError;
use serde::Deserialize;
use tokio::net::TcpStream;
use tokio::sync::OnceCell;

/// [Memcached](https://memcached.org/) service support.
#[doc = include_str!("docs.md")]
#[derive(Clone, Default)]
pub struct MemcachedBuilder {
/// Config for MemCached services support
#[derive(Default, Deserialize, Clone)]
#[serde(default)]
#[non_exhaustive]
pub struct MemcachedConfig {
/// network address of the memcached service.
///
/// For example: "tcp://localhost:11211"
Expand All @@ -44,13 +45,20 @@ pub struct MemcachedBuilder {
default_ttl: Option<Duration>,
}

/// [Memcached](https://memcached.org/) service support.
#[doc = include_str!("docs.md")]
#[derive(Clone, Default)]
pub struct MemcachedBuilder {
config: MemcachedConfig,
}

impl MemcachedBuilder {
/// set the network address of memcached service.
///
/// For example: "tcp://localhost:11211"
pub fn endpoint(&mut self, endpoint: &str) -> &mut Self {
if !endpoint.is_empty() {
self.endpoint = Some(endpoint.to_owned());
self.config.endpoint = Some(endpoint.to_owned());
}
self
}
Expand All @@ -60,14 +68,14 @@ impl MemcachedBuilder {
/// default: "/"
pub fn root(&mut self, root: &str) -> &mut Self {
if !root.is_empty() {
self.root = Some(root.to_owned());
self.config.root = Some(root.to_owned());
}
self
}

/// Set the default ttl for memcached services.
pub fn default_ttl(&mut self, ttl: Duration) -> &mut Self {
self.default_ttl = Some(ttl);
self.config.default_ttl = Some(ttl);
self
}
}
Expand All @@ -77,16 +85,13 @@ impl Builder for MemcachedBuilder {
type Accessor = MemcachedBackend;

fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = MemcachedBuilder::default();

map.get("root").map(|v| builder.root(v));
map.get("endpoint").map(|v| builder.endpoint(v));

builder
let config = MemcachedConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed");
MemcachedBuilder { config }
}

fn build(&mut self) -> Result<Self::Accessor> {
let endpoint = self.endpoint.clone().ok_or_else(|| {
let endpoint = self.config.endpoint.clone().ok_or_else(|| {
Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")
.with_context("service", Scheme::Memcached)
})?;
Expand Down Expand Up @@ -135,7 +140,8 @@ impl Builder for MemcachedBuilder {
let endpoint = format!("{host}:{port}",);

let root = normalize_root(
self.root
self.config
.root
.clone()
.unwrap_or_else(|| "/".to_string())
.as_str(),
Expand All @@ -145,7 +151,7 @@ impl Builder for MemcachedBuilder {
Ok(MemcachedBackend::new(Adapter {
endpoint,
conn,
default_ttl: self.default_ttl,
default_ttl: self.config.default_ttl,
})
.with_root(&root))
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/services/memcached/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@

mod backend;
pub use backend::MemcachedBuilder as Memcached;

pub use backend::MemcachedConfig;
mod ascii;
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub use libsql::LibsqlConfig;
mod memcached;
#[cfg(feature = "services-memcached")]
pub use memcached::Memcached;
#[cfg(feature = "services-memcached")]
pub use memcached::MemcachedConfig;

#[cfg(feature = "services-memory")]
mod memory;
Expand Down

0 comments on commit e24206e

Please sign in to comment.