Skip to content

Commit

Permalink
Merge pull request #166 from omid/add_origin_function
Browse files Browse the repository at this point in the history
Add origin function
  • Loading branch information
jaemk authored Sep 5, 2023
2 parents f1bcfd6 + 89747ba commit ce6c7cc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 0 additions & 5 deletions cached_proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

29 changes: 21 additions & 8 deletions cached_proc_macro/src/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,40 @@ 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! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<::cached::async_sync::Mutex<#cache_ty>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_sync::Mutex::new(#cache_create));
};
} 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! {
Expand All @@ -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
};
Expand Down Expand Up @@ -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.",
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions examples/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ path ="../.."
default_features = false
features = ["proc_macro", "wasm"]

[dependencies.web-sys]
version = "0.3"

[dependencies.chrono]
version = "0.4"
features = [
Expand Down
8 changes: 4 additions & 4 deletions src/stores/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
pool_min_idle: None,
pool_max_lifetime: None,
pool_idle_timeout: None,
_phantom: PhantomData::default(),
_phantom: PhantomData,
}
}

Expand Down Expand Up @@ -187,7 +187,7 @@ where
pool: self.create_pool()?,
namespace: self.namespace,
prefix: self.prefix,
_phantom: PhantomData::default(),
_phantom: PhantomData,
})
}
}
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -494,7 +494,7 @@ mod async_redis {
connection: self.create_connection_manager().await?,
namespace: self.namespace,
prefix: self.prefix,
_phantom: PhantomData::default(),
_phantom: PhantomData,
})
}
}
Expand Down

0 comments on commit ce6c7cc

Please sign in to comment.