Here should be some modern logo
Kotlin/JVM useful coroutine-based key-value cache abstraction with several implementations.
Library with modules are available only from jitpack
so far:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
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 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:
- JdkSerializableSerializer - works only with java.io.Serializable objects
- JacksonSerializer (preferred) - jackson implementation
- GsonSerializer - gson implementation
- XStreamSerializer - xstream implementation
- KryoSerializer - kryo implementation
There are several jvm kache implementations so far
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()
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())
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())
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())