hitbox-moka 0.2.0

In-memory cache backend for Hitbox using Moka.
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
//! Builder for configuring [`MokaBackend`].

use std::time::{Duration, Instant};

use chrono::Utc;
use moka::Expiry;
use moka::future::{Cache, CacheBuilder};
use moka::policy::EvictionPolicy;

use crate::backend::MokaBackend;
use hitbox::{BackendLabel, CacheKey, CacheValue, Raw};
use hitbox_backend::format::{Format, JsonFormat};
use hitbox_backend::{CacheKeyFormat, Compressor, PassthroughCompressor};

/// Custom expiration policy that calculates TTL from [`CacheValue::expire`] timestamps.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Expiration;

impl Expiry<CacheKey, CacheValue<Raw>> for Expiration {
    fn expire_after_create(
        &self,
        _key: &CacheKey,
        value: &CacheValue<Raw>,
        _created_at: Instant,
    ) -> Option<Duration> {
        Self::calculate_ttl(value)
    }

    fn expire_after_update(
        &self,
        _key: &CacheKey,
        value: &CacheValue<Raw>,
        _updated_at: Instant,
        _duration_until_expiry: Option<Duration>,
    ) -> Option<Duration> {
        // IMPORTANT: Always use the NEW value's expiration time.
        //
        // Moka's default `expire_after_update` returns `duration_until_expiry`,
        // which preserves the OLD expiration time. This causes premature expiration
        // when updating a cache entry with a new (longer) TTL.
        Self::calculate_ttl(value)
    }
}

impl Expiration {
    fn calculate_ttl(value: &CacheValue<Raw>) -> Option<Duration> {
        value.expire().map(|expiration| {
            let delta = expiration - Utc::now();
            let millis = delta.num_milliseconds();
            if millis <= 0 {
                Duration::ZERO
            } else {
                Duration::from_millis(millis as u64)
            }
        })
    }
}

/// Marker type: capacity has not been configured yet.
///
/// This is the initial state of a [`MokaBackendBuilder`]. You must call either
/// [`max_entries()`](MokaBackendBuilder::max_entries) or
/// [`max_bytes()`](MokaBackendBuilder::max_bytes) before calling `build()`.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoCapacity;

/// Marker type: entry-count capacity has been configured.
///
/// The cache will hold at most `n` entries, evicting least recently used
/// entries when capacity is exceeded.
#[derive(Debug, Clone, Copy)]
pub struct EntryCapacity(pub(crate) u64);

/// Marker type: byte-based capacity has been configured.
///
/// The cache will use at most `n` bytes (approximate), evicting least recently
/// used entries when the memory budget is exceeded.
#[derive(Debug, Clone, Copy)]
pub struct ByteCapacity(pub(crate) u64);

/// Builder for creating and configuring a [`MokaBackend`].
///
/// Use [`MokaBackend::builder`] to create a new builder instance.
///
/// # Capacity Configuration (Required)
///
/// You must configure capacity using exactly one of:
/// - [`max_entries(n)`](Self::max_entries) - limit by entry count
/// - [`max_bytes(n)`](Self::max_bytes) - limit by approximate memory usage
///
/// These methods use the typestate pattern to enforce compile-time guarantees:
/// - `build()` is only available after setting capacity
/// - You cannot set both entry and byte limits
///
/// # Examples
///
/// Entry-based capacity:
///
/// ```
/// use hitbox_moka::MokaBackend;
///
/// let backend = MokaBackend::builder()
///     .max_entries(10_000)
///     .build();
/// ```
///
/// Byte-based capacity (100 MB):
///
/// ```
/// use hitbox_moka::MokaBackend;
///
/// let backend = MokaBackend::builder()
///     .max_bytes(100 * 1024 * 1024)
///     .build();
/// ```
///
/// With custom configuration:
///
/// ```ignore
/// use hitbox_moka::MokaBackend;
/// use hitbox_backend::format::BincodeFormat;
/// use hitbox_backend::{CacheKeyFormat, GzipCompressor};
///
/// let backend = MokaBackend::builder()
///     .label("sessions")
///     .max_bytes(50_000_000)
///     .key_format(CacheKeyFormat::UrlEncoded)
///     .value_format(BincodeFormat)
///     .compressor(GzipCompressor::default())
///     .build();
/// ```
///
/// **Note:** `GzipCompressor` and `ZstdCompressor` require enabling the `gzip`
/// or `zstd` feature on `hitbox-backend`.
pub struct MokaBackendBuilder<Cap, S = JsonFormat, C = PassthroughCompressor>
where
    S: Format,
    C: Compressor,
{
    capacity: Cap,
    key_format: CacheKeyFormat,
    serializer: S,
    compressor: C,
    label: BackendLabel,
    eviction_policy: Option<EvictionPolicy>,
}

impl MokaBackendBuilder<NoCapacity, JsonFormat, PassthroughCompressor> {
    /// Creates a new builder with no capacity configured.
    ///
    /// You must call [`max_entries()`](Self::max_entries) or
    /// [`max_bytes()`](Self::max_bytes) before calling `build()`.
    pub fn new() -> Self {
        Self {
            capacity: NoCapacity,
            key_format: CacheKeyFormat::Bitcode,
            serializer: JsonFormat,
            compressor: PassthroughCompressor,
            label: BackendLabel::new_static("moka"),
            eviction_policy: None,
        }
    }
}

