simplify Cache invalidate trait, reduce EndpointCacheKey

This commit is contained in:
Conrad Ludgate
2024-05-07 08:34:21 +01:00
parent ef3a9dfafa
commit ca578449e4
10 changed files with 80 additions and 48 deletions

View File

@@ -5,34 +5,26 @@ use std::ops::{Deref, DerefMut};
/// This is useful for [`Cached`].
#[allow(async_fn_in_trait)]
pub trait Cache {
/// Entry's key.
type Key;
/// Entry's value.
type Value;
/// Used for entry invalidation.
type LookupInfo<Key>;
type LookupInfo;
/// Invalidate an entry using a lookup info.
/// We don't have an empty default impl because it's error-prone.
async fn invalidate(&self, _: &Self::LookupInfo<Self::Key>);
async fn invalidate(&self, _: &Self::LookupInfo);
}
impl<C: Cache> Cache for &C {
type Key = C::Key;
type Value = C::Value;
type LookupInfo<Key> = C::LookupInfo<Key>;
type LookupInfo = C::LookupInfo;
async fn invalidate(&self, info: &Self::LookupInfo<Self::Key>) {
async fn invalidate(&self, info: &Self::LookupInfo) {
C::invalidate(self, info).await
}
}
/// Wrapper for convenient entry invalidation.
pub struct Cached<C: Cache, V = <C as Cache>::Value> {
pub struct Cached<C: Cache, V> {
/// Cache + lookup info.
pub token: Option<(C, C::LookupInfo<C::Key>)>,
pub token: Option<(C, C::LookupInfo)>,
/// The value itself.
pub value: V,
@@ -88,11 +80,9 @@ where
V: Clone + Send + Sync + 'static,
S: std::hash::BuildHasher + Clone + Send + Sync + 'static,
{
type Key = K;
type Value = V;
type LookupInfo<Key> = Key;
type LookupInfo = K;
async fn invalidate(&self, key: &Self::LookupInfo<Self::Key>) {
async fn invalidate(&self, key: &Self::LookupInfo) {
moka::future::Cache::invalidate(self, key).await
}
}

View File

@@ -8,7 +8,6 @@ use std::{
use async_trait::async_trait;
use dashmap::DashMap;
use rand::{thread_rng, Rng};
use smol_str::SmolStr;
use tokio::sync::Mutex;
use tokio::time::Instant;
use tracing::{debug, info};
@@ -346,13 +345,9 @@ enum LookupType {
}
impl Cache for ProjectInfoCacheImpl {
type Key = SmolStr;
// Value is not really used here, but we need to specify it.
type Value = SmolStr;
type LookupInfo = CachedLookupInfo;
type LookupInfo<Key> = CachedLookupInfo;
async fn invalidate(&self, key: &Self::LookupInfo<SmolStr>) {
async fn invalidate(&self, key: &Self::LookupInfo) {
match &key.lookup_type {
LookupType::RoleSecret(role_name) => {
if let Some(mut endpoint_info) = self.cache.get_mut(&key.endpoint_id) {