martin_core/resources/fonts/
cache.rs1use bytes::Bytes;
2use martin_tile_utils::Encoding;
3
4use crate::cache::{CacheKey, ResourceCache};
5
6pub type FontCache = ResourceCache<FontCacheKey, Bytes>;
8
9pub type OptFontCache = Option<FontCache>;
11
12pub const NO_FONT_CACHE: OptFontCache = None;
14
15#[derive(Debug, Hash, PartialEq, Eq, Clone)]
23pub struct FontCacheKey {
24 ids: String,
25 start: u32,
26 end: u32,
27 encoding: Option<Encoding>,
29}
30
31impl FontCacheKey {
32 #[must_use]
34 pub const fn new(ids: String, start: u32, end: u32) -> Self {
35 Self {
36 ids,
37 start,
38 end,
39 encoding: None,
40 }
41 }
42
43 #[must_use]
45 pub fn with_encoding(self, encoding: Encoding) -> Self {
46 Self {
47 encoding: Some(encoding),
48 ..self
49 }
50 }
51}
52
53impl CacheKey for FontCacheKey {
54 const CACHE_NAME: &'static str = "font";
55
56 fn matches_source(&self, source_id: &str) -> bool {
57 self.ids.split(',').any(|s| s == source_id)
58 }
59
60 fn record_outcome(&self, hit: bool) {
61 #[cfg(feature = "metrics")]
62 crate::metrics::CACHE_REQUESTS_TOTAL
63 .with_label_values(&[Self::CACHE_NAME, crate::cache::hit_miss_label(hit)])
64 .inc();
65 #[allow(clippy::if_same_then_else)]
66 if hit {
67 hotpath::gauge!("font_cache_hits").inc(1.0);
68 } else {
69 hotpath::gauge!("font_cache_misses").inc(1.0);
70 }
71 }
72}