DiskTileCache

Persistent disk cache for map tiles.

Stores up to 50MB of tiles in the app's cache directory, one file per tile, with the bytes exactly as the tile server sent them. Tiles persist across app restarts, reducing network usage on subsequent launches. When the size limit is reached, the least recently used tiles are deleted.

Tiles older than 7 days count as expired: get ignores them, so they are downloaded again, but they stay on disk as a fallback for when the download fails (e.g. offline), see the allowStale parameter of get.

Tiles are stored under a namespace (e.g. the tile source), so tiles of different sources never replace each other. All instances in a process share the size accounting per directory.

The cache stays usable after clear, and a failing cache only results in cache misses.

Parameters

context

Context used to locate the app's cache directory

namespace

Prefix for the cache keys, e.g. the name of the tile source

Constructors

Link copied to clipboard
constructor(context: Context, namespace: String = DEFAULT_NAMESPACE)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun clear()

Removes all cached tiles of all namespaces.

Link copied to clipboard
fun close()

Stops using the cache. Later calls to get and put are treated as cache misses. Other instances on the same directory keep working.

Link copied to clipboard
fun get(tile: TileCoordinate, allowStale: Boolean = false): Bitmap?

Returns the cached tile, decoded with RGB_565, or null if it is missing.

Link copied to clipboard
fun put(tile: TileCoordinate, bitmap: Bitmap)

Stores a decoded tile as PNG. Prefer the put overload with the original bytes, which avoids re-encoding.

fun put(tile: TileCoordinate, bytes: ByteArray)

Stores the tile bytes as sent by the tile server.