claviron-cache 0.2.7

Named cache stores, typed values, and fleet-shared caching for Claviron
Documentation
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::any::Any;
use std::borrow::Cow;
use std::sync::{Arc, OnceLock, PoisonError, RwLock};
use std::time::{Duration, Instant};

use arc_swap::ArcSwap;
use async_trait::async_trait;
use bytes::Bytes;
use moka::Expiry;
use serde::{Deserialize, Deserializer};

use crate::config::CacheStoreConfig;
use crate::memory::MemoryCache;
use crate::{
    CacheDriver, CacheEntry, CacheExpiry, CacheStore, CacheStoreResolver,
    Error, Result,
};

pub(crate) const NAME: &str = "memory";
const DEFAULT_CAPACITY: u64 = 10_000;

pub(crate) struct MemoryCacheDriver;

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct MemoryCacheStoreConfig {
    #[serde(default)]
    capacity: Option<u64>,
    #[serde(default, deserialize_with = "deserialize_byte_size")]
    max_bytes: Option<u64>,
    #[serde(default)]
    l2: Option<Box<str>>,
}

/// What bounds a memory store's residency.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MemoryLimit {
    /// Resident entry count.
    Entries(u64),
    /// Resident key and value bytes; per-entry bookkeeping is not counted.
    Bytes(u64),
}

impl MemoryLimit {
    pub(crate) const DEFAULT: Self = Self::Entries(DEFAULT_CAPACITY);
}

impl MemoryCacheStoreConfig {
    fn limit(
        &self,
        store_name: &str,
    ) -> Result<MemoryLimit> {
        match (self.capacity, self.max_bytes) {
            (Some(_), Some(_)) => Err(Error::Rejected(format!(
                "memory cache store '{store_name}' sets both capacity and max_bytes; choose one bound"
            ))),
            (Some(entries), None) => Ok(MemoryLimit::Entries(entries)),
            (None, Some(bytes)) => Ok(MemoryLimit::Bytes(bytes)),
            (None, None) => Ok(MemoryLimit::DEFAULT),
        }
    }
}

/// Process-local byte store over Moka.
///
/// The store outlives catalog generations: reload keeps it, and a changed
/// bound moves the resident entries into a cache built with the new bound.
pub(crate) struct MemoryCacheStore {
    name: Box<str>,
    generation: ArcSwap<Generation>,
    /// Mutations hold this shared; a bound change holds it exclusively while
    /// it moves the entries, so no write lands in the cache being replaced.
    mutations: RwLock<()>,
    l2_name: Option<Box<str>>,
    backing: OnceLock<Backing>,
}

struct Backing {
    store: Arc<dyn CacheStore>,
    operations: tokio::sync::RwLock<()>,
}

struct Generation {
    limit: MemoryLimit,
    cache: MemoryCache<String, Entry>,
}

impl Generation {
    fn new(limit: MemoryLimit) -> Self {
        let builder = MemoryCache::builder().expire_after(EntryExpiry);
        let cache = match limit {
            MemoryLimit::Entries(entries) => {
                builder.max_capacity(entries).build()
            }
            MemoryLimit::Bytes(bytes) => builder
                .max_capacity(bytes)
                .weigher(|key: &String, entry: &Entry| {
                    u32::try_from(key.len() + entry.bytes.len())
                        .unwrap_or(u32::MAX)
                })
                .build(),
        };
        Self { limit, cache }
    }
}

impl MemoryCacheStore {
    pub(crate) fn new(
        name: &str,
        limit: MemoryLimit,
    ) -> Self {
        Self {
            name: name.into(),
            generation: ArcSwap::from_pointee(Generation::new(limit)),
            mutations: RwLock::new(()),
            l2_name: None,
            backing: OnceLock::new(),
        }
    }

    pub(crate) fn reload_limit(
        &self,
        config: &CacheStoreConfig,
    ) -> Result<MemoryLimit> {
        let options: MemoryCacheStoreConfig = config.deserialize_options()?;
        if self.l2_name != options.l2 {
            return Err(Error::Rejected(format!(
                "memory cache store '{}' cannot change its L2 during reload; restart is required",
                self.name
            )));
        }
        options.limit(&self.name)
    }

    pub(crate) fn l2_name(&self) -> Option<&str> {
        self.l2_name.as_deref()
    }

