Skip to content

sokomishalov/kache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kache

Here should be some modern logo

Apache License 2

Overview

Kotlin/JVM useful coroutine-based key-value cache abstraction with several implementations.

Distribution

Library with modules are available only from jitpack so far:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Kache abstraction

Library provides common interface for cache operations:

interface Kache {

    // -----------------------------------------------------------------------------------//
    //  Methods that must be implemented.                                                 //
    // -----------------------------------------------------------------------------------//

    val serializer: Serializer? = null
    suspend fun getRaw(key: String): ByteArray?
    suspend fun putRaw(key: String, value: ByteArray)
    suspend fun findKeys(glob: GlobString): List<String>
    suspend fun delete(key: String)

    // -------------------------------------------------------------------------------------//
    //  Methods below have inefficient default implementation and should be overridden      // 
    //  for a better performance. This is one of main reasons why these methods are         //
    //  not extension functions.                                                            //
    // -------------------------------------------------------------------------------------//

    suspend fun <T> getOne(key: String, clazz: Class<T>): T? { /*...*/ }
    suspend fun <T> getList(key: String, elementClass: Class<T>): List<T> { /*...*/ }
    suspend fun <T> getMap(key: String, valueClass: Class<T>): Map<String, T> { /*...*/ }
    suspend fun <T> getFromMap(key: String, mapKey: String, clazz: Class<T>): T? { /*...*/ }
    suspend fun <T> put(key: String, value: T) { /*...*/ }
    suspend fun <T> addToList(key: String, clazz: Class<T>, vararg values: T): List<T> { /*...*/ }
    suspend fun <T> addToMap(key: String, clazz: Class<T>, additionalMap: Map<String, T>): Map<String, T> { /*...*/ }
    suspend fun findAllKeys(): List<String> { /*...*/ }
    suspend fun <T : Any> find(glob: GlobString, elementClass: Class<T>): List<T> { /*...*/ }
    suspend fun delete(keys: Iterable<String>) { /*...*/ }
    suspend fun <T> deleteFromList(key: String, clazz: Class<T>, vararg values: T): List<T> { /*...*/ }
    suspend fun <T> deleteFromMap(key: String, clazz: Class<T>, removalMap: Map<String, T>): Map<String, T> { /*...*/ }
    suspend fun deleteAll() { /*...*/ }
    suspend fun exists(key: String): Boolean { /*...*/ }
    suspend fun expire(key: String, ttlMs: Long) { /*...*/ }
}

Serializers

Serializers are required by cache implementations to serialize various types of data into different storage types. To use them you just have to put required dependencies in the classpath. There are several serializer implementations so far:

Implementations

There are several jvm kache implementations so far

Concurrent map

Import a dep:

<dependency>
    <groupId>com.github.sokomishalov</groupId>
    <artifactId>kache-concurrent-map</artifactId>
    <version>${kache.version}</version>
</dependency>

Then use this implementation:

val kache = ConcurrentMapKache()

Integration with org.springframework.cache.Cache

Import a dep:

<dependency>
    <groupId>com.github.sokomishalov</groupId>
    <artifactId>kache-spring</artifactId>
    <version>${kache.version}</version>
</dependency>

Then use this implementation

val kache = SpringKache(serializer = JacksonSerializer())

Redis with reactive Lettuce

Import a dep:

<dependency>
    <groupId>com.github.sokomishalov</groupId>
    <artifactId>kache-redis-lettuce</artifactId>
    <version>${kache.version}</version>
</dependency>

Then use this implementation

val kache = RedisLettuceKache(serializer = JacksonSerializer(), client = RedisClient.create())

Mongo with reactive streams driver

Import a dep:

<dependency>
    <groupId>com.github.sokomishalov</groupId>
    <artifactId>kache-mongo-reactive-streams</artifactId>
    <version>${kache.version}</version>
</dependency>

Then use this implementation

val kache = MongoReactiveStreamsKache(serializer = JacksonSerializer(), client = MongoClients.create())