runtime: add cache backend factory seam
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod aggregation;
|
||||
mod auth;
|
||||
mod cache;
|
||||
mod cache_factory;
|
||||
mod error;
|
||||
mod executor;
|
||||
mod executor_builder;
|
||||
@@ -18,6 +19,9 @@ pub use cache::{
|
||||
InMemoryResponseCacheStore, RedisCacheStore, RuntimeCacheConfig, RuntimeCacheConfigError,
|
||||
RuntimeCacheStoreInitError, RuntimeCacheStores,
|
||||
};
|
||||
pub use cache_factory::{
|
||||
BuiltinCacheBackendFactory, CacheBackendFactory, SharedCacheBackendFactory,
|
||||
};
|
||||
pub use error::RuntimeError;
|
||||
pub use executor::RuntimeExecutor;
|
||||
pub use executor_builder::{RuntimeExecutorBuilder, community_default};
|
||||
|
||||
Reference in New Issue
Block a user