    fn local_entry(
        &self,
        key: &str,
    ) -> Option<CacheEntry> {
        self.generation.load().cache.get(key).map(Entry::into_cache_entry)
    }

    /// Apply a new residency bound without dropping what the store holds.
    ///
    /// Moka fixes the bound when a cache is built, so a changed bound builds
    /// a new cache and moves every live entry into it with its remaining
    /// TTL. A lower bound then evicts the excess under the usual policy.
    /// Bound changes are serialized by the caller; the provider's reload lock
    /// does that.
    pub(crate) fn set_limit(
        &self,
        limit: MemoryLimit,
    ) {
        // An unchanged bound, the usual reload, does not hold writes off.
        if self.generation.load().limit == limit {
            return;
        }
        let _mutations =
            self.mutations.write().unwrap_or_else(PoisonError::into_inner);
        let current = self.generation.load_full();
        let next = Generation::new(limit);
        for (key, entry) in current.cache.iter() {
            next.cache.insert(String::clone(&key), entry);
        }
        self.generation.store(Arc::new(next));
    }

    fn mutate<T>(
        &self,
        mutation: impl FnOnce(&MemoryCache<String, Entry>) -> T,
    ) -> T {
        let _mutations =
            self.mutations.read().unwrap_or_else(PoisonError::into_inner);
        mutation(&self.generation.load().cache)
    }
}

#[derive(Clone)]
struct Entry {
    bytes: Bytes,
    lifetime: Option<(Instant, Duration)>,
}

impl Entry {
    fn remaining(
        &self,
        at: Instant,
    ) -> Option<Duration> {
        self.lifetime.map(|(started, ttl)| {
            ttl.saturating_sub(at.saturating_duration_since(started))
        })
    }

    fn into_cache_entry(self) -> CacheEntry {
        let expiry =
            self.lifetime.map_or(CacheExpiry::Never, |(started, ttl)| {
                started
                    .checked_add(ttl)
                    .map_or(CacheExpiry::Unknown, CacheExpiry::At)
            });
        CacheEntry { bytes: self.bytes, expiry }
    }
}

struct EntryExpiry;

impl Expiry<String, Entry> for EntryExpiry {
    fn expire_after_create(
        &self,
        _key: &String,
        value: &Entry,
        created_at: Instant,
    ) -> Option<Duration> {
        value.remaining(created_at)
    }

    fn expire_after_update(
        &self,
        _key: &String,
        value: &Entry,
        updated_at: Instant,
        _remaining: Option<Duration>,
    ) -> Option<Duration> {
        value.remaining(updated_at)
    }
}

impl CacheDriver for MemoryCacheDriver {
    fn driver_name(&self) -> &'static str {
        NAME
    }

    fn create_store(
        self: Arc<Self>,
        store_name: &str,
        config: &CacheStoreConfig,
    ) -> Result<Arc<dyn CacheStore>> {
        let options: MemoryCacheStoreConfig = config.deserialize_options()?;
        let mut store =
            MemoryCacheStore::new(store_name, options.limit(store_name)?);
        store.l2_name = options.l2;
        Ok(Arc::new(store))
    }

    fn finish_store(
        &self,
        store: &Arc<dyn CacheStore>,
        stores: &dyn CacheStoreResolver,
    ) -> Result<()> {
        let memory = store
            .as_any()
            .downcast_ref::<MemoryCacheStore>()
            .expect("memory driver creates a memory store");
        let Some(name) = memory.l2_name() else {
            return Ok(());
        };
        let backing = stores.store(name)?;
        if Arc::ptr_eq(store, &backing)
            || backing.driver_name() == "orbit"
            || backing
                .as_any()
                .downcast_ref::<MemoryCacheStore>()
                .is_some_and(|store| store.l2_name().is_some())
        {
            return Err(Error::Rejected(format!(
                "memory cache store '{}' requires a single-tier L2, got '{name}'",
                memory.name
            )));
        }
        memory
            .backing
            .set(Backing {
                store: backing,
                operations: tokio::sync::RwLock::new(()),
            })
            .map_err(|_| {
                Error::Rejected(format!(
                    "memory cache store '{}' already has an L2",
                    memory.name
                ))
            })?;
        Ok(())
    }
}

