Skip to main content

DiskCache

Struct DiskCache 

Source
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

Source

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.

Source

pub fn get(&self, id: &TileId) -> Result<Option<TileData>, DiskCacheError>

Load a tile from cache.

Returns Ok(None) if the tile is not cached.

Source

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.

Source

pub fn contains(&self, id: &TileId) -> bool

Check whether a tile is present in the cache.

Source

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.

Source

pub fn clear(&self) -> Result<(), DiskCacheError>

Delete all cached tiles.

Removes the entire directory tree under base_dir and recreates the root.

Source

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.

Source

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.

Source

pub fn len(&self) -> Result<usize, DiskCacheError>

Count the number of cached tiles (walks the directory tree).

Source

pub fn is_empty(&self) -> Result<bool, DiskCacheError>

Whether the cache directory is empty (no .bin files).

Source

pub fn base_dir(&self) -> &Path

The root directory of this cache.

Trait Implementations§

Source§

impl Debug for DiskCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.