Skip to content

Commit d0b860f

Browse files
committed
feat: add cache invalidations
1 parent f3d12be commit d0b860f

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

codegen/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,14 @@ pub fn dynamic_cache(arguments: TokenStream, input: TokenStream) -> TokenStream
134134
})
135135
.collect::<Vec<Ident>>();
136136

137+
let invalidate = format_ident!("invalidate_{}", &sig.ident);
137138
let wrapper = format_ident!("__{}", &sig.ident);
138139
let skytable_tls = format_ident!("__{}_skytable_tls", &sig.ident);
140+
let skytable_tls_static = format_ident!("{}", skytable_tls.to_string().to_uppercase());
139141
let skytable = format_ident!("__{}_skytable", &sig.ident);
142+
let skytable_static = format_ident!("{}", skytable.to_string().to_uppercase());
140143
let memory = format_ident!("__{}_memory", &sig.ident);
144+
let memory_static = format_ident!("{}", memory.to_string().to_uppercase());
141145

142146
let mut wrapper_sig = sig.clone();
143147
wrapper_sig.ident = wrapper.clone();
@@ -251,6 +255,34 @@ pub fn dynamic_cache(arguments: TokenStream, input: TokenStream) -> TokenStream
251255
#memory(#(#args),*).await
252256
}
253257

258+
#vis async fn #invalidate(key: String) -> Result<()> {
259+
use cached::Cached;
260+
// check if the user did configure skytable
261+
#[cfg(feature = "caching-skytable")]
262+
if CONFIG.skytable_host().is_some() {
263+
// now check if the user did configure tls
264+
match CONFIG.skytable_certificate() {
265+
None => {
266+
// tls is inactive so we do use the raw tcp stream
267+
#skytable_static.get().unwrap().cache_remove(&key).await?;
268+
},
269+
Some(_) => {
270+
// tls is active so we use the tls cache
271+
#skytable_tls_static.get().unwrap().cache_remove(&key).await?;
272+
}
273+
}
274+
} else {
275+
// otherwise use timed in memory caching
276+
#memory_static.lock().await.cache_remove(&key);
277+
}
278+
279+
#[cfg(not(feature = "caching-skytable"))]
280+
#memory_static.lock().await.cache_remove(&key);
281+
282+
283+
Ok(())
284+
}
285+
254286
// create the wrapper containing the original data logic
255287
#wrapper_sig #block
256288

src/cache.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ where
397397
}
398398
}
399399

400+
#[macro_export]
401+
macro_rules! invalidate {
402+
($cache: ident, $key: expr) => {
403+
paste! {{
404+
let key = async { $key }.await;
405+
406+
tokio::spawn(async move {
407+
[<invalidate_ $cache>](key).await.unwrap();
408+
});
409+
}
410+
}};
411+
}
412+
400413
#[dynamic_cache(ttl = "300", key = r#"format!('prompt-{}', id)"#)]
401414
pub async fn fetch_prompt(connection: &DatabaseConnection, id: &str) -> Result<Option<Prompt>> {
402415
let result = database_request!(

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub enum FeedbackFusionError {
4040
Unauthorized,
4141
#[error("{0}")]
4242
Forbidden(String),
43+
#[cfg(feature = "caching-skytable")]
44+
#[error(transparent)]
45+
CacheError(#[from] crate::cache::SkytableCacheError)
4346
}
4447

4548
impl From<ValidationErrors> for FeedbackFusionError {

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ async fn main() {
198198
}
199199

200200
pub mod prelude {
201-
#[cfg(feature = "caching-skytable")]
202-
pub use crate::{cache::*, skytable_configuration, skytable_configuration_tls};
201+
pub use crate::{cache::*, skytable_configuration, skytable_configuration_tls, invalidate};
203202
pub use crate::{
204203
config::*,
205204
database::{DatabaseConfiguration, DatabaseConnection},

src/services/v1/field.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
use super::{FeedbackFusionV1Context, PublicFeedbackFusionV1Context};
2424
use crate::{
25-
cache::fetch_prompt, database::schema::feedback::{Field, FieldOptions, FieldType}, prelude::*
25+
cache::fetch_prompt,
26+
database::schema::feedback::{Field, FieldOptions, FieldType},
27+
prelude::*,
2628
};
2729
use feedback_fusion_common::proto::{
2830
CreateFieldRequest, DeleteFieldRequest, Field as ProtoField, FieldPage, GetFieldsRequest,
@@ -48,6 +50,8 @@ pub async fn create_field(
4850
.build();
4951
database_request!(Field::insert(connection, &field).await, "Insert field")?;
5052

53+
invalidate!(fields_by_prompt, format!("prompt-{}", field.prompt()));
54+
5155
Ok(Response::new(field.into()))
5256
}
5357

@@ -151,6 +155,8 @@ pub async fn update_field(
151155
"Update field by id"
152156
)?;
153157

158+
invalidate!(fields_by_prompt, format!("prompt-{}", field.prompt()));
159+
154160
Ok(Response::new(field.into()))
155161
}
156162

src/services/v1/prompt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ pub async fn update_prompt(
117117
Prompt::update_by_column(connection, &prompt, "id").await,
118118
"Update prompt"
119119
)?;
120+
121+
invalidate!(fetch_prompt, format!("prompt-{}", prompt.id()));
122+
120123
Ok(Response::new(prompt.into()))
121124
}
122125

0 commit comments

Comments
 (0)