Easy to use file cache for Kotlin/JVM.
Stores data in a directory on a local drive. Allows you to keep data of any types that are
java.io.Serializable
.
import io.github.rtmigo.jaseca.filecache
import java.nio.file.Paths
fun main() {
filecache<String, Int>(Paths.get("/path/to/cache_dir"))
.use { cache ->
// writing to cache
cache["first key"] = 10
cache["second key"] = 20
// reading from cache
println(cache["first key"])
}
}
"Jaseca" stands for JAva.io.SErializable CAche.
In fact, this is a thin wrapper around Ehcache.
sourceControl {
gitRepository(java.net.URI("https://github.com/rtmigo/jaseca_kt.git")) {
producesModule("io.github.rtmigo:jaseca")
}
}
dependencies {
implementation("io.github.rtmigo:jaseca") {
version { branch = "staging" }
}
}
import io.github.rtmigo.jaseca.filecache
import java.nio.file.Paths
import kotlin.time.Duration.Companion.minutes
fun main() {
filecache<String, Int>(Paths.get("/path/to/cache_dir")) {
// optional configuration block
maxEntries = 1000
timeToIdle = 15.minutes
}
.use { cache ->
// writing to cache
cache["first key"] = 10
cache["second key"] = 20
// reading from cache
println(cache["first key"])
}
}
use { }
block or call .close()
after use. Otherwise, the data may not be saved to disk.
Just inherit your Kotlin class from java.io.Serializable
to make it compatible.
import io.github.rtmigo.jaseca.filecache
import java.nio.file.Paths
data class Planet(val radius: Double, val period: Double)
: java.io.Serializable // this makes the object compatible
fun main() {
filecache<String, Planet>(Paths.get("/path/to/cache_dir"))
.use { cache ->
cache["Mars"] = Planet(389.5, 686.980)
cache["Mercury"] = Planet(2439.7, 87.9691)
}
}
Copyright © 2022 Artyom IG. Released under the Apache-2.0.