1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use TokenStream;
use ;
/// Derive `CacheEntity` metadata for database result-cache helpers.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache_db::{CacheEntity, HydraCacheEntity};
///
/// #[derive(HydraCacheEntity)]
/// #[hydracache(entity = "user", collection = "users", id = i64)]
/// struct User {
/// id: i64,
/// name: String,
/// }
///
/// assert_eq!(User::cache_key_for(&42), "user:42");
/// ```
/// Build a `QueryCachePolicy` with less boilerplate.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache_db::{query_cache_policy, QueryCachePolicy};
///
/// let user_id = 42_i64;
/// let policy = query_cache_policy!(
/// name = "load-user",
/// entity = User,
/// id = user_id,
/// ttl_secs = 60,
/// );
/// ```
/// Cache an ordinary fallible async loader with explicit local-cache metadata.
///
/// The macro builds `CacheOptions` and calls `HydraCache::get_or_load`.
/// `cache`, `key`, and `load` are required. `tag = ...` can be repeated,
/// `tags = ...` accepts any iterable accepted by `CacheOptions::tags`, and
/// either `ttl = Duration` or `ttl_secs = u64` can be supplied.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache::{cacheable, CacheKeyBuilder, HydraCache, TagSet};
///
/// let cache = HydraCache::local().build();
/// let user_id = 42_u64;
/// let key = CacheKeyBuilder::new().entity("user", user_id).build_string();
///
/// let value = cacheable!(
/// cache = cache,
/// key = key.as_str(),
/// tags = TagSet::new().tag("users").entity("user", user_id),
/// ttl_secs = 60,
/// load = move || async move { Ok::<_, std::io::Error>(user_id) },
/// )
/// .await?;
/// ```
/// Cache an ordinary async loader that cannot fail in application terms.
///
/// The macro builds `CacheOptions` and calls `HydraCache::get_or_insert_with`.
/// Use it when the loader returns `T` instead of `Result<T, E>`.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache::{cacheable_infallible, HydraCache};
///
/// let cache = HydraCache::local().build();
///
/// let value = cacheable_infallible!(
/// cache = cache,
/// key = "expensive:42",
/// tags = ["expensive"],
/// ttl_secs = 60,
/// load = || async { 42_u64 },
/// )
/// .await?;
/// ```