From 9c7a1206de25a9b67a131002ead73fe5d88979aa Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Wed, 30 Aug 2023 12:50:36 +0200 Subject: [PATCH 1/3] Add origin function --- CHANGELOG.md | 6 ++++++ cached_proc_macro/src/cached.rs | 29 +++++++++++++++++++++-------- src/stores/redis.rs | 8 ++++---- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39cdcf4..4ea83bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ ## Changed ## Removed +## [Unreleased] +## Added +- Also generate `*_origin` function for every cached function to allow calling the original function without caching. +## Changed +## Removed + ## [0.44.0] / [cached_proc_macro[0.17.0]] ## Added - Option to enable redis multiplex-connection manager on `AsyncRedisCache` diff --git a/cached_proc_macro/src/cached.rs b/cached_proc_macro/src/cached.rs index 6394f58..54fa71d 100644 --- a/cached_proc_macro/src/cached.rs +++ b/cached_proc_macro/src/cached.rs @@ -190,19 +190,24 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { #set_cache_block result }; + + let origin_fn_ident = Ident::new(&format!("{}_origin", &fn_ident), fn_ident.span()); + let lock; + let function_origin; let function_call; let cache_type; if asyncness.is_some() { lock = quote! { - // try to get a lock first let mut cache = #cache_ident.lock().await; }; + function_origin = quote! { + async fn #origin_fn_ident(#inputs) #output #body + }; + function_call = quote! { - // run the function and cache the result - async fn inner(#inputs) #output #body; - let result = inner(#(#input_names),*).await; + let result = #origin_fn_ident(#(#input_names),*).await; }; cache_type = quote! { @@ -210,14 +215,15 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { }; } else { lock = quote! { - // try to get a lock first let mut cache = #cache_ident.lock().unwrap(); }; + function_origin = quote! { + fn #origin_fn_ident(#inputs) #output #body + }; + function_call = quote! { - // run the function and cache the result - fn inner(#inputs) #output #body; - let result = inner(#(#input_names),*); + let result = #origin_fn_ident(#(#input_names),*); }; cache_type = quote! { @@ -226,7 +232,9 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { } let prime_do_set_return_block = quote! { + // try to get a lock first #lock + // run the function and cache the result #function_call #set_cache_and_return }; @@ -263,6 +271,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { // make cached static, cached function and prime cached function doc comments let cache_ident_doc = format!("Cached static for the [`{}`] function.", fn_ident); + let origin_fn_indent_doc = format!("Origin of the cached function [`{}`].", fn_ident); let prime_fn_indent_doc = format!("Primes the cached function [`{}`].", fn_ident); let cache_fn_doc_extra = format!( "This is a cached function that uses the [`{}`] cached static.", @@ -275,6 +284,9 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { // Cached static #[doc = #cache_ident_doc] #cache_type + // Origin function + #[doc = #origin_fn_indent_doc] + #visibility #function_origin // Cached function #(#attributes)* #visibility #signature_no_muts { @@ -285,6 +297,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { // Prime cached function #[doc = #prime_fn_indent_doc] #[allow(dead_code)] + #(#attributes)* #visibility #prime_sig { use cached::Cached; let key = #key_convert_block; diff --git a/src/stores/redis.rs b/src/stores/redis.rs index 9c67583..5878b7d 100644 --- a/src/stores/redis.rs +++ b/src/stores/redis.rs @@ -52,7 +52,7 @@ where pool_min_idle: None, pool_max_lifetime: None, pool_idle_timeout: None, - _phantom: PhantomData::default(), + _phantom: PhantomData, } } @@ -187,7 +187,7 @@ where pool: self.create_pool()?, namespace: self.namespace, prefix: self.prefix, - _phantom: PhantomData::default(), + _phantom: PhantomData, }) } } @@ -391,7 +391,7 @@ mod async_redis { namespace: DEFAULT_NAMESPACE.to_string(), prefix: prefix.as_ref().to_string(), connection_string: None, - _phantom: PhantomData::default(), + _phantom: PhantomData, } } @@ -494,7 +494,7 @@ mod async_redis { connection: self.create_connection_manager().await?, namespace: self.namespace, prefix: self.prefix, - _phantom: PhantomData::default(), + _phantom: PhantomData, }) } } From 39f47b44f4857c55a3f975d86aeea362a2ae3336 Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Wed, 30 Aug 2023 12:53:03 +0200 Subject: [PATCH 2/3] Remove unused deps --- CHANGELOG.md | 3 ++- cached_proc_macro/Cargo.toml | 5 ----- examples/wasm/Cargo.toml | 3 --- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ea83bc..799ccbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ ## [Unreleased] ## Added -- Also generate `*_origin` function for every cached function to allow calling the original function without caching. +- Also generate `*_origin` function for every cached function to allow calling the original function + without caching. It can be backward incompatible, if you have a function with the same name. ## Changed ## Removed diff --git a/cached_proc_macro/Cargo.toml b/cached_proc_macro/Cargo.toml index ee65197..e50e7a2 100644 --- a/cached_proc_macro/Cargo.toml +++ b/cached_proc_macro/Cargo.toml @@ -24,8 +24,3 @@ proc-macro2 = "1.0.49" [dependencies.syn] version = "1.0.27" features = ["full"] - -[dependencies.cached_proc_macro_types] -version = "0.1.0" -path = "../cached_proc_macro_types" - diff --git a/examples/wasm/Cargo.toml b/examples/wasm/Cargo.toml index f88dc1d..46f6f84 100644 --- a/examples/wasm/Cargo.toml +++ b/examples/wasm/Cargo.toml @@ -11,9 +11,6 @@ path ="../.." default_features = false features = ["proc_macro", "wasm"] -[dependencies.web-sys] -version = "0.3" - [dependencies.chrono] version = "0.4" features = [ From 89747ba9e13bcbe999837376f155adb7d0bc569c Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Wed, 30 Aug 2023 13:24:44 +0200 Subject: [PATCH 3/3] rename the origin to no_cache --- cached_proc_macro/src/cached.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/cached_proc_macro/src/cached.rs b/cached_proc_macro/src/cached.rs index 54fa71d..a76ce36 100644 --- a/cached_proc_macro/src/cached.rs +++ b/cached_proc_macro/src/cached.rs @@ -191,10 +191,10 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { result }; - let origin_fn_ident = Ident::new(&format!("{}_origin", &fn_ident), fn_ident.span()); + let no_cache_fn_ident = Ident::new(&format!("{}_no_cache", &fn_ident), fn_ident.span()); let lock; - let function_origin; + let function_no_cache; let function_call; let cache_type; if asyncness.is_some() { @@ -202,12 +202,12 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { let mut cache = #cache_ident.lock().await; }; - function_origin = quote! { - async fn #origin_fn_ident(#inputs) #output #body + function_no_cache = quote! { + async fn #no_cache_fn_ident(#inputs) #output #body }; function_call = quote! { - let result = #origin_fn_ident(#(#input_names),*).await; + let result = #no_cache_fn_ident(#(#input_names),*).await; }; cache_type = quote! { @@ -218,12 +218,12 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { let mut cache = #cache_ident.lock().unwrap(); }; - function_origin = quote! { - fn #origin_fn_ident(#inputs) #output #body + function_no_cache = quote! { + fn #no_cache_fn_ident(#inputs) #output #body }; function_call = quote! { - let result = #origin_fn_ident(#(#input_names),*); + let result = #no_cache_fn_ident(#(#input_names),*); }; cache_type = quote! { @@ -271,7 +271,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { // make cached static, cached function and prime cached function doc comments let cache_ident_doc = format!("Cached static for the [`{}`] function.", fn_ident); - let origin_fn_indent_doc = format!("Origin of the cached function [`{}`].", fn_ident); + let no_cache_fn_indent_doc = format!("Origin of the cached function [`{}`].", fn_ident); let prime_fn_indent_doc = format!("Primes the cached function [`{}`].", fn_ident); let cache_fn_doc_extra = format!( "This is a cached function that uses the [`{}`] cached static.", @@ -284,9 +284,9 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { // Cached static #[doc = #cache_ident_doc] #cache_type - // Origin function - #[doc = #origin_fn_indent_doc] - #visibility #function_origin + // No cache function (origin of the cached function) + #[doc = #no_cache_fn_indent_doc] + #visibility #function_no_cache // Cached function #(#attributes)* #visibility #signature_no_muts {