
Lightweight caching library supports both in-memory and persistent caches with various eviction strategies (LRU, FIFO, MRU, FILO), offering coroutine-friendly operations and a simple, modern API.
Kache is a lightweight Kotlin Multiplatform caching library that supports both in-memory and persistent caches and supports different eviction strategies (LRU, FIFO, MRU, FILO).
Supported platforms:
Kotlin DSL:
repositories {
mavenCentral()
// Add only if you're using snapshot version
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
dependencies {
// For in-memory cache
implementation("com.mayakapps.kache:kache:<version>")
// For persistent cache
implementation("com.mayakapps.kache:file-kache:<version>")
}Groovy DSL:
repositories {
mavenCentral()
// Add only if you're using snapshot version
maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
// For in-memory cache
implementation "com.mayakapps.kache:kache:<version>"
// For persistent cache
implementation "com.mayakapps.kache:file-kache:<version>"
}Don't forget to replace <version> with the latest found on the badges above or the desired version.
You can create your cache using the following builder DSL, then you can use the usual operators of get, put, and remove and their different implementations.
Sample Code (Kache):
// Could be InMemoryKache
val cache = Kache<String, ByteArray>(maxSize = 5 * 1024 * 1024) { // 5 MB
// Other optional configurations
strategy = KacheStrategy.LRU
// ...
}
// ...
val newValue = cache.put(uniqueKey) {
try {
// Some CPU-intensive process - Returning a not null value means success
} catch (ex: Throwable) {
// Handle exception
null // returning null means creating the value has failed - The value (null) will not be cached
}
}
// ...
val cachedValue = cache.get(uniqueKey)Sample Code (FileKache):
// Could be OkioFileKache or JavaFileKache
val cache = FileKache(directory = "cache", maxSize = 10 * 1024 * 1024) {
// Other optional configurations
strategy = KacheStrategy.MRU
// ...
}
// ...
try {
val imageData = cache.getOrPut(uniqueKey) { cacheFilename ->
try {
// downloadFromInternet(imageUrl, cacheFilename)
true // returning true means caching has succeeded - The file will be kept
} catch (ex: IOException) {
// Handle exception
false // returning false means caching has failed - The file will be deleted
}
}
} finally {
cache.close()
}See documentation here
All the code inside this library is licensed under Apache License 2.0 unless explicitly stated otherwise.
All contributions are welcome. If you are reporting an issue, please use the provided template. If you're planning to contribute to the code, please open an issue first describing what feature you're planning to add or what issue you're planning to fix. This allows better discussion and coordination of efforts. You can also check open issues for bugs/features that needs to be fixed/implemented.
Kache is a lightweight Kotlin Multiplatform caching library that supports both in-memory and persistent caches and supports different eviction strategies (LRU, FIFO, MRU, FILO).
Supported platforms:
Kotlin DSL:
repositories {
mavenCentral()
// Add only if you're using snapshot version
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
dependencies {
// For in-memory cache
implementation("com.mayakapps.kache:kache:<version>")
// For persistent cache
implementation("com.mayakapps.kache:file-kache:<version>")
}Groovy DSL:
repositories {
mavenCentral()
// Add only if you're using snapshot version
maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
// For in-memory cache
implementation "com.mayakapps.kache:kache:<version>"
// For persistent cache
implementation "com.mayakapps.kache:file-kache:<version>"
}Don't forget to replace <version> with the latest found on the badges above or the desired version.
You can create your cache using the following builder DSL, then you can use the usual operators of get, put, and remove and their different implementations.
Sample Code (Kache):
// Could be InMemoryKache
val cache = Kache<String, ByteArray>(maxSize = 5 * 1024 * 1024) { // 5 MB
// Other optional configurations
strategy = KacheStrategy.LRU
// ...
}
// ...
val newValue = cache.put(uniqueKey) {
try {
// Some CPU-intensive process - Returning a not null value means success
} catch (ex: Throwable) {
// Handle exception
null // returning null means creating the value has failed - The value (null) will not be cached
}
}
// ...
val cachedValue = cache.get(uniqueKey)Sample Code (FileKache):
// Could be OkioFileKache or JavaFileKache
val cache = FileKache(directory = "cache", maxSize = 10 * 1024 * 1024) {
// Other optional configurations
strategy = KacheStrategy.MRU
// ...
}
// ...
try {
val imageData = cache.getOrPut(uniqueKey) { cacheFilename ->
try {
// downloadFromInternet(imageUrl, cacheFilename)
true // returning true means caching has succeeded - The file will be kept
} catch (ex: IOException) {
// Handle exception
false // returning false means caching has failed - The file will be deleted
}
}
} finally {
cache.close()
}See documentation here
All the code inside this library is licensed under Apache License 2.0 unless explicitly stated otherwise.
All contributions are welcome. If you are reporting an issue, please use the provided template. If you're planning to contribute to the code, please open an issue first describing what feature you're planning to add or what issue you're planning to fix. This allows better discussion and coordination of efforts. You can also check open issues for bugs/features that needs to be fixed/implemented.