impl Default for MokaBackendBuilder<NoCapacity, JsonFormat, PassthroughCompressor> {
    fn default() -> Self {
        Self::new()
    }
}

impl<S, C> MokaBackendBuilder<NoCapacity, S, C>
where
    S: Format,
    C: Compressor,
{
    /// Sets the maximum number of entries the cache can hold.
    ///
    /// When the cache exceeds this capacity, least recently used entries are
    /// evicted.
    ///
    /// # Example
    ///
    /// ```
    /// use hitbox_moka::MokaBackend;
    ///
    /// let backend = MokaBackend::builder()
    ///     .max_entries(10_000)
    ///     .build();
    /// ```
    pub fn max_entries(self, capacity: u64) -> MokaBackendBuilder<EntryCapacity, S, C> {
        MokaBackendBuilder {
            capacity: EntryCapacity(capacity),
            key_format: self.key_format,
            serializer: self.serializer,
            compressor: self.compressor,
            label: self.label,
            eviction_policy: self.eviction_policy,
        }
    }

    /// Sets the maximum memory budget in bytes.
    ///
    /// The cache will use approximately this many bytes for stored values,
    /// evicting least recently used entries when the budget is exceeded.
    ///
    /// The byte count includes:
    /// - Serialized value data
    /// - Fixed overhead estimate for keys and metadata (~112 bytes per entry)
    ///
    /// # Example
    ///
    /// ```
    /// use hitbox_moka::MokaBackend;
    ///
    /// // 100 MB cache
    /// let backend = MokaBackend::builder()
    ///     .max_bytes(100 * 1024 * 1024)
    ///     .build();
    /// ```
    pub fn max_bytes(self, bytes: u64) -> MokaBackendBuilder<ByteCapacity, S, C> {
        MokaBackendBuilder {
            capacity: ByteCapacity(bytes),
            key_format: self.key_format,
            serializer: self.serializer,
            compressor: self.compressor,
            label: self.label,
            eviction_policy: self.eviction_policy,
        }
    }
}

impl<Cap, S, C> MokaBackendBuilder<Cap, S, C>
where
    S: Format,
    C: Compressor,
{
    /// Sets a custom label for this backend.
    ///
    /// The label identifies this backend in multi-tier cache compositions and
    /// appears in metrics and debug output.
    ///
    /// # Default
    ///
    /// `"moka"`
    pub fn label(mut self, label: impl Into<BackendLabel>) -> Self {
        self.label = label.into();
        self
    }

    /// Sets the cache key serialization format.
    ///
    /// The key format determines how [`CacheKey`] values are serialized for
    /// storage. This affects key size and debuggability.
    ///
    /// # Default
    ///
    /// [`CacheKeyFormat::Bitcode`]
    ///
    /// # Options
    ///
    /// | Format | Size | Human-readable |
    /// |--------|------|----------------|
    /// | [`Bitcode`](CacheKeyFormat::Bitcode) | Compact | No |
    /// | [`UrlEncoded`](CacheKeyFormat::UrlEncoded) | Larger | Yes |
    ///
    /// [`CacheKey`]: hitbox_core::CacheKey
    pub fn key_format(mut self, format: CacheKeyFormat) -> Self {
        self.key_format = format;
        self
    }

    /// Sets the eviction policy for the cache.
    ///
    /// The eviction policy determines how entries are selected for removal when
    /// the cache reaches capacity.
    ///
    /// # Default
    ///
    /// - **Entry-based capacity** ([`max_entries`]): [`EvictionPolicy::tiny_lfu()`] -
    ///   combines LRU eviction with LFU admission for optimal hit rates
    /// - **Byte-based capacity** ([`max_bytes`]): [`EvictionPolicy::lru()`] -
    ///   pure LRU for predictable eviction behavior with weighted entries
    ///
    /// # Options
    ///
    /// | Policy | Description | Best for |
    /// |--------|-------------|----------|
    /// | [`tiny_lfu()`](EvictionPolicy::tiny_lfu) | LRU eviction + LFU admission | General caching, web workloads |
    /// | [`lru()`](EvictionPolicy::lru) | Pure least-recently-used | Recency-biased, streaming data |
    ///
    /// # Example
    ///
    /// ```
    /// use hitbox_moka::{MokaBackend, EvictionPolicy};
    ///
    /// // Use TinyLFU with byte-based capacity (overriding default LRU)
    /// let backend = MokaBackend::builder()
    ///     .max_bytes(100 * 1024 * 1024)
    ///     .eviction_policy(EvictionPolicy::tiny_lfu())
    ///     .build();
    /// ```
    ///
    /// [`max_entries`]: Self::max_entries
    /// [`max_bytes`]: Self::max_bytes
    pub fn eviction_policy(mut self, policy: EvictionPolicy) -> Self {
        self.eviction_policy = Some(policy);
        self
    }

    /// Sets the cache value serialization format.
    ///
    /// The value format determines how cached data is serialized before storage.
    ///
    /// # Default
    ///
    /// [`JsonFormat`]
    ///
    /// # Options
    ///
    /// | Format | Speed | Size | Human-readable |
    /// |--------|-------|------|----------------|
    /// | [`JsonFormat`] | Slow | Large | Yes |
    /// | [`BincodeFormat`](hitbox_backend::format::BincodeFormat) | Fast | Compact | No |
    /// | [`RonFormat`](hitbox_backend::format::RonFormat) | Medium | Medium | Yes |
    pub fn value_format<NewS>(self, serializer: NewS) -> MokaBackendBuilder<Cap, NewS, C>
    where
        NewS: Format,
    {
        MokaBackendBuilder {
            capacity: self.capacity,
            key_format: self.key_format,
            serializer,
            compressor: self.compressor,
            label: self.label,
            eviction_policy: self.eviction_policy,
        }
    }

    /// Sets the compression strategy for cache values.
    ///
    /// Compression reduces memory usage at the cost of CPU time. For in-memory
    /// caches like Moka, compression is typically **not recommended** since
    /// memory access is fast and compression adds latency.
    ///
    /// # Default
    ///
    /// [`PassthroughCompressor`] (no compression)
    ///
    /// # Options
    ///
    /// | Compressor | Ratio | Speed | Feature flag |
    /// |------------|-------|-------|--------------|
    /// | [`PassthroughCompressor`] | None | Fastest | — |
    /// | [`GzipCompressor`] | Good | Medium | `gzip` |
    /// | [`ZstdCompressor`] | Best | Fast | `zstd` |
    ///
    /// # When to Use Compression
    ///
    /// - Large cached values (>10KB)
    /// - Memory-constrained environments
    /// - When composing with network backends (compression done once, reused)
    ///
    /// [`PassthroughCompressor`]: hitbox_backend::PassthroughCompressor
    /// [`GzipCompressor`]: https://docs.rs/hitbox-backend/latest/hitbox_backend/struct.GzipCompressor.html
    /// [`ZstdCompressor`]: https://docs.rs/hitbox-backend/latest/hitbox_backend/struct.ZstdCompressor.html
    pub fn compressor<NewC>(self, compressor: NewC) -> MokaBackendBuilder<Cap, S, NewC>
    where
        NewC: Compressor,
    {
        MokaBackendBuilder {
            capacity: self.capacity,
            key_format: self.key_format,
            serializer: self.serializer,
            compressor,
            label: self.label,
            eviction_policy: self.eviction_policy,
        }
    }
}

