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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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")]
/// struct User {
/// #[hydracache(id)]
/// id: i64,
/// name: String,
/// }
///
/// assert_eq!(User::cache_key_for(&42), "user:42");
/// ```
///
/// `#[hydracache(id = Type)]` on the struct remains supported when the id type
/// cannot be inferred from one named field.
/// Build a `QueryCachePolicy` with less boilerplate.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache_db::query_cache_policy;
///
/// let user_id = 42_i64;
/// let policy = query_cache_policy!(
/// preset = read_mostly,
/// name = "load-user",
/// entity = User,
/// id = user_id,
/// refresh_ahead_secs = 10,
/// stale_while_revalidate_secs = 300,
/// );
///
/// let search = query_cache_policy!(
/// name = "search-users",
/// key_segments = ["tenant", tenant_id, "q", query, "page", page],
/// tag_segments = [["tenant", tenant_id], ["users"]],
/// ttl_secs = 30,
/// );
/// ```
/// Build a reusable `PreparedQueryPolicy` with less boilerplate.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache_db::prepared_query_policy;
///
/// let load_user = prepared_query_policy!(
/// per_entity = User,
/// name = "load-user",
/// ttl_secs = 300,
/// );
/// ```
/// Cache an ordinary async function with explicit local-cache metadata.
///
/// The decorated function must be async, return `Result<T, E>`, and receive the
/// cache as an explicit argument referenced by `cache = ...`. The generated
/// wrapper returns `hydracache::CacheResult<T>` because cache errors can also be
/// produced outside the user loader.
///
/// # Example
///
/// ```rust,ignore
/// use hydracache::{cacheable, HydraCache};
///
/// #[cacheable(
/// cache = cache,
/// key_segments = ["profile", profile_id],
/// tag_segments = [["profile", profile_id], ["profiles"]],
/// ttl_secs = 60
/// )]
/// async fn load_profile(
/// cache: &HydraCache,
/// profile_id: u64,
/// ) -> Result<Profile, LoadError> {
/// repo_load_profile(profile_id).await
/// }
/// ```
/// 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_loader, 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_loader!(
/// 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?;
/// ```