Skip to content

Commit 055778f

Browse files
Copilotjaemk
andauthored
feat: add time_stores feature flag and re-export web_time as cached::time (#259)
- Add a new 'time_stores' feature flag to gate all time-dependent cache types so the crate can compile for targets without system time (e.g. wasm32-unknown-unknown without WASI) - Expose web_time as a public cached::time module re-export in src/lib.rs so users can access time primitives via cached::time without a direct web_time dependency - Keep the old #[doc(hidden)] pub use web_time re-export for backward compat - Replace all internal web_time:: imports with crate::time:: throughout - Update doc-comment examples and example files to use cached::time - Restructure tests/cached.rs with feature-gated modules - Enable 'time_stores' in the default feature set (no user-visible breakage) - Bump version: cached 0.59.0, cached_proc_macro 0.27.0 - Regenerate README.md; run cargo fmt Co-authored-by: James Kominick <james@kominick.com>
1 parent a2676a5 commit 055778f

31 files changed

Lines changed: 931 additions & 875 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
## Changed
66
## Removed
77

8+
## [0.59.0 / [cached_proc_macro[0.27.0]]]
9+
## Added
10+
## Changed
11+
- Fix `examples/wasm` build: add `time_stores` feature to the `cached` dependency (required when using `default-features = false` with `TimedCache`)
12+
## Removed
13+
814
## [0.58.0]
915
## Added
1016
- Add `redis_async_cache` feature for Redis client-side caching support via the RESP3 protocol

Cargo.toml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cached"
3-
version = "0.58.0"
3+
version = "0.59.0"
44
authors = ["James Kominick <james@kominick.com>"]
55
description = "Generic cache implementations and simplified function memoization"
66
repository = "https://github.com/jaemk/cached"
@@ -16,7 +16,7 @@ all-features = true
1616
rustdoc-args = ["--cfg", "docsrs"]
1717

1818
[features]
19-
default = ["proc_macro", "ahash"]
19+
default = ["proc_macro", "ahash", "time_stores"]
2020
proc_macro = ["cached_proc_macro", "cached_proc_macro_types"]
2121
ahash = ["dep:ahash", "hashbrown/default"]
2222
async = ["futures", "tokio", "async-trait"]
@@ -39,9 +39,10 @@ redis_async_cache = [
3939
redis_ahash = ["redis_store", "redis/ahash"]
4040
disk_store = ["sled", "serde", "rmp-serde", "directories"]
4141
wasm = []
42+
time_stores = []
4243

4344
[dependencies.cached_proc_macro]
44-
version = "0.26.0"
45+
version = "0.27.0"
4546
path = "cached_proc_macro"
4647
optional = true
4748

@@ -152,7 +153,15 @@ required-features = ["async", "proc_macro"]
152153

153154
[[example]]
154155
name = "expiring_sized_cache"
155-
required-features = ["async_tokio_rt_multi_thread"]
156+
required-features = ["async_tokio_rt_multi_thread", "time_stores"]
157+
158+
[[example]]
159+
name = "basic_proc_macro"
160+
required-features = ["time_stores", "proc_macro"]
161+
162+
[[example]]
163+
name = "kitchen_sink_proc_macro"
164+
required-features = ["time_stores", "proc_macro"]
156165

157166
[[example]]
158167
name = "disk"

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ of un-cached arguments, specify `#[cached(sync_writes = "default")]` / `#[once(s
2222

2323
**Features**
2424

25-
- `default`: Include `proc_macro` and `ahash` features
25+
- `default`: Include `proc_macro`, `ahash`, and `time_stores` features
2626
- `proc_macro`: Include proc macros
2727
- `ahash`: Enable the optional `ahash` hasher as default hashing algorithm.
2828
- `async`: Include support for async functions and async cache stores
@@ -36,6 +36,8 @@ of un-cached arguments, specify `#[cached(sync_writes = "default")]` / `#[once(s
3636
- `disk_store`: Include disk cache store
3737
- `wasm`: Enable WASM support. Note that this feature is incompatible with `tokio`'s multi-thread
3838
runtime (`async_tokio_rt_multi_thread`) and all Redis features (`redis_store`, `redis_smol`, `redis_tokio`, `redis_ahash`)
39+
- `time_stores`: Include time-based cache stores ([`TimedCache`], [`TimedSizedCache`], and [`stores::ExpiringSizedCache`]).
40+
Disable this feature when targeting environments without system time support (e.g. `wasm32-unknown-unknown` without WASI or JS).
3941

4042
The procedural macros (`#[cached]`, `#[once]`, `#[io_cached]`) offer more features, including async support.
4143
See the [`proc_macro`](crate::proc_macro) and [`macros`](crate::macros) modules for more samples, and the
@@ -65,7 +67,7 @@ fn fib(n: u64) -> u64 {
6567

6668
```rust
6769
use std::thread::sleep;
68-
use std::time::Duration;
70+
use cached::time::Duration;
6971
use cached::proc_macro::cached;
7072
use cached::SizedCache;
7173

@@ -124,7 +126,7 @@ fn doesnt_compile() -> Result<String, ()> {
124126
```rust,no_run,ignore
125127
use cached::proc_macro::io_cached;
126128
use cached::AsyncRedisCache;
127-
use std::time::Duration;
129+
use cached::time::Duration;
128130
use thiserror::Error;
129131
130132
#[derive(Error, Debug, PartialEq, Clone)]
@@ -150,7 +152,7 @@ enum ExampleError {
150152
} "##
151153
)]
152154
async fn async_cached_sleep_secs(secs: u64) -> Result<String, ExampleError> {
153-
std::thread::sleep(std::time::Duration::from_secs(secs));
155+
std::thread::sleep(cached::time::Duration::from_secs(secs));
154156
Ok(secs.to_string())
155157
}
156158
```
@@ -179,7 +181,7 @@ enum ExampleError {
179181
disk = true
180182
)]
181183
fn cached_sleep_secs(secs: u64) -> Result<String, ExampleError> {
182-
std::thread::sleep(std::time::Duration::from_secs(secs));
184+
std::thread::sleep(cached::time::Duration::from_secs(secs));
183185
Ok(secs.to_string())
184186
}
185187
```

cached_proc_macro/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cached_proc_macro"
3-
version = "0.26.0"
3+
version = "0.27.0"
44
authors = ["csos95 <csoscss@gmail.com>", "James Kominick <james@kominick.com>"]
55
description = "Generic cache implementations and simplified function memoization"
66
repository = "https://github.com/jaemk/cached"

cached_proc_macro/src/cached.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +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 = quote! {cached::TimedCache::with_lifespan_and_refresh(std::time::Duration::from_secs(#time), #time_refresh)};
128+
let cache_create = quote! {cached::TimedCache::with_lifespan_and_refresh(::cached::time::Duration::from_secs(#time), #time_refresh)};
129129
(cache_ty, cache_create)
130130
}
131131
(false, Some(size), Some(time), None, None, time_refresh) => {
132132
let cache_ty = quote! {cached::TimedSizedCache<#cache_key_ty, #cache_value_ty>};
133-
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, std::time::Duration::from_secs(#time), #time_refresh)};
133+
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, ::cached::time::Duration::from_secs(#time), #time_refresh)};
134134
(cache_ty, cache_create)
135135
}
136136
(false, None, None, None, None, _) => {

cached_proc_macro/src/helpers.rs

Lines changed: 1 addition & 1 deletion
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) < std::time::Duration::from_secs(#time) {
204+
if now.duration_since(*created_sec) < ::cached::time::Duration::from_secs(#time) {
205205
#return_cache_block
206206
}
207207
}

cached_proc_macro/src/io_cached.rs

Lines changed: 5 additions & 5 deletions
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, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
228+
quote! { cached::AsyncRedisCache::new(#cache_prefix, ::cached::time::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, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
231+
cached::RedisCache::new(#cache_prefix, ::cached::time::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, std::time::Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
237+
quote! { cached::AsyncRedisCache::new(#cache_prefix, ::cached::time::Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
238238
} else {
239239
quote! {
240-
cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
240+
cached::RedisCache::new(#cache_prefix, ::cached::time::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(std::time::Duration::from_secs(#time))
300+
(#create).set_lifespan(::cached::time::Duration::from_secs(#time))
301301
}
302302
}
303303
};

cached_proc_macro/src/once.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
8080
let (cache_ty, cache_create) = match &args.time {
8181
None => (quote! { Option<#cache_value_ty> }, quote! { None }),
8282
Some(_) => (
83-
quote! { Option<(::cached::web_time::Instant, #cache_value_ty)> },
83+
quote! { Option<(::cached::time::Instant, #cache_value_ty)> },
8484
quote! { None },
8585
),
8686
};
@@ -255,21 +255,27 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
255255
fill_in_attributes(&mut attributes, cache_fn_doc_extra);
256256

257257
// put it all together
258+
let now_block = if args.time.is_some() {
259+
quote! { let now = ::cached::time::Instant::now(); }
260+
} else {
261+
quote! {}
262+
};
263+
258264
let expanded = quote! {
259265
// Cached static
260266
#[doc = #cache_ident_doc]
261267
#ty
262268
// Cached function
263269
#(#attributes)*
264270
#visibility #signature_no_muts {
265-
let now = ::cached::web_time::Instant::now();
271+
#now_block
266272
#do_set_return_block
267273
}
268274
// Prime cached function
269275
#[doc = #prime_fn_indent_doc]
270276
#[allow(dead_code)]
271277
#visibility #prime_sig {
272-
let now = ::cached::web_time::Instant::now();
278+
#now_block
273279
#prime_do_set_return_block
274280
}
275281
};

examples/async_std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use async_std::task::sleep;
22
use cached::proc_macro::cached;
33
use cached::proc_macro::once;
4-
use std::time::Duration;
4+
use cached::time::Duration;
55

66
async fn sleep_secs(secs: u64) {
77
sleep(Duration::from_secs(secs)).await;

examples/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#[macro_use]
22
extern crate cached;
33

4+
use cached::time::{Duration, Instant};
45
use std::thread::sleep;
5-
use std::time::{Duration, Instant};
66

77
use cached::SizedCache;
88

0 commit comments

Comments
 (0)