#[async_trait]
impl CacheStore for MemoryCacheStore {
    fn store_name(&self) -> &str {
        &self.name
    }

    fn driver_name(&self) -> &str {
        NAME
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn supports_ttl(&self) -> bool {
        self.backing.get().is_none_or(|backing| backing.store.supports_ttl())
    }

    async fn get(
        &self,
        key: &str,
    ) -> Result<Option<Bytes>> {
        if let Some(entry) = self.generation.load().cache.get(key) {
            return Ok(Some(entry.bytes));
        }
        Ok(self.read_backing(key).await?.map(|entry| entry.bytes))
    }

    async fn get_entry(
        &self,
        key: &str,
    ) -> Result<Option<CacheEntry>> {
        if let Some(entry) = self.local_entry(key) {
            return Ok(Some(entry));
        }
        self.read_backing(key).await
    }

    async fn put(
        &self,
        key: &str,
        value: Cow<'_, [u8]>,
        ttl: Option<Duration>,
    ) -> Result<()> {
        if ttl == Some(Duration::ZERO) {
            return self.forget(key).await;
        }
        let backing = self.backing.get();
        let _operation = if let Some(backing) = backing {
            if ttl.is_some() && !backing.store.supports_ttl() {
                return Err(Error::Rejected(format!(
                    "backing store '{}' does not preserve TTL for key '{key}'",
                    backing.store.store_name()
                )));
            }
            Some(backing.operations.write().await)
        } else {
            None
        };
        let lifetime = ttl.map(|ttl| (Instant::now(), ttl));
        if let Some(backing) = backing {
            self.mutate(|cache| cache.invalidate(key));
            backing.store.put(key, Cow::Borrowed(value.as_ref()), ttl).await?;
        }
        let entry = Entry { bytes: Bytes::from(value.into_owned()), lifetime };
        self.mutate(|cache| cache.insert(key.to_owned(), entry));
        Ok(())
    }

    async fn forget(
        &self,
        key: &str,
    ) -> Result<()> {
        if let Some(backing) = self.backing.get() {
            let _operation = backing.operations.write().await;
            self.mutate(|cache| cache.invalidate(key));
            return backing.store.forget(key).await;
        }
        self.mutate(|cache| cache.invalidate(key));
        Ok(())
    }

    async fn clear(&self) -> Result<()> {
        if let Some(backing) = self.backing.get() {
            let _operation = backing.operations.write().await;
            self.mutate(MemoryCache::invalidate_all);
            return backing.store.clear().await;
        }
        self.mutate(MemoryCache::invalidate_all);
        Ok(())
    }
}

impl MemoryCacheStore {
    async fn read_backing(
        &self,
        key: &str,
    ) -> Result<Option<CacheEntry>> {
        let Some(backing) = self.backing.get() else {
            return Ok(None);
        };
        let _operation = backing.operations.read().await;
        if let Some(entry) = self.local_entry(key) {
            return Ok(Some(entry));
        }
        let Some(entry) = backing.store.get_entry(key).await? else {
            return Ok(None);
        };
        let lifetime = match entry.expiry {
            CacheExpiry::At(deadline) => {
                let now = Instant::now();
                let ttl = deadline.saturating_duration_since(now);
                if ttl.is_zero() {
                    return Ok(None);
                }
                Some((now, ttl))
            }
            CacheExpiry::Unknown => return Ok(Some(entry)),
            CacheExpiry::Never => None,
        };
        let cached = self.mutate(|cache| {
            // Another miss can fill the entry while this read is in flight.
            cache.get_with(key.to_owned(), || Entry {
                bytes: entry.bytes,
                lifetime,
            })
        });
        Ok(Some(cached.into_cache_entry()))
    }
}

/// A byte count or a size such as `64 MiB`.
fn deserialize_byte_size<'de, D>(
    deserializer: D
) -> std::result::Result<Option<u64>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum Size {
        Bytes(u64),
        Human(
            #[serde(
                deserialize_with = "claviron_helpers::de::deserialize_byte_size"
            )]
            u64,
        ),
    }

    Ok(Option::<Size>::deserialize(deserializer)?.map(|size| match size {
        Size::Bytes(bytes) | Size::Human(bytes) => bytes,
    }))
}

#[cfg(test)]
#[path = "../../tests/drivers/memory.rs"]
mod tests;