diff --git a/CHANGELOG.md b/CHANGELOG.md index c4a1289..a8da57c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ ## Changed ## Removed +## [0.48.0 / [cached_proc_macro[0.19.0]]] +## Added +- Add `CloneCached` trait with additional methods when the cache value type implements `Clone` +- Add `result_fallback` option to `cached` proc_macro to support re-using expired cache values + when utilizing an expiring cache store and a fallible function. +## Changed +## Removed + ## [0.47.0] ## Added ## Changed diff --git a/Cargo.toml b/Cargo.toml index 944635b..04d0c22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cached" -version = "0.47.0" +version = "0.48.0" authors = ["James Kominick "] description = "Generic cache implementations and simplified function memoization" repository = "https://github.com/jaemk/cached" @@ -29,7 +29,7 @@ redis_ahash = ["redis_store", "redis/ahash"] wasm = ["instant/wasm-bindgen"] [dependencies.cached_proc_macro] -version = "0.18.1" +version = "0.19.0" path = "cached_proc_macro" optional = true diff --git a/cached_proc_macro/Cargo.toml b/cached_proc_macro/Cargo.toml index 2cd91be..2f22b63 100644 --- a/cached_proc_macro/Cargo.toml +++ b/cached_proc_macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cached_proc_macro" -version = "0.18.1" +version = "0.19.0" authors = ["csos95 ", "James Kominick "] description = "Generic cache implementations and simplified function memoization" repository = "https://github.com/jaemk/cached" diff --git a/cached_proc_macro/src/cached.rs b/cached_proc_macro/src/cached.rs index 9dec94d..21534dd 100644 --- a/cached_proc_macro/src/cached.rs +++ b/cached_proc_macro/src/cached.rs @@ -250,39 +250,37 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { #function_call #set_cache_and_return } - } else { - if args.result_fallback { - quote! { - let old_val = { - #lock - let (result, has_expired) = cache.cache_get_expired(&key); - if let (Some(result), false) = (result, has_expired) { - #return_cache_block - } - result - }; - #function_call + } else if args.result_fallback { + quote! { + let old_val = { #lock - let result = match (result.is_err(), old_val) { - (true, Some(old_val)) => { - Ok(old_val) - } - _ => result - }; - #set_cache_and_return - } - } else { - quote! { - { - #lock - if let Some(result) = cache.cache_get(&key) { - #return_cache_block - } + let (result, has_expired) = cache.cache_get_expired(&key); + if let (Some(result), false) = (result, has_expired) { + #return_cache_block + } + result + }; + #function_call + #lock + let result = match (result.is_err(), old_val) { + (true, Some(old_val)) => { + Ok(old_val) } - #function_call + _ => result + }; + #set_cache_and_return + } + } else { + quote! { + { #lock - #set_cache_and_return + if let Some(result) = cache.cache_get(&key) { + #return_cache_block + } } + #function_call + #lock + #set_cache_and_return } }; diff --git a/cached_proc_macro/src/lib.rs b/cached_proc_macro/src/lib.rs index d1131c0..1b40fde 100644 --- a/cached_proc_macro/src/lib.rs +++ b/cached_proc_macro/src/lib.rs @@ -32,6 +32,7 @@ use proc_macro::TokenStream; /// - `result_fallback`: (optional, bool) If your function returns a `Result` and it fails, the cache will instead refresh the recently expired `Ok` value. /// In other words, refreshes are best-effort - returning `Ok` refreshes as usual but `Err` falls back to the last `Ok`. /// This is useful, for example, for keeping the last successful result of a network operation even during network disconnects. +/// *Note*, this option requires the cache type implements `CloneCached`. /// /// ## Note /// The `type`, `create`, `key`, and `convert` attributes must be in a `String` diff --git a/src/stores/redis.rs b/src/stores/redis.rs index fe15229..8e95dc7 100644 --- a/src/stores/redis.rs +++ b/src/stores/redis.rs @@ -584,7 +584,7 @@ mod async_redis { key, serde_json::to_string(&val) .map_err(|e| RedisCacheError::CacheSerializationError { error: e })?, - self.seconds as u64, + self.seconds, ) .ignore();