Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]
## Added
## Changed
- All timed/expiring caches now use std::time::Duration values instead of raw seconds/millis.
## Removed

## [0.55.1 / [cached_proc_macro[0.24.0]]]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn keyed(a: &str, b: &str) -> usize {

```rust
use cached::proc_macro::once;
use std::time::Duration;

/// Only cache the initial function call.
/// Function will be re-executed after the cache
Expand Down Expand Up @@ -124,6 +125,7 @@ fn doesnt_compile() -> Result<String, ()> {
```rust,no_run,ignore
use cached::proc_macro::io_cached;
use cached::AsyncRedisCache;
use std::time::Duration;
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Clone)]
Expand All @@ -141,7 +143,7 @@ enum ExampleError {
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
ty = "AsyncRedisCache<u64, String>",
create = r##" {
AsyncRedisCache::new("cached_redis_prefix", 1)
AsyncRedisCache::new("cached_redis_prefix", Duration::from_secs(1))
.set_refresh(true)
.build()
.await
Expand Down
5 changes: 2 additions & 3 deletions cached_proc_macro/src/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,12 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
}
(false, None, Some(time), None, None, time_refresh) => {
let cache_ty = quote! {cached::TimedCache<#cache_key_ty, #cache_value_ty>};
let cache_create =
quote! {cached::TimedCache::with_lifespan_and_refresh(#time, #time_refresh)};
let cache_create = quote! {cached::TimedCache::with_lifespan_and_refresh(Duration::from_secs(#time), #time_refresh)};
(cache_ty, cache_create)
}
(false, Some(size), Some(time), None, None, time_refresh) => {
let cache_ty = quote! {cached::TimedSizedCache<#cache_key_ty, #cache_value_ty>};
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, #time, #time_refresh)};
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, Duration::from_secs(#time), #time_refresh)};
(cache_ty, cache_create)
}
(false, None, None, None, None, _) => {
Expand Down
2 changes: 1 addition & 1 deletion cached_proc_macro/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub(super) fn gen_return_cache_block(
if let Some(time) = &time {
quote! {
let (created_sec, result) = result;
if now.duration_since(*created_sec).as_secs() < #time {
if now.duration_since(*created_sec) < Duration::from_secs(#time) {
#return_cache_block
}
}
Expand Down
10 changes: 5 additions & 5 deletions cached_proc_macro/src/io_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,19 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
match time_refresh {
Some(time_refresh) => {
if asyncness.is_some() {
quote! { cached::AsyncRedisCache::new(#cache_prefix, #time).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
} else {
quote! {
cached::RedisCache::new(#cache_prefix, #time).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
}
}
}
None => {
if asyncness.is_some() {
quote! { cached::AsyncRedisCache::new(#cache_prefix, #time).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
} else {
quote! {
cached::RedisCache::new(#cache_prefix, #time).build().expect("error constructing RedisCache in #[io_cached] macro")
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
}
}
}
Expand Down Expand Up @@ -297,7 +297,7 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
None => create,
Some(time) => {
quote! {
(#create).set_lifespan(#time)
(#create).set_lifespan(Duration::from_secs(#time))
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/expiring_sized_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use web_time::Instant;

#[tokio::main]
async fn main() {
let mut cache = ExpiringSizedCache::new(20_000);
let mut cache = ExpiringSizedCache::new(Duration::from_millis(20_000));
cache.size_limit(100);

let cache = Arc::new(RwLock::new(cache));
Expand Down
4 changes: 2 additions & 2 deletions examples/redis-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn cached_sleep_secs(secs: u64) -> Result<(), ExampleError> {
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
ty = "cached::AsyncRedisCache<u64, String>",
create = r##" {
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs", 1)
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs", Duration::from_secs(1))
.set_refresh(true)
.build()
.await
Expand Down Expand Up @@ -67,7 +67,7 @@ static CONFIG: Lazy<Config> = Lazy::new(Config::load);
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
ty = "cached::AsyncRedisCache<u64, String>",
create = r##" {
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs_config", 1)
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs_config", Duration::from_secs(1))
.set_refresh(true)
.set_connection_string(&CONFIG.conn_str)
.build()
Expand Down
2 changes: 1 addition & 1 deletion examples/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static CONFIG: Lazy<Config> = Lazy::new(Config::load);
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
ty = "cached::RedisCache<u64, String>",
create = r##" {
RedisCache::new("cache_redis_example_cached_sleep_secs_config", 1)
RedisCache::new("cache_redis_example_cached_sleep_secs_config", Duration::from_secs(1))
.set_refresh(true)
.set_connection_string(&CONFIG.conn_str)
.build()
Expand Down
2 changes: 1 addition & 1 deletion examples/wasm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn app() -> Html {

#[cached(
ty = "TimedCache<String, Option<String>>",
create = "{ TimedCache::with_lifespan(5) }"
create = "{ TimedCache::with_lifespan(std::time::Duration::from_secs(5)) }"
)]
async fn fetch(body: String) -> Option<String> {
Request::post(URL)
Expand Down
24 changes: 14 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn keyed(a: &str, b: &str) -> usize {

```rust,no_run
use cached::proc_macro::once;
use std::time::Duration;

/// Only cache the initial function call.
/// Function will be re-executed after the cache
Expand Down Expand Up @@ -126,6 +127,7 @@ fn doesnt_compile() -> Result<String, ()> {
```rust,no_run,ignore
use cached::proc_macro::io_cached;
use cached::AsyncRedisCache;
use std::time::Duration;
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Clone)]
Expand All @@ -143,7 +145,7 @@ enum ExampleError {
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
ty = "AsyncRedisCache<u64, String>",
create = r##" {
AsyncRedisCache::new("cached_redis_prefix", 1)
AsyncRedisCache::new("cached_redis_prefix", Duration::from_secs(1))
.set_refresh(true)
.build()
.await
Expand Down Expand Up @@ -220,6 +222,8 @@ Due to the requirements of storing arguments and return values in a global cache
#[doc(hidden)]
pub extern crate once_cell;

use std::time::Duration;

#[cfg(feature = "proc_macro")]
#[cfg_attr(docsrs, doc(cfg(feature = "proc_macro")))]
pub use proc_macro::Return;
Expand Down Expand Up @@ -372,20 +376,20 @@ pub trait Cached<K, V> {
}

/// Return the lifespan of cached values (time to eviction)
fn cache_lifespan(&self) -> Option<u64> {
fn cache_lifespan(&self) -> Option<Duration> {
None
}

/// Set the lifespan of cached values, returns the old value
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
fn cache_set_lifespan(&mut self, _ttl: Duration) -> Option<Duration> {
None
}

/// Remove the lifespan for cached values, returns the old value.
///
/// For cache implementations that don't support retaining values indefinitely, this method is
/// a no-op.
fn cache_unset_lifespan(&mut self) -> Option<u64> {
fn cache_unset_lifespan(&mut self) -> Option<Duration> {
None
}
}
Expand Down Expand Up @@ -445,20 +449,20 @@ pub trait IOCached<K, V> {
fn cache_set_refresh(&mut self, refresh: bool) -> bool;

/// Return the lifespan of cached values (time to eviction)
fn cache_lifespan(&self) -> Option<u64> {
fn cache_lifespan(&self) -> Option<Duration> {
None
}

/// Set the lifespan of cached values, returns the old value.
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
fn cache_set_lifespan(&mut self, _ttl: Duration) -> Option<Duration> {
None
}

/// Remove the lifespan for cached values, returns the old value.
///
/// For cache implementations that don't support retaining values indefinitely, this method is
/// a no-op.
fn cache_unset_lifespan(&mut self) -> Option<u64> {
fn cache_unset_lifespan(&mut self) -> Option<Duration> {
None
}
}
Expand All @@ -479,20 +483,20 @@ pub trait IOCachedAsync<K, V> {
fn cache_set_refresh(&mut self, refresh: bool) -> bool;

/// Return the lifespan of cached values (time to eviction)
fn cache_lifespan(&self) -> Option<u64> {
fn cache_lifespan(&self) -> Option<Duration> {
None
}

/// Set the lifespan of cached values, returns the old value
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
fn cache_set_lifespan(&mut self, _ttl: Duration) -> Option<Duration> {
None
}

/// Remove the lifespan for cached values, returns the old value.
///
/// For cache implementations that don't support retaining values indefinitely, this method is
/// a no-op.
fn cache_unset_lifespan(&mut self) -> Option<u64> {
fn cache_unset_lifespan(&mut self) -> Option<Duration> {
None
}
}
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ cached! {
}

cached! {
TIMED_REDIS: RedisCache<u32, u32> = RedisCache::with_lifespan(2);
TIMED_REDIS: RedisCache<u32, u32> = RedisCache::with_lifespan(Duration::from_secs(2));
fn cached_timed_redis(n: u32) -> u32 = {
sleep(Duration::new(3, 0));
n
Expand Down
1 change: 1 addition & 0 deletions src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ fn keyed(a: &str, b: &str) -> usize {

```rust,no_run
use cached::proc_macro::once;
use std::time::Duration;

/// Only cache the initial function call.
/// Function will be re-executed after the cache
Expand Down
Loading
Loading