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
use martin_tile_utils::{Encoding, Format, TileCoord};
use crate::cache::{CacheKey, Cacheable, ResourceCache};
use crate::tiles::Tile;
/// Tile cache for storing rendered tile data, keyed by source ID, tile
/// coordinate, query string, `Accept`-driven output format, and the
/// `Accept-Encoding`-negotiated encoding.
pub type TileCache = ResourceCache<TileCacheKey, Tile>;
/// Optional wrapper for [`TileCache`].
pub type OptTileCache = Option<TileCache>;
/// Constant representing no tile cache configuration.
pub const NO_TILE_CACHE: OptTileCache = None;
/// Cache key for one tile entry.
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub enum TileCacheKey {
/// A particular request shapes the bytes that would be served
Dynamic {
/// Source the tile belongs to, or the comma-separated sources of a composite.
source_id: Box<str>,
/// Tile coordinate.
xyz: TileCoord,
/// Request query string, when the source consumes one.
query: Option<Box<str>>,
/// Format requested via the `Accept` header
/// `None` if absent.
format: Option<Format>,
/// Encoding negotiated via the `Accept-Encoding` header.
/// `None` for the tile as the pre-cache pipeline produced it.
encoding: Option<Encoding>,
},
/// A tile exactly as its source produced it, before any post-cache processing.
Static {
/// Source the tile belongs to.
source_id: Box<str>,
/// Tile coordinate.
xyz: TileCoord,
},
}
impl TileCacheKey {
/// Key for the bytes a particular request shape produces.
#[must_use]
pub fn new_request_dynamic(
source_id: impl Into<Box<str>>,
xyz: TileCoord,
query: Option<Box<str>>,
format: Option<Format>,
encoding: Option<Encoding>,
) -> Self {
Self::Dynamic {
source_id: source_id.into(),
xyz,
query,
format,
encoding,
}
}
/// Key for a source's own bytes, independent of any request shape.
#[must_use]
pub fn new_request_static(source_id: impl Into<Box<str>>, xyz: TileCoord) -> Self {
Self::Static {
source_id: source_id.into(),
xyz,
}
}
/// The source this entry belongs to.
#[must_use]
pub fn source_id(&self) -> &str {
match self {
Self::Dynamic { source_id, .. } | Self::Static { source_id, .. } => source_id,
}
}
/// The coordinate this entry is for.
pub const fn xyz(&self) -> TileCoord {
match self {
Self::Dynamic { xyz, .. } | Self::Static { xyz, .. } => *xyz,
}
}
}
impl CacheKey for TileCacheKey {
const CACHE_NAME: &'static str = "tile";
fn matches_source(&self, source_id: &str) -> bool {
self.source_id().split(',').any(|s| s == source_id)
}
fn record_outcome(&self, hit: bool) {
#[cfg(feature = "metrics")]
crate::metrics::TILE_CACHE_REQUESTS_TOTAL
.with_label_values(&[
Self::CACHE_NAME,
crate::cache::hit_miss_label(hit),
crate::metrics::ZOOM_LABELS[self.xyz().z() as usize],
])
.inc();
#[allow(clippy::if_same_then_else)]
if hit {
hotpath::gauge!("tile_cache_hits").inc(1.0);
} else {
hotpath::gauge!("tile_cache_misses").inc(1.0);
}
}
}
impl Cacheable for Tile {
fn weight(&self) -> u32 {
self.data.len().try_into().unwrap_or(u32::MAX)
}
}