MemoryCache

class MemoryCache(nextCache: NormalizedCache? = null, maxSizeBytes: Int = Int.MAX_VALUE, expireAfterMillis: Long = -1) : NormalizedCache

Memory (multiplatform) cache implementation based on recently used property (LRU).

maxSizeBytes - the maximum size in bytes the cache may occupy. expireAfterMillis - after what timeout each entry in the cache treated as expired. By default there is no timeout.

Expired entries removed from the cache only on cache miss (loadRecord operation) and not removed from the cache automatically (there is no any sort of GC that runs in the background).

Constructors

Link copied to clipboard
constructor(nextCache: NormalizedCache? = null, maxSizeBytes: Int = Int.MAX_VALUE, expireAfterMillis: Long = -1)

Functions

Link copied to clipboard
Link copied to clipboard
open suspend override fun clearAll()

Clears all records from the cache.

Link copied to clipboard
open suspend fun close()
Link copied to clipboard
open suspend override fun dump(): Map<KClass<*>, Map<CacheKey, Record>>
Link copied to clipboard
suspend fun NormalizedCache.garbageCollect(maxAgeProvider: MaxAgeProvider, maxStale: Duration = Duration.ZERO, clock: () -> Long = { currentTimeMillis() }): GarbageCollectResult

Perform garbage collection on the cache.

Link copied to clipboard
open suspend override fun loadRecord(key: CacheKey, cacheHeaders: CacheHeaders): Record?
Link copied to clipboard
open suspend override fun loadRecords(keys: Collection<CacheKey>, cacheHeaders: CacheHeaders): Collection<Record>

Calls through to NormalizedCache.loadRecord. Implementations should override this method if the underlying storage technology can offer an optimized manner to read multiple records. There is no guarantee on the order of returned Record

Link copied to clipboard
open suspend override fun merge(record: Record, cacheHeaders: CacheHeaders, recordMerger: RecordMerger): Set<String>

open suspend override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders, recordMerger: RecordMerger): Set<String>

Calls through to NormalizedCache.merge. Implementations should override this method if the underlying storage technology can offer an optimized manner to store multiple records.

Link copied to clipboard
open suspend override fun remove(cacheKey: CacheKey, cascade: Boolean): Boolean

Remove a record and potentially its referenced records from this cache and all chained caches

open suspend override fun remove(cacheKeys: Collection<CacheKey>, cascade: Boolean): Int

Calls through to NormalizedCache.remove. Implementations should override this method if the underlying storage technology can offer an optimized manner to remove multiple records.

Link copied to clipboard

Remove all dangling references in the cache. A field is a dangling reference if its value (or, for lists, any of its values) is a reference to a record that does not exist.

Link copied to clipboard
suspend fun NormalizedCache.removeStaleFields(maxAgeProvider: MaxAgeProvider, maxStale: Duration = Duration.ZERO, clock: () -> Long = { currentTimeMillis() }): RemovedFieldsAndRecords

Remove all stale fields in the cache. A field is stale if its received date is older than its max age (configurable via maxAgeProvider) or if its expiration date has passed. A maximum staleness can be passed.

Link copied to clipboard

Remove all unreachable records in the cache. A record is unreachable if there exists no chain of references from the root record to it.

Link copied to clipboard
open override fun sizeOfRecord(record: Record): Int

Returns the size in bytes of a Record. This is an optional operation that can be implemented by the caches for debug purposes, otherwise it defaults to -1, meaning unknown size.

Link copied to clipboard
open suspend override fun trim(maxSizeBytes: Long, trimFactor: Float): Long

Trims the cache if its size exceeds maxSizeBytes. The amount of data to remove is determined by trimFactor. The oldest records are removed according to their updated date.