CachedTileSource

class CachedTileSource(inner: TileSource?, store: TileStore, revalidationScope: CoroutineScope = GlobalScope) : TileSource, OfflineToggleable, CachedSourceInfoProvider, RevalidatingSource

Cache-first decorator over a TileSource. The lookup order is:

  1. Read store. Hit → return cached blob (no network); if the blob is older than the store's eviction CachePolicy.staleAfter, also kick a background refresh (stale-while-revalidate — see maybeRevalidate).

  2. Miss → fetch from inner (network), write-through to store, return.

When inner is null, this acts as a pure cache view — useful for replaying a GPKG file offline (e.g. opened in QGIS-style "browse cached layers" mode). A network failure with no cached blob propagates as null from fetchTile.

Cache hits bypass previousEtag / previousLastModified entirely — the cached blob is served verbatim. Pass those revalidation headers in only when you want the inner source to issue a conditional GET, which the decorator only does on a miss.

Constructors

Link copied to clipboard
constructor(inner: TileSource?, store: TileStore, revalidationScope: CoroutineScope = GlobalScope)

Properties

Link copied to clipboard
open override val cacheInfo: CachedSourceInfo?

Delegates to the underlying store when it reports a CachedSourceInfo; null when the store isn't cache-introspectable (custom in-memory store, etc.).

Link copied to clipboard
open override var isCacheOnly: Boolean

When true, fetchTile never calls inner — cache hits are served as usual, cache misses return null. Equivalent to constructing with inner = null, but toggleable at runtime so a single layer can flip between online and offline modes without re-wiring its tile factory.

Link copied to clipboard

The wrapped upstream tile source (e.g. WmsTileSource, WmtsTileSource, UrlTemplateImageTileSource). Exposed for rebind flows — attachCache extractors peel this off when the layer is already cache-attached and being rebound to a different content manager. null for offline-only caches.

Link copied to clipboard
open override var onTileRevalidated: (z: Int, x: Int, y: Int) -> Unit?

Invoked (off the render thread) after a stale tile is re-downloaded and written through, so the render layer can drop the tile's cached texture and redraw. null = no swap; the fresh tile then appears on the next texture (re)load.

Link copied to clipboard

The backing store. Exposed for bulk-download flows that persist store-level metadata (e.g. the downloaded-region bounding sector).

Functions

Link copied to clipboard
suspend fun bulkFetchTile(z: Int, x: Int, y: Int, overrideCache: Boolean = false): TileBlob?

Bulk-download semantics: by default skip already-cached tiles, otherwise force a network fetch and write through to store. Unlike fetchTile, this bypasses isCacheOnly — bulk is a user-initiated "fetch these bytes now" operation orthogonal to the renderer's offline toggle. Returns the blob (cached or freshly fetched), or null when there's no cached tile and no network source / network fetch failed.

Link copied to clipboard
open fun close()

Release any resources held by the source (HTTP client, etc.). Idempotent default no-op.

Link copied to clipboard
open suspend override fun fetchTile(z: Int, x: Int, y: Int, previousEtag: String? = null, previousLastModified: String? = null): TileBlob?
Link copied to clipboard
fun TileSource.launchBulkRetrieval(levelSet: LevelSet, sector: Sector, resolution: ClosedRange<Angle>, scope: CoroutineScope = GlobalScope, maxRetries: Int = 3, retryTimeoutShort: Duration = 5.seconds, retryTimeoutLong: Duration = 15.seconds, onProgress: (downloaded: Long, skipped: Long, total: Long) -> Unit? = null): Job

Download every tile that intersects sector across every level whose resolution falls in resolution — typically used by "save a region for offline use" flows. Returns the launched Job so the caller can cancel mid-download.

Link copied to clipboard
fun trackBulkJob(job: Job)

Pause stale-while-revalidate for the lifetime of job (a bulk region download using this source). SWR resumes automatically when the job completes or is cancelled.

Link copied to clipboard
open suspend override fun tryReadCachedTile(z: Int, x: Int, y: Int): TileBlob?

Cache-only fast-path read for (z, x, y). Default returns null — a plain network source has no cache to consult. Cache-backed sources override to read their store without going through the network-fetch concurrency budget.