Repository: jaemk/cached (language: rust)

Issue:
Also generate the fresh data function
What do you think that also generate the third function to always fetch the fresh data?

Currently, the macro generates `function` and `function_prime_cache`. I'm suggesting to generate the third function to call the function to bypass the cache completely, like `function_bypass_cache` or `cached_function_origin` or so.

It happened to me several times, that I need to have a cached version normally but in some specific cases bypass the cache system and always have the fresh data (with or without caching, both cases). At the moment, I always call `_prime_cache`, even when I don't need the cached data anymore.

The cherry on top will be that the generated code and binary size can be smaller, since currently we generate two `inner` functions, but then in this case we just call the `origin` function instead.

If you agree, just suggest a name for the function, then I'll create a PR.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 39cdcf4..799ccbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,13 @@
 ## Changed
 ## Removed
 
+## [Unreleased]
+## Added
+- 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
+
 ## [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/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/cached_proc_macro/src/cached.rs b/cached_proc_macro/src/cached.rs
index 6394f58..a76ce36 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 no_cache_fn_ident = Ident::new(&format!("{}_no_cache", &fn_ident), fn_ident.span());
+
     let lock;
+    let function_no_cache;
     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_no_cache = quote! {
+            async fn #no_cache_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 = #no_cache_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_no_cache = quote! {
+            fn #no_cache_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 = #no_cache_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 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.",
@@ -275,6 +284,9 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
         // Cached static
         #[doc = #cache_ident_doc]
         #cache_type
+        // 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 {
@@ -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/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 = [
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,
             })
         }
     }


Test patch: