Skip to content

FileRasterTileCache

Namespace: ThinkGeo.Core

A disk-based raster tile cache.

public class FileRasterTileCache : RasterTileCache

Inheritance ObjectTileCacheRasterTileCacheFileRasterTileCache

Remarks:

Tiles are stored as individual image files under FileRasterTileCache.CacheDirectory using the folder layout: {CacheDirectory}\{CacheId}\{zoom}\{x}\{y}.{extension}.

This implementation is designed for concurrent access within a process. It uses StripedAsyncLocker to serialize writes and deletes for the same target file path while keeping reads lock-free. Writes are performed using an atomic publish pattern (write to a temporary file, then move/rename).

Properties

CacheDirectory

Gets or sets the root directory where cached tiles are stored.

public string CacheDirectory { get; set; }

Property Value

String

Remarks:

Tiles are stored beneath this directory in a folder hierarchy based on TileCache.CacheId, zoom level, and tile coordinates.

NoDataTileImage

This property returns back a preset image showing the tileView data is missing.

public GeoImage NoDataTileImage { get; }

Property Value

GeoImage

LoadingTileImage

This property returns back a preset image showing the Tile is loading.

public GeoImage LoadingTileImage { get; }

Property Value

GeoImage

ImageFormat

Gets or sets the tileView image format.

public GeoImageFormat ImageFormat { get; set; }

Property Value

GeoImageFormat

JpegQuality

Gets or sets the Jpeg quality , this property only take effects when setting the ImageFormat to Jpeg.

public short JpegQuality { get; set; }

Property Value

Int16

TileAccessMode

Gets or sets the Mode for the TileCache access the tiles. The Default value is ReadAddDelete

public TileAccessMode TileAccessMode { get; set; }

Property Value

TileAccessMode

Remarks:

If you want it to take effect, you need set the Read property false.

CacheId

Gets or sets the id of the TileCache.

public string CacheId { get; set; }

Property Value

String

ExpirationTime

Gets or sets the ExpirationTime.

public TimeSpan ExpirationTime { get; set; }

Property Value

TimeSpan

Constructors

FileRasterTileCache()

Initializes a new instance of the FileRasterTileCache class using the system temporary directory.

public FileRasterTileCache()

Remarks:

When this constructor is used, you may still need to set properties such as FileRasterTileCache.CacheDirectory to match your application's desired cache location.

FileRasterTileCache(String)

Initializes a new instance of the FileRasterTileCache class using the specified cache directory.

public FileRasterTileCache(string cacheDirectory)

Parameters

cacheDirectory String

        The root directory where cached tiles are stored. Subfolders will be created as needed.

Remarks:

A new unique TileCache.CacheId is generated automatically.

FileRasterTileCache(String, String)

Initializes a new instance of the FileRasterTileCache class using the specified cache directory and cache id.

public FileRasterTileCache(string cacheDirectory, string cacheId)

Parameters

cacheDirectory String

        The root directory where cached tiles are stored. Subfolders will be created as needed.

cacheId String

        A logical identifier used as a subfolder name under  to separate caches.

Remarks:

The default image format is PNG.

FileRasterTileCache(String, String, GeoImageFormat)

Initializes a new instance of the FileRasterTileCache class using the specified cache directory, cache id, and image format.

public FileRasterTileCache(string cacheDirectory, string cacheId, GeoImageFormat imageFormat)

Parameters

cacheDirectory String

        The root directory where cached tiles are stored. Subfolders will be created as needed.

cacheId String

        A logical identifier used as a subfolder name under  to separate caches.

imageFormat GeoImageFormat

        The image format used to persist tiles on disk (for example, PNG or JPG).

Methods

GetTileCore(Int32, Int64, Int64)

Gets a tile from the cache for the specified zoom level and tile coordinates.

protected Tile GetTileCore(int zoom, long x, long y)

Parameters

zoom Int32
The zoom level of the requested tile.

x Int64
The X tile coordinate (column index).

y Int64
The Y tile coordinate (row index).

Returns

Tile

        A  instance. If the tile is not available (or is expired), the returned tile will have
        no content.

Remarks:

Reads are performed without an in-process lock. The cache uses an atomic publish strategy when writing tiles (temporary file + move), which prevents readers from observing partially written files.

If the cached file is expired based on TileCache.ExpirationTime, this method attempts to delete it and returns an empty tile.

GetTileAsyncCore(Int32, Int64, Int64, CancellationToken)

Asynchronously gets a tile from the cache for the specified zoom level and tile coordinates.

protected Task<Tile> GetTileAsyncCore(int zoom, long x, long y, CancellationToken cancellationToken)

Parameters

zoom Int32
The zoom level of the requested tile.

x Int64
The X tile coordinate (column index).

