From acb52f2ce0ff0db7a131dc99bfbb29dee95cde6c Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 23 Dec 2023 23:06:37 -0500 Subject: [PATCH] Limit PMTiles cache 4MB per file (#1095) More precursor work for https://github.com/maplibre/martin/issues/1093 --- martin/src/pmtiles/http_pmtiles.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/martin/src/pmtiles/http_pmtiles.rs b/martin/src/pmtiles/http_pmtiles.rs index 922c28185..678042780 100644 --- a/martin/src/pmtiles/http_pmtiles.rs +++ b/martin/src/pmtiles/http_pmtiles.rs @@ -23,8 +23,15 @@ use crate::{MartinResult, TileCoord, TileData}; struct PmtCache(Cache); impl PmtCache { - fn new() -> Self { - Self(Cache::new(500)) + fn new(max_capacity: u64) -> Self { + Self( + Cache::builder() + .weigher(|_key, value: &Directory| -> u32 { + value.get_approx_byte_size().try_into().unwrap_or(u32::MAX) + }) + .max_capacity(max_capacity) + .build(), + ) } } @@ -54,7 +61,7 @@ impl_pmtiles_source!( impl PmtHttpSource { pub async fn new_url_box(id: String, url: Url) -> FileResult> { let client = Client::new(); - let cache = PmtCache::new(); + let cache = PmtCache::new(4 * 1024 * 1024); Ok(Box::new( PmtHttpSource::new_url(client, cache, id, url).await?, ))