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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Database-neutral query result caching helpers for HydraCache.
//!
//! This crate is intentionally a thin runtime adapter. It does not replace a
//! database client, ORM, or query builder. Callers keep their database library
//! as the query authority and provide an explicit cache key, tags, and TTL
//! around the operation they want to cache.
//!
//! # Example
//!
//! ```rust
//! use hydracache::HydraCache;
//! use hydracache_db::{
//! DbCache, HydraCacheEntity, PreparedQueryPolicy, QueryCachePolicy, RefreshPolicy,
//! };
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, HydraCacheEntity)]
//! #[hydracache(entity = "user", collection = "users")]
//! struct User {
//! #[hydracache(id)]
//! id: i64,
//! name: String,
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> hydracache_db::Result<()> {
//! let local = HydraCache::local().build();
//!
//! // The adapter wraps the local HydraCache instance. The namespace becomes
//! // part of the physical cache key, so key("user:42") is stored as
//! // "db:user:42".
//! let queries = DbCache::new(local, "db");
//!
//! let policy = QueryCachePolicy::read_mostly()
//! // Metadata helper: key "user:42", tag "user:42", and tag "users".
//! .for_cache_entity::<User>(42)
//! .with_name("load-user")
//! .refresh_policy(
//! RefreshPolicy::new()
//! .refresh_ahead(std::time::Duration::from_secs(10))
//! .stale_while_revalidate(std::time::Duration::from_secs(300)),
//! );
//!
//! let user = queries
//! .cached_with::<User>(policy)
//! .load(|| async {
//! // This loader runs only on a cache miss. On a cache hit, HydraCache
//! // returns the cached User and this database code is not executed.
//! Ok::<_, std::io::Error>(User {
//! id: 42,
//! name: "Ada".to_owned(),
//! })
//! })
//! .await?;
//!
//! assert_eq!(user.id, 42);
//! # Ok(())
//! # }
//! ```
//!
//! For hot repository methods, prepare stable metadata once and bind only the
//! dynamic id on each call:
//!
//! ```rust
//! use hydracache::HydraCache;
//! use hydracache_db::{DbCache, HydraCacheEntity, PreparedQueryPolicy};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, HydraCacheEntity)]
//! #[hydracache(entity = "user", collection = "users")]
//! struct User {
//! #[hydracache(id)]
//! id: i64,
//! name: String,
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> hydracache_db::Result<()> {
//! let queries = DbCache::new(HydraCache::local().build(), "db");
//! let load_user = queries.prepare::<User>(
//! PreparedQueryPolicy::per_entity()
//! .cache_entity::<User>()
//! .with_name("load-user"),
//! );
//!
//! let user = load_user
//! .load_id(42, || async {
//! Ok::<_, std::io::Error>(User {
//! id: 42,
//! name: "Ada".to_owned(),
//! })
//! })
//! .await?;
//!
//! assert_eq!(user.id, 42);
//! # Ok(())
//! # }
//! ```
//!
//! For compact policy construction, use [`query_cache_policy!`]:
//!
//! ```rust
//! use hydracache_db::{query_cache_policy, CacheEntity};
//!
//! struct User;
//!
//! impl CacheEntity for User {
//! type Id = i64;
//!
//! const ENTITY: &'static str = "user";
//! const COLLECTION: Option<&'static str> = Some("users");
//! }
//!
//! 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,
//! );
//!
//! assert_eq!(policy.name(), Some("load-user"));
//! assert_eq!(policy.key_value(), Some("user:42"));
//! assert!(policy.refresh_policy_value().is_some());
//!
//! let search = query_cache_policy!(
//! name = "search-users",
//! key_segments = ["tenant", 7_u64, "q", "ada:lovelace", "page", 1_u32],
//! tag_segments = [["tenant", 7_u64], ["users"]],
//! ttl_secs = 30,
//! );
//!
//! assert_eq!(
//! search.key_value(),
//! Some("tenant:7:q:ada%3Alovelace:page:1")
//! );
//! assert_eq!(search.tags_value(), &["tenant:7".to_owned(), "users".to_owned()]);
//! ```
//!
//! For write paths, stage invalidations during repository work and execute them
//! only after the database transaction commits:
//!
//! ```rust
//! use hydracache::HydraCache;
//! use hydracache_db::{HydraCacheEntity, InvalidationPlan};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Clone, Serialize, Deserialize, HydraCacheEntity)]
//! #[hydracache(entity = "user", collection = "users")]
//! struct User {
//! #[hydracache(id)]
//! id: i64,
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> hydracache::CacheResult<()> {
//! let cache = HydraCache::local().build();
//! let pending = InvalidationPlan::new().cache_entity::<User>(42);
//!
//! // tx.update_user(42).await?;
//! // tx.commit().await?;
//!
//! let report = pending.execute(&cache).await?;
//! assert_eq!(report.tag_count, 2);
//! # Ok(())
//! # }
//! ```
extern crate self as hydracache_db;
pub use CacheEntity;
pub use ;
pub use ;
pub use CacheKeyBuilder;
pub use ;
pub use ;
pub use ;
pub use ;
pub use QueryCachePolicy;
pub use PreparedQueryPolicy;
pub use ;
pub use ;
pub use sqlite_hook_drift;
pub use ;
pub use ;
pub use ;
/// Database-facing alias for local cache refresh/stale behavior.
pub type RefreshPolicy = RefreshOptions;