46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use std::sync::Arc;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use crate::{RuntimeCacheConfig, RuntimeCacheStoreInitError, RuntimeCacheStores};
|
|
|
|
#[async_trait]
|
|
pub trait CacheBackendFactory: Send + Sync {
|
|
async fn build(
|
|
&self,
|
|
config: &RuntimeCacheConfig,
|
|
) -> Result<RuntimeCacheStores, RuntimeCacheStoreInitError>;
|
|
}
|
|
|
|
pub type SharedCacheBackendFactory = Arc<dyn CacheBackendFactory>;
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct BuiltinCacheBackendFactory;
|
|
|
|
#[async_trait]
|
|
impl CacheBackendFactory for BuiltinCacheBackendFactory {
|
|
async fn build(
|
|
&self,
|
|
config: &RuntimeCacheConfig,
|
|
) -> Result<RuntimeCacheStores, RuntimeCacheStoreInitError> {
|
|
RuntimeCacheStores::from_config(config).await
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::{CacheBackendFactory, RuntimeCacheConfig};
|
|
|
|
use super::BuiltinCacheBackendFactory;
|
|
|
|
#[tokio::test]
|
|
async fn builtin_factory_builds_default_memory_stores() {
|
|
let stores = BuiltinCacheBackendFactory
|
|
.build(&RuntimeCacheConfig::default())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(stores.backend, crank_core::CacheBackend::Memory);
|
|
}
|
|
}
|