suparnatural-cache / com.suparnatural.core.cache / InMemoryCache

InMemoryCache

abstract class InMemoryCache : Cache

An abstract, thread-safe in-memory cache with a custom HashTable implementation backed by an array of atomic values. It guarantees consistency in both single and multi-threaded environment.

Constructors

<init>

InMemoryCache(size: Int, persistentStores: List<CacheStore> = emptyList(), replacementPolicy: CacheReplacementPolicy)

An abstract, thread-safe in-memory cache with a custom HashTable implementation backed by an array of atomic values. It guarantees consistency in both single and multi-threaded environment.

Properties

lock

val lock: <ERROR CLASS>

A read write lock for thread safety.

persistentStores

open val persistentStores: List<CacheStore>

List of persistent stores backing cache.

replacementPolicy

open val replacementPolicy: CacheReplacementPolicy

Cache replacement policy to create space when cache is full.

size

open val size: Int

Size of the cache

Functions

addObject

fun <T : Cacheable> addObject(obj: CacheableContainer): Boolean

Adds an object to persistent store. Assumes that lock is already obtained.

getAllObjects

open fun getAllObjects(): List<Cacheable>

Returns a list of all the cached objects.

hashCode

open fun hashCode(key: String): Int

Returns a hash code for the given key. For example, it may wrap the raw hash code for a string by cache sizeUnsafe.

rehydrate

open fun rehydrate(): Unit

Rehydrates the cache by loading all the cached objects from the persisted stores. It does a total replacement of current cache with the objects loaded from persistent stores and does not perform a merge of any sort.

removeObject

fun <T : Cacheable> removeObject(obj: CacheableContainer): <ERROR CLASS><T?, Boolean>

Removes the item from persistent stores. Assumes that lock is already obtained.

Inherited Functions

addObject

abstract fun <T : Cacheable> addObject(obj: T): Boolean

Adds a Cacheable object to the cache. The cache key is retrieved by calling Cacheable.cacheKey method.

getObject

abstract fun <T : Cacheable> getObject(key: String): T?

Returns a object with the given key.

removeObject

abstract fun <T : Cacheable> removeObject(key: String): <ERROR CLASS><T?, Boolean>

Removed a Cacheable object from the cache.

Inheritors

LinearProbingCache

class LinearProbingCache : InMemoryCache

A thread-safe InMemoryCache with a custom HashTable implementation based on Linear Probing.

RobinHoodProbingCache

class RobinHoodProbingCache : InMemoryCache

A thread-safe InMemoryCache with a custom HashTable implementation using Robin Hood hashing.