impl<S, C> MokaBackendBuilder<EntryCapacity, S, C>
where
    S: Format,
    C: Compressor,
{
    /// Builds the [`MokaBackend`] with entry-count based capacity.
    ///
    /// Consumes the builder and returns a fully configured backend ready for use.
    pub fn build(self) -> MokaBackend<S, C> {
        let policy = self
            .eviction_policy
            .unwrap_or_else(EvictionPolicy::tiny_lfu);
        let cache: Cache<CacheKey, CacheValue<Raw>> = CacheBuilder::new(self.capacity.0)
            .eviction_policy(policy)
            .expire_after(Expiration)
            .build();

        MokaBackend {
            cache,
            key_format: self.key_format,
            serializer: self.serializer,
            compressor: self.compressor,
            label: self.label,
        }
    }
}

impl<S, C> MokaBackendBuilder<ByteCapacity, S, C>
where
    S: Format + 'static,
    C: Compressor + 'static,
{
    /// Builds the [`MokaBackend`] with byte-based capacity.
    ///
    /// Consumes the builder and returns a fully configured backend ready for use.
    /// The cache will use a weigher function to track approximate memory usage.
    ///
    /// Note: Default eviction policy is LRU (not TinyLFU) to ensure predictable
    /// eviction behavior with weighted capacity. TinyLFU's admission policy can
    /// reject new entries even when eviction could make room. Override with
    /// [`eviction_policy()`](MokaBackendBuilder::eviction_policy) if needed.
    pub fn build(self) -> MokaBackend<S, C> {
        let policy = self.eviction_policy.unwrap_or_else(EvictionPolicy::lru);
        let cache: Cache<CacheKey, CacheValue<Raw>> = CacheBuilder::new(self.capacity.0)
            .weigher(Self::byte_weigher)
            .eviction_policy(policy)
            .expire_after(Expiration)
            .build();

        MokaBackend {
            cache,
            key_format: self.key_format,
            serializer: self.serializer,
            compressor: self.compressor,
            label: self.label,
        }
    }

    /// Weigher function that calculates the approximate byte cost of a cache entry.
    ///
    /// Uses the precalculated `memory_size()` methods on `CacheKey` and `CacheValue`
    /// which account for struct overhead and variable-length content.
    fn byte_weigher(key: &CacheKey, value: &CacheValue<Raw>) -> u32 {
        (key.memory_size() + value.memory_size()).min(u32::MAX as usize) as u32
    }
}