FileSystemCache

class FileSystemCache<V : Any>(directory: File, encode: (V) -> String, decode: (String) -> V, ioContext: CoroutineContext = Dispatchers.IO, currentTimeMillis: () -> Long = System::currentTimeMillis) : TimestampedCache<V> , PersistentCache<V>

A durable PersistentCache that stores each entry as a file under directory.

Mirrors SwiftyNetwork's disk-backed cache. Values are turned into text by the caller-supplied encode/decode pair (e.g. kotlinx.serialization's Json.encodeToString/decodeFromString), so :cache stays free of any serialization dependency. Each file stores the epoch-millisecond timestamp on its first line followed by the encoded value, so it is a TimestampedCache and promotes into a memory layer without losing age (see LayeredCache).

File I/O runs on ioContext and is guarded by a Mutex. Writes use a same-directory temporary file and atomic replacement, so a process interruption leaves either the old complete entry or the new complete entry. Only files this cache wrote (suffixed SUFFIX) are touched, so the directory may be shared. A malformed or unreadable file reads back as null rather than throwing.

Parameters

directory

the storage directory; created on first write.

encode

serializes a value to text.

decode

parses text produced by encode back into a value.

ioContext

context for blocking file I/O; defaults to Dispatchers.IO.

currentTimeMillis

time source, injectable for deterministic tests.

Constructors

Link copied to clipboard
constructor(directory: File, encode: (V) -> String, decode: (String) -> V, ioContext: CoroutineContext = Dispatchers.IO, currentTimeMillis: () -> Long = System::currentTimeMillis)

Functions

Link copied to clipboard
open override fun changes(): Flow<CacheChange>

A hot stream of CacheChanges for this cache. The default is an empty flow, so caches that cannot observe mutations are simply non-reactive; observable caches override this.

Link copied to clipboard
open suspend override fun entry(key: CacheKey): CacheEntry<V>?

Atomically returns the value and its timestamp for key, or null if absent.

Link copied to clipboard
open suspend override fun removeAll()

Removes every entry.

Link copied to clipboard
open suspend override fun removeValue(key: CacheKey)

Removes any value stored for key.

Link copied to clipboard
open suspend override fun setValue(value: V, key: CacheKey)

Stores value for key, stamping it with the current time.

open suspend override fun setValue(value: V, key: CacheKey, timestamp: Long)

Stores value for key stamped with the given epoch-millisecond timestamp.

Link copied to clipboard
open suspend override fun timestamp(key: CacheKey): Long?

Returns the epoch-millisecond timestamp recorded for key, or null if absent.

Link copied to clipboard
open suspend override fun value(key: CacheKey): V?

Returns the value stored for key, or null if absent.