Skip to content

Commit b5c9321

Browse files
committed
Use Duration instead of raw seconds and millis
closes #74
1 parent d9fe57c commit b5c9321

19 files changed

+265
-233
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
target: wasm32-unknown-unknown
2424

2525
- name: Cache cargo dir
26-
uses: actions/cache@v2
26+
uses: actions/cache@v4
2727
env:
2828
cache-name: cache-cargo-dir
2929
with:
@@ -35,7 +35,7 @@ jobs:
3535
${{ runner.os }}-
3636
3737
- name: Cache target dir
38-
uses: actions/cache@v2
38+
uses: actions/cache@v4
3939
env:
4040
cache-name: cache-target-dir
4141
with:

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
## Added
55
## Changed
6+
- All timed/expiring caches now use std::time::Duration values instead of raw seconds/millis.
67
## Removed
78

89
## [0.55.1 / [cached_proc_macro[0.24.0]]]

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ fn doesnt_compile() -> Result<String, ()> {
124124
```rust,no_run,ignore
125125
use cached::proc_macro::io_cached;
126126
use cached::AsyncRedisCache;
127+
use std::time::Duration;
127128
use thiserror::Error;
128129
129130
#[derive(Error, Debug, PartialEq, Clone)]
@@ -141,7 +142,7 @@ enum ExampleError {
141142
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
142143
ty = "AsyncRedisCache<u64, String>",
143144
create = r##" {
144-
AsyncRedisCache::new("cached_redis_prefix", 1)
145+
AsyncRedisCache::new("cached_redis_prefix", Duration::from_secs(1))
145146
.set_refresh(true)
146147
.build()
147148
.await

cached_proc_macro/src/cached.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,12 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
125125
}
126126
(false, None, Some(time), None, None, time_refresh) => {
127127
let cache_ty = quote! {cached::TimedCache<#cache_key_ty, #cache_value_ty>};
128-
let cache_create =
129-
quote! {cached::TimedCache::with_lifespan_and_refresh(#time, #time_refresh)};
128+
let cache_create = quote! {cached::TimedCache::with_lifespan_and_refresh(Duration::from_secs(#time), #time_refresh)};
130129
(cache_ty, cache_create)
131130
}
132131
(false, Some(size), Some(time), None, None, time_refresh) => {
133132
let cache_ty = quote! {cached::TimedSizedCache<#cache_key_ty, #cache_value_ty>};
134-
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, #time, #time_refresh)};
133+
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, Duration::from_secs(#time), #time_refresh)};
135134
(cache_ty, cache_create)
136135
}
137136
(false, None, None, None, None, _) => {

cached_proc_macro/src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub(super) fn gen_return_cache_block(
201201
if let Some(time) = &time {
202202
quote! {
203203
let (created_sec, result) = result;
204-
if now.duration_since(*created_sec).as_secs() < #time {
204+
if now.duration_since(*created_sec) < Duration::from_secs(#time) {
205205
#return_cache_block
206206
}
207207
}

cached_proc_macro/src/io_cached.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,19 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
225225
match time_refresh {
226226
Some(time_refresh) => {
227227
if asyncness.is_some() {
228-
quote! { cached::AsyncRedisCache::new(#cache_prefix, #time).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
228+
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
229229
} else {
230230
quote! {
231-
cached::RedisCache::new(#cache_prefix, #time).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
231+
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
232232
}
233233
}
234234
}
235235
None => {
236236
if asyncness.is_some() {
237-
quote! { cached::AsyncRedisCache::new(#cache_prefix, #time).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
237+
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
238238
} else {
239239
quote! {
240-
cached::RedisCache::new(#cache_prefix, #time).build().expect("error constructing RedisCache in #[io_cached] macro")
240+
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
241241
}
242242
}
243243
}
@@ -297,7 +297,7 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
297297
None => create,
298298
Some(time) => {
299299
quote! {
300-
(#create).set_lifespan(#time)
300+
(#create).set_lifespan(Duration::from_secs(#time))
301301
}
302302
}
303303
};

examples/expiring_sized_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use web_time::Instant;
66

77
#[tokio::main]
88
async fn main() {
9-
let mut cache = ExpiringSizedCache::new(20_000);
9+
let mut cache = ExpiringSizedCache::new(Duration::from_millis(20_000));
1010
cache.size_limit(100);
1111

1212
let cache = Arc::new(RwLock::new(cache));

examples/redis-async.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn cached_sleep_secs(secs: u64) -> Result<(), ExampleError> {
3838
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
3939
ty = "cached::AsyncRedisCache<u64, String>",
4040
create = r##" {
41-
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs", 1)
41+
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs", Duration::from_secs(1))
4242
.set_refresh(true)
4343
.build()
4444
.await
@@ -67,7 +67,7 @@ static CONFIG: Lazy<Config> = Lazy::new(Config::load);
6767
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
6868
ty = "cached::AsyncRedisCache<u64, String>",
6969
create = r##" {
70-
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs_config", 1)
70+
AsyncRedisCache::new("cache_redis_example_cached_sleep_secs_config", Duration::from_secs(1))
7171
.set_refresh(true)
7272
.set_connection_string(&CONFIG.conn_str)
7373
.build()

examples/redis.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static CONFIG: Lazy<Config> = Lazy::new(Config::load);
6363
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
6464
ty = "cached::RedisCache<u64, String>",
6565
create = r##" {
66-
RedisCache::new("cache_redis_example_cached_sleep_secs_config", 1)
66+
RedisCache::new("cache_redis_example_cached_sleep_secs_config", Duration::from_secs(1))
6767
.set_refresh(true)
6868
.set_connection_string(&CONFIG.conn_str)
6969
.build()

examples/wasm/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn app() -> Html {
4747

4848
#[cached(
4949
ty = "TimedCache<String, Option<String>>",
50-
create = "{ TimedCache::with_lifespan(5) }"
50+
create = "{ TimedCache::with_lifespan(std::time::Duration::from_secs(5)) }"
5151
)]
5252
async fn fetch(body: String) -> Option<String> {
5353
Request::post(URL)

src/lib.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fn keyed(a: &str, b: &str) -> usize {
8787
8888
```rust,no_run
8989
use cached::proc_macro::once;
90+
use std::time::Duration;
9091
9192
/// Only cache the initial function call.
9293
/// Function will be re-executed after the cache
@@ -126,6 +127,7 @@ fn doesnt_compile() -> Result<String, ()> {
126127
```rust,no_run,ignore
127128
use cached::proc_macro::io_cached;
128129
use cached::AsyncRedisCache;
130+
use std::time::Duration;
129131
use thiserror::Error;
130132
131133
#[derive(Error, Debug, PartialEq, Clone)]
@@ -143,7 +145,7 @@ enum ExampleError {
143145
map_error = r##"|e| ExampleError::RedisError(format!("{:?}", e))"##,
144146
ty = "AsyncRedisCache<u64, String>",
145147
create = r##" {
146-
AsyncRedisCache::new("cached_redis_prefix", 1)
148+
AsyncRedisCache::new("cached_redis_prefix", Duration::from_secs(1))
147149
.set_refresh(true)
148150
.build()
149151
.await
@@ -220,6 +222,8 @@ Due to the requirements of storing arguments and return values in a global cache
220222
#[doc(hidden)]
221223
pub extern crate once_cell;
222224

225+
use std::time::Duration;
226+
223227
#[cfg(feature = "proc_macro")]
224228
#[cfg_attr(docsrs, doc(cfg(feature = "proc_macro")))]
225229
pub use proc_macro::Return;
@@ -372,20 +376,20 @@ pub trait Cached<K, V> {
372376
}
373377

374378
/// Return the lifespan of cached values (time to eviction)
375-
fn cache_lifespan(&self) -> Option<u64> {
379+
fn cache_lifespan(&self) -> Option<Duration> {
376380
None
377381
}
378382

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

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

447451
/// Return the lifespan of cached values (time to eviction)
448-
fn cache_lifespan(&self) -> Option<u64> {
452+
fn cache_lifespan(&self) -> Option<Duration> {
449453
None
450454
}
451455

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

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

481485
/// Return the lifespan of cached values (time to eviction)
482-
fn cache_lifespan(&self) -> Option<u64> {
486+
fn cache_lifespan(&self) -> Option<Duration> {
483487
None
484488
}
485489

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

491495
/// Remove the lifespan for cached values, returns the old value.
492496
///
493497
/// For cache implementations that don't support retaining values indefinitely, this method is
494498
/// a no-op.
495-
fn cache_unset_lifespan(&mut self) -> Option<u64> {
499+
fn cache_unset_lifespan(&mut self) -> Option<Duration> {
496500
None
497501
}
498502
}

src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ cached! {
121121
}
122122
123123
cached! {
124-
TIMED_REDIS: RedisCache<u32, u32> = RedisCache::with_lifespan(2);
124+
TIMED_REDIS: RedisCache<u32, u32> = RedisCache::with_lifespan(Duration::from_secs(2));
125125
fn cached_timed_redis(n: u32) -> u32 = {
126126
sleep(Duration::new(3, 0));
127127
n

src/proc_macro.rs

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ fn keyed(a: &str, b: &str) -> usize {
226226
227227
```rust,no_run
228228
use cached::proc_macro::once;
229+
use std::time::Duration;
229230
230231
/// Only cache the initial function call.
231232
/// Function will be re-executed after the cache

0 commit comments

Comments
 (0)