mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-08-18 03:58:29 +00:00
refactor: cache invalidator (#3611)
* chore: remove some alias * refactor: cache invalidator
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::instruction::CacheIdent;
|
||||
use crate::key::table_info::TableInfoKey;
|
||||
@@ -58,6 +60,34 @@ impl CacheInvalidator for DummyCacheInvalidator {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MultiCacheInvalidator {
|
||||
invalidators: RwLock<Vec<CacheInvalidatorRef>>,
|
||||
}
|
||||
|
||||
impl MultiCacheInvalidator {
|
||||
pub fn with_invalidators(invalidators: Vec<CacheInvalidatorRef>) -> Self {
|
||||
Self {
|
||||
invalidators: RwLock::new(invalidators),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_invalidator(&self, invalidator: CacheInvalidatorRef) {
|
||||
self.invalidators.write().await.push(invalidator);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl CacheInvalidator for MultiCacheInvalidator {
|
||||
async fn invalidate(&self, ctx: &Context, caches: Vec<CacheIdent>) -> Result<()> {
|
||||
let invalidators = self.invalidators.read().await;
|
||||
for invalidator in invalidators.iter() {
|
||||
invalidator.invalidate(ctx, caches.clone()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T> CacheInvalidator for T
|
||||
where
|
||||
|
||||
@@ -331,23 +331,19 @@ impl AlterTableProcedure {
|
||||
async fn on_broadcast(&mut self) -> Result<Status> {
|
||||
let alter_kind = self.alter_kind()?;
|
||||
let cache_invalidator = &self.context.cache_invalidator;
|
||||
|
||||
if matches!(alter_kind, Kind::RenameTable { .. }) {
|
||||
cache_invalidator
|
||||
.invalidate(
|
||||
&Context::default(),
|
||||
vec![CacheIdent::TableName(self.data.table_ref().into())],
|
||||
)
|
||||
.await?;
|
||||
let cache_keys = if matches!(alter_kind, Kind::RenameTable { .. }) {
|
||||
vec![CacheIdent::TableName(self.data.table_ref().into())]
|
||||
} else {
|
||||
cache_invalidator
|
||||
.invalidate(
|
||||
&Context::default(),
|
||||
vec![CacheIdent::TableId(self.data.table_id())],
|
||||
)
|
||||
.await?;
|
||||
vec![
|
||||
CacheIdent::TableId(self.data.table_id()),
|
||||
CacheIdent::TableName(self.data.table_ref().into()),
|
||||
]
|
||||
};
|
||||
|
||||
cache_invalidator
|
||||
.invalidate(&Context::default(), cache_keys)
|
||||
.await?;
|
||||
|
||||
Ok(Status::done())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user