in core/src/services/redis/backend.rs [166:246]
fn build(&mut self) -> Result<Self::Accessor> {
let endpoint = self
.endpoint
.clone()
.unwrap_or_else(|| DEFAULT_REDIS_ENDPOINT.to_string());
let ep_url = endpoint.parse::<Uri>().map_err(|e| {
Error::new(ErrorKind::ConfigInvalid, "endpoint is invalid")
.with_context("service", Scheme::Redis)
.with_context("endpoint", endpoint)
.set_source(e)
})?;
let con_addr = match ep_url.scheme_str() {
Some("tcp") | Some("redis") | None => {
let host = ep_url
.host()
.map(|h| h.to_string())
.unwrap_or_else(|| "127.0.0.1".to_string());
let port = ep_url.port_u16().unwrap_or(DEFAULT_REDIS_PORT);
ConnectionAddr::Tcp(host, port)
}
Some("rediss") => {
let host = ep_url
.host()
.map(|h| h.to_string())
.unwrap_or_else(|| "127.0.0.1".to_string());
let port = ep_url.port_u16().unwrap_or(DEFAULT_REDIS_PORT);
ConnectionAddr::TcpTls {
host,
port,
insecure: false,
}
}
Some("unix") | Some("redis+unix") => {
let path = PathBuf::from(ep_url.path());
ConnectionAddr::Unix(path)
}
Some(s) => {
return Err(
Error::new(ErrorKind::ConfigInvalid, "invalid or unsupported scheme")
.with_context("service", Scheme::Redis)
.with_context("scheme", s),
)
}
};
let redis_info = RedisConnectionInfo {
db: self.db,
username: self.username.clone(),
password: self.password.clone(),
};
let con_info = ConnectionInfo {
addr: con_addr,
redis: redis_info,
};
let client = Client::open(con_info).map_err(|e| {
Error::new(ErrorKind::ConfigInvalid, "invalid or unsupported scheme")
.with_context("service", Scheme::Redis)
.with_context("endpoint", self.endpoint.as_ref().unwrap())
.with_context("db", self.db.to_string())
.set_source(e)
})?;
let root = normalize_root(
self.root
.clone()
.unwrap_or_else(|| "/".to_string())
.as_str(),
);
let conn = OnceCell::new();
Ok(RedisBackend::new(Adapter {
client,
conn,
default_ttl: self.default_ttl,
})
.with_root(&root))
}