From 280ea996287d9f8a75be7ba633678b2649487987 Mon Sep 17 00:00:00 2001 From: github-ops Date: Fri, 15 May 2026 13:12:38 +0000 Subject: [PATCH] runtime: add cache backend factory seam --- TASKS.md | 1 + crates/crank-runtime/src/cache_factory.rs | 47 +++++++++++++++++++++++ crates/crank-runtime/src/lib.rs | 4 ++ 3 files changed, 52 insertions(+) create mode 100644 crates/crank-runtime/src/cache_factory.rs diff --git a/TASKS.md b/TASKS.md index 634c242..307261b 100644 --- a/TASKS.md +++ b/TASKS.md @@ -168,4 +168,5 @@ Progress: - Phase 3 dependency slice: added public tenancy seam in `crank-core` — `TenantId`, `TenantResolutionContext`, `TenantController`, `TenancyError`, and `SingleTenantController` — so `cloud` can build hosted tenant routing without bypassing the shared extension model - Phase 3 dependency slice: added public metering seam in `crank-core` — `MeteringEvent`, `MeteringSink`, `SharedMeteringSink`, and `NoopMeteringSink` — so `cloud` can add hosted usage capture without introducing private-only contracts - Phase 3 dependency slice: added public billing seam in `crank-core` — `BillingHook`, `BillingGate`, `BillingError`, `SharedBillingHook`, and `NoopBillingHook` — so `cloud` can layer billing policy without private-only contracts in the base repo + - Phase 3 dependency slice: added `CacheBackendFactory` in `crank-runtime` with `BuiltinCacheBackendFactory`; unlike the original draft, the seam lives in runtime rather than core to avoid a `crank-core -> crank-runtime` dependency cycle around `RuntimeCacheStores` - backward-compatible alias `CommunityMachineCredentialVerifier` сохранен, поведение Community не изменено diff --git a/crates/crank-runtime/src/cache_factory.rs b/crates/crank-runtime/src/cache_factory.rs new file mode 100644 index 0000000..2faa214 --- /dev/null +++ b/crates/crank-runtime/src/cache_factory.rs @@ -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; +} + +pub type SharedCacheBackendFactory = Arc; + +#[derive(Clone, Copy, Debug, Default)] +pub struct BuiltinCacheBackendFactory; + +#[async_trait] +impl CacheBackendFactory for BuiltinCacheBackendFactory { + async fn build( + &self, + config: &RuntimeCacheConfig, + ) -> Result { + 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); + } +} diff --git a/crates/crank-runtime/src/lib.rs b/crates/crank-runtime/src/lib.rs index e24bf64..4920668 100644 --- a/crates/crank-runtime/src/lib.rs +++ b/crates/crank-runtime/src/lib.rs @@ -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};