suparnatural-cache / com.suparnatural.core.cache / DiskStore

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.

A new file is created under location per cached object.

Note: Even though DiskStore does not impose any limits on the number of persisted objects, the underlying platform may impose a limit on number of files created by the application in which case the persistence will fail without any recovery.

Constructors

<init>

DiskStore(blocking: Boolean = true, preprocessors: List<CacheStorePreprocessor<Cacheable, Cacheable, Cacheable>>? = null, location: <ERROR CLASS> = FileSystem.contentsDirectory.absolutePath?.byAppending("cache")!!)

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.

Properties

blocking

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.

location

var location: <ERROR CLASS>

Location of cache directory on the disk. Defaults to DocumentsDirectory/cache.

preprocessors

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

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

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

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

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

unlinkObject

fun unlinkObject(key: String): Unit

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

wipe

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.