use crate::error::CacheError;
use crate::integrity::IntegrityCheck;
use crate::key::KeyRef;
use crate::tier::TierId;
use crate::tier::sharded_stub::ShardedTierStub;
use crate::tier::tier_trait::{BackendKind, CacheTier, TierHealth};
#[derive(Debug)]
pub struct L3Stub<V> {
inner: ShardedTierStub<V>,
}
impl<V> L3Stub<V> {
#[allow(clippy::expect_used)] pub fn new() -> Self {
L3Stub {
inner: ShardedTierStub::new(),
}
}
pub fn with_capacity(capacity: usize) -> Result<Self, CacheError> {
Ok(L3Stub {
inner: ShardedTierStub::with_shards(
crate::tier::sharded_stub::DEFAULT_SHARDS,
capacity,
)?,
})
}
}
#[async_trait::async_trait]
impl<V: Clone + Send + Sync + 'static + IntegrityCheck> CacheTier<V> for L3Stub<V> {
fn name(&self) -> String {
"L3-in-memory-fallback".into()
}
fn backend(&self) -> BackendKind {
BackendKind::InMemoryFallback
}
fn capability(&self) -> crate::capability::TierCapability {
crate::capability::TierCapability::new(
BackendKind::InMemoryFallback,
crate::capability::CapabilityFlags::IN_MEMORY
| crate::capability::CapabilityFlags::VOLATILE
| crate::capability::CapabilityFlags::ATOMIC_WRITE_OR_ERROR,
crate::capability::OperationalState::Healthy,
crate::capability::DurabilityClass::Volatile,
)
}
async fn get(&self, key: &KeyRef<'_>) -> Result<Option<V>, CacheError> {
self.inner.get(key)
}
async fn set(
&self,
key: &KeyRef<'_>,
value: V,
ttl: Option<std::time::Duration>,
) -> Result<(), CacheError> {
self.inner.set(key, value, ttl)
}
async fn remove(&self, key: &KeyRef<'_>) -> Result<(), CacheError> {
self.inner.remove(key)
}
async fn contains(&self, key: &KeyRef<'_>) -> Result<bool, CacheError> {
self.inner.contains(key)
}
fn eviction_candidate(&self) -> Option<crate::integrity::KeyAddress> {
self.inner.eviction_candidate()
}
fn remove_if_address(
&self,
address: crate::integrity::KeyAddress,
) -> Result<bool, crate::error::CacheError> {
self.inner.remove_if_address(address)
}
fn health(&self) -> TierHealth {
TierHealth::default()
}
fn tier_id(&self) -> TierId {
TierId::L3
}
}