pub struct DiskCache { /* private fields */ }Expand description
A flat-file on-disk tile cache.
Directory layout: {base_dir}/{z}/{x}/{y}.bin.
§Example
use rustial_engine::DiskCache;
use rustial_math::TileId;
let cache = DiskCache::new("./tile_cache").unwrap();
let tile = TileId::new(10, 512, 340);
if let Some(data) = cache.get(&tile).unwrap() {
// use cached tile
}Implementations§
Source§impl DiskCache
impl DiskCache
Sourcepub fn new(base_dir: impl Into<PathBuf>) -> Result<Self, DiskCacheError>
pub fn new(base_dir: impl Into<PathBuf>) -> Result<Self, DiskCacheError>
Create (or open) a disk cache rooted at base_dir.
The directory tree is created on demand; this call only ensures the root directory exists.
Sourcepub fn get(&self, id: &TileId) -> Result<Option<TileData>, DiskCacheError>
pub fn get(&self, id: &TileId) -> Result<Option<TileData>, DiskCacheError>
Load a tile from cache.
Returns Ok(None) if the tile is not cached.
Sourcepub fn put(&self, id: &TileId, data: &TileData) -> Result<(), DiskCacheError>
pub fn put(&self, id: &TileId, data: &TileData) -> Result<(), DiskCacheError>
Store a tile in the cache.
Writes to a temporary file first, then renames atomically to prevent readers from seeing a partial write.
Sourcepub fn remove(&self, id: &TileId) -> Result<bool, DiskCacheError>
pub fn remove(&self, id: &TileId) -> Result<bool, DiskCacheError>
Remove a single tile from the cache.
Returns true if the file was deleted, false if it was not cached.
Sourcepub fn clear(&self) -> Result<(), DiskCacheError>
pub fn clear(&self) -> Result<(), DiskCacheError>
Delete all cached tiles.
Removes the entire directory tree under base_dir
and recreates the root.
Sourcepub fn evict_older_than(
&self,
max_age: Duration,
) -> Result<usize, DiskCacheError>
pub fn evict_older_than( &self, max_age: Duration, ) -> Result<usize, DiskCacheError>
Remove cached tiles whose last-modified time is older than max_age.
Walks the directory tree, deletes matching .bin files, and
removes any empty parent directories left behind.
Returns the number of files deleted.
Sourcepub fn evict_to_size(&self, max_bytes: u64) -> Result<usize, DiskCacheError>
pub fn evict_to_size(&self, max_bytes: u64) -> Result<usize, DiskCacheError>
Remove cached tiles until the total on-disk size is at or below
max_bytes.
Files are sorted oldest-first (by last-modified time) and deleted in that order until the total size drops below the limit.
Returns the number of files deleted.
Sourcepub fn len(&self) -> Result<usize, DiskCacheError>
pub fn len(&self) -> Result<usize, DiskCacheError>
Count the number of cached tiles (walks the directory tree).
Sourcepub fn is_empty(&self) -> Result<bool, DiskCacheError>
pub fn is_empty(&self) -> Result<bool, DiskCacheError>
Whether the cache directory is empty (no .bin files).