Skip to content

Easy to use file cache for Kotlin/JVM. Serializes Java classes and stores the data in a directory on a local drive.

License

Notifications You must be signed in to change notification settings

rtmigo/jaseca_kt

Repository files navigation

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.

Install

settings.gradle.kts

sourceControl {
    gitRepository(java.net.URI("https://github.com/rtmigo/jaseca_kt.git")) {
        producesModule("io.github.rtmigo:jaseca")
    }
}

build.gradle.kts

dependencies {
    implementation("io.github.rtmigo:jaseca") {
        version { branch = "staging" }
    }
}

Use

Configure, write, read

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"])
        }
}

⚠️ You should always use the cache inside use { } block or call .close() after use. Otherwise, the data may not be saved to disk.

Save structured data

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)
        }
}

License

Copyright © 2022 Artyom IG. Released under the Apache-2.0.

About

Easy to use file cache for Kotlin/JVM. Serializes Java classes and stores the data in a directory on a local drive.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published