use std::time::Duration;
use crate::client::{CacheKit, CacheKitBuilder, SharedBackend};
use crate::error::CachekitError;
#[cfg(not(any(target_arch = "wasm32", feature = "unsync")))]
fn wrap(b: impl crate::backend::Backend + 'static) -> SharedBackend {
std::sync::Arc::new(b)
}
#[cfg(any(target_arch = "wasm32", feature = "unsync"))]
fn wrap(b: impl crate::backend::Backend + 'static) -> SharedBackend {
std::rc::Rc::new(b)
}
impl CacheKit {
#[cfg(feature = "redis")]
pub async fn minimal(redis_url: &str) -> Result<CacheKitBuilder, CachekitError> {
let backend = crate::backend::redis::RedisBackend::builder()
.url(redis_url)
.build()?;
drop(backend.connect().await?);
Ok(CacheKitBuilder::default()
.backend(wrap(backend))
.default_ttl(Duration::from_secs(300))
.no_l1())
}
#[cfg(feature = "redis")]
pub async fn production(redis_url: &str) -> Result<CacheKitBuilder, CachekitError> {
let backend = crate::backend::redis::RedisBackend::builder()
.url(redis_url)
.auto_reconnect()
.build()?;
drop(backend.connect().await?);
Ok(CacheKitBuilder::default()
.backend(wrap(backend))
.default_ttl(Duration::from_secs(600))
.l1_capacity(1000))
}
#[cfg(all(feature = "redis", feature = "encryption"))]
pub async fn encrypted(
redis_url: &str,
master_key: &[u8],
) -> Result<CacheKitBuilder, CachekitError> {
let builder = CacheKitBuilder::default()
.default_ttl(Duration::from_secs(600))
.l1_capacity(1000)
.encryption_from_bytes(master_key, "default")?;
let backend = crate::backend::redis::RedisBackend::builder()
.url(redis_url)
.auto_reconnect()
.build()?;
drop(backend.connect().await?);
Ok(builder.backend(wrap(backend)))
}
#[cfg(all(feature = "cachekitio", not(target_arch = "wasm32")))]
pub fn io(api_key: &str) -> Result<CacheKitBuilder, CachekitError> {
let backend = crate::backend::cachekitio::CachekitIO::builder()
.api_key(api_key)
.build()?;
Ok(CacheKitBuilder::default()
.backend(wrap(backend))
.default_ttl(Duration::from_secs(3600))
.l1_capacity(1000))
}
}