y Int64
The Y tile coordinate (row index).

cancellationToken CancellationToken

        A token that can be used to cancel the operation.

Returns

Task<Tile>

        A task that produces a  instance. If the tile is not available (or is expired), the returned tile
        will have no content.

Remarks:

Reads are performed without an in-process lock. Writers publish completed files atomically (temporary file + move), so readers will observe either the previous version or the new version, but not a partially written file.

If the cached file is expired based on TileCache.ExpirationTime, this method attempts to delete it and returns an empty tile.

SaveTileCore(Tile)

Saves the specified tile to the cache.

protected void SaveTileCore(Tile tile)

Parameters

tile Tile
The tile to save.

Remarks:

If a cached tile already exists at the target path, this method does nothing.

This method uses StripedAsyncLocker to serialize writers for the same cache path. The tile is written to a temporary file first and then moved into place to ensure readers never observe a partially written file.

SaveTileAsyncCore(Tile, CancellationToken)

Asynchronously saves the specified tile to the cache.

protected Task SaveTileAsyncCore(Tile tile, CancellationToken cancellationToken)

Parameters

tile Tile
The tile to save.

cancellationToken CancellationToken
A token that can be used to cancel the operation.

Returns

Task
A task that represents the asynchronous operation.

Remarks:

If a cached tile already exists at the target path, this method does nothing.

This method uses StripedAsyncLocker to serialize writers for the same cache path. The tile is written to a temporary file first and then moved into place to ensure readers never observe a partially written file.

DeleteTileCore(Tile)

Deletes the specified tile from the cache.

protected void DeleteTileCore(Tile tile)

Parameters

tile Tile
The tile to delete.

Remarks:

This method uses StripedAsyncLocker to serialize deletes with concurrent writers for the same cache path.

ClearCache(TimeSpan)

Deletes cached tiles older than the specified expiration window.

public void ClearCache(TimeSpan tileExpiration)

Parameters

tileExpiration TimeSpan

        The age threshold. Files older than this value (based on file creation time) are deleted.

Remarks:

This method is a best-effort cleanup routine. It may skip files that are in use or cannot be deleted.

ClearCache(Double)

Deletes cached tiles until the cache size is at or below the specified limit.

public void ClearCache(double maxSizeInMegabytes)

Parameters

maxSizeInMegabytes Double
The maximum allowed cache size, in megabytes.

Remarks:

When the cache exceeds , this method deletes files starting from the lowest zoom-level directories first (based on directory naming).

This method is a best-effort cleanup routine. It may skip files that are in use or cannot be deleted.

ClearCache(TimeSpan, Double)

Deletes cached tiles older than the specified expiration window, and also enforces a maximum cache size.

public void ClearCache(TimeSpan tileExpiration, double maxSizeInMegabytes)

Parameters

tileExpiration TimeSpan

        The age threshold. Files older than this value (based on file creation time) are deleted.

maxSizeInMegabytes Double
The maximum allowed cache size, in megabytes.

Remarks:

This is a convenience overload that applies both cleanup policies in sequence.

ToString()

Returns a string that identifies this cache instance.

public string ToString()

Returns

String

        A string containing the cache directory and cache id.

ClearCacheCore()

Deletes all cached tiles for this cache instance.

protected void ClearCacheCore()

Remarks:

The delete operation is performed only if the directory structure matches the expected cache layout.

GetCacheFilePath(Int32, Int64, Int64)

Returns the cache file path for the specified zoom level and tile coordinates.

public string GetCacheFilePath(int zoom, long x, long y)

Parameters

zoom Int32
The zoom level of the tile.

x Int64
The X tile coordinate (column index).

y Int64
The Y tile coordinate (row index).

Returns

String

        The full file path for the cached tile image.

Remarks:

The returned path follows the cache layout: {CacheDirectory}\{CacheId}\{zoom}\{x}\{y}.{extension}.

Events

GottenCacheTile

Occurs when the GottenCacheTile event is raised.

public event EventHandler<GottenCacheImageBitmapTileCacheEventArgs> GottenCacheTile;

GettingCacheTile

Occurs when the GettingCacheTile event is raised.

public event EventHandler<GettingCacheImageBitmapTileCacheEventArgs> GettingCacheTile;

GottenTile

Occurs when the GottenTile event is raised.

public event EventHandler<GottenTileTileCacheEventArgs> GottenTile;

GettingTile

Occurs when the GettingTile event is raised.

public event EventHandler<GettingTileTileCacheEventArgs> GettingTile;

SavingTile

Occurs when the SavingTile event is raised.

public event EventHandler<SavingTileTileCacheEventArgs> SavingTile;

SavedTile

Occurs when the SavedTile event is raised.

public event EventHandler<SavedTileTileCacheEventArgs> SavedTile;