mpl_registry_api/
cache.rs1use moka::sync::Cache;
4use serde_json::Value;
5use std::time::Duration;
6
7pub struct SchemaCache {
9 schemas: Cache<String, Value>,
10}
11
12impl SchemaCache {
13 pub fn new() -> Self {
15 Self {
16 schemas: Cache::builder()
17 .max_capacity(1000)
18 .time_to_live(Duration::from_secs(300)) .time_to_idle(Duration::from_secs(60)) .build(),
21 }
22 }
23
24 pub fn get(&self, stype: &str) -> Option<Value> {
26 self.schemas.get(stype)
27 }
28
29 pub fn insert(&self, stype: String, schema: Value) {
31 self.schemas.insert(stype, schema);
32 }
33
34 pub fn contains(&self, stype: &str) -> bool {
36 self.schemas.contains_key(stype)
37 }
38
39 pub fn invalidate(&self, stype: &str) {
41 self.schemas.invalidate(stype);
42 }
43
44 pub fn clear(&self) {
46 self.schemas.invalidate_all();
47 }
48
49 pub fn stats(&self) -> CacheStats {
51 CacheStats {
52 entry_count: self.schemas.entry_count(),
53 weighted_size: self.schemas.weighted_size(),
54 }
55 }
56}
57
58impl Default for SchemaCache {
59 fn default() -> Self {
60 Self::new()
61 }
62}
63
64#[derive(Debug, Clone, serde::Serialize)]
65pub struct CacheStats {
66 pub entry_count: u64,
67 pub weighted_size: u64,
68}