suparnatural-cache / com.suparnatural.core.cache / CacheStore

CacheStore

interface CacheStore

A persistent store to back a cache. This store is considered to be slow by default. Therefore, direct interaction with it to access cached objects should be avoided. The store is useful in certain scenarios.

For example, an in-memory cache can be rehydrated after a cold start of the application by loading from the backing CacheStore.

Properties

blocking

abstract val blocking: Boolean

Marker whether the store does any blocking I/O. If it is set to true, then it is expected that the calling thread will be blocked. Otherwise, certain method calls on the CacheStore will return immediately and all the operations will be scheduled on a separate worker thread.

preprocessors

abstract val preprocessors: List<CacheStorePreprocessor<Cacheable, Cacheable, Cacheable>>?

Chain of preprocessors to archive and unarchive Cacheable objects. While persisting, the preprocessors are applied to the incoming Cacheable instance and are expected to return an archived Cacheable instance. Before archiving, a CacheStore always adds a special CacheStorePreprocessor at the end of the preprocessors chain which outputs a RawCacheable.

Functions

fetchAllObjects

abstract fun fetchAllObjects(): List<Cacheable>

Returns a list containing all the objects persisted in the store. This method is always invoked on the calling thread and is not affected by blocking value.

flushAndClose

abstract fun flushAndClose(): <ERROR CLASS><Unit>

Close all the handles to the resources by flushing any scheduled tasks. This method should be used in conjunction when blocking is set to true. The store implementation is expected to guarantee that calling this method will not result in any loss and that all resources will be freed only after the schedules tasks are completed.

persistObject

abstract fun <T : Cacheable> persistObject(obj: T): Unit

Persists a Cacheable obj to the store. Uses a background worker if blocking is false.

unlinkObject

abstract fun unlinkObject(key: String): Unit

Deletes an object with the given key. Uses a background worker is blocking is false.

wipe

abstract fun wipe(): Unit

Calling this method guarantees that all traces of persisted objects are removed from the system. This method is always invoked on the calling thread and is not affected by blocking value.

Inheritors

DiskStore

class DiskStore : CacheStore

A CacheStore which persists Cacheable objects on the disk. By default, the store blocks the calling thread for all its operations which can changed by setting blocking to false.