ModelCache

class ModelCache(val directory: String, fileSystem: ModelFileSystem = defaultModelFileSystem(), val maxBytes: Long = Long.MAX_VALUE, clock: () -> Long = { currentTimeMillis() })(source)

On-disk model cache: a directory of downloaded model files with a size budget and least-recently-used eviction.

Files are addressed by plain file names (no directories). In-progress downloads (*.part) are ignored by the size accounting. Pinned files (models currently loaded by an engine) are never evicted.

Parameters

directory

cache directory; created on first use.

maxBytes

disk budget enforced by trimToSize and after touch.

clock

epoch-millisecond clock (injectable for tests).

Constructors

Link copied to clipboard
constructor(directory: String, fileSystem: ModelFileSystem = defaultModelFileSystem(), maxBytes: Long = Long.MAX_VALUE, clock: () -> Long = { currentTimeMillis() })

Properties

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard

Deletes abandoned *.part files (for example after the user cancels a download for good).

Link copied to clipboard
fun contains(fileName: String): Boolean

Whether a complete fileName is cached.

Link copied to clipboard
fun downloadRequest(url: String, fileName: String, expectedSha256: String? = null, expectedSizeBytes: Long? = null): DownloadRequest

A download request whose destination is fileName in this cache.

Link copied to clipboard

Complete cached files, least recently used first.

Link copied to clipboard
fun pathFor(fileName: String): String

Path for fileName inside the cache (the file may not exist yet; use it as a download destination).

Link copied to clipboard
fun pin(fileName: String): AutoCloseable

Pins fileName so it is never evicted while the returned handle is open (for example while an engine has it loaded). Pins nest; closing a handle twice is a no-op.

Link copied to clipboard
fun remove(fileName: String): Boolean

Deletes fileName unless it is pinned. Returns whether it was deleted.

Link copied to clipboard

Total bytes of complete cached files.

Link copied to clipboard
fun touch(fileName: String)

Marks fileName as used now, then enforces maxBytes.

Link copied to clipboard
fun trimToSize(targetBytes: Long = maxBytes): List<String>

Deletes least-recently-used, unpinned files until the cache holds at most targetBytes.