Giter Club home page Giter Club logo

cache4k's Introduction

cache4k

CI Maven Central License

In-memory Cache for Kotlin Multiplatform.

Work in progress.

cache4k provides a simple in-memory key-value cache for Kotlin Multiplatform, with support for time-based (expiration) and size-based evictions.

Note that only the new Kotlin Native memory model is supported.

The following targets are currently supported:

  • jvm
  • js
  • wasmJs
  • iosX64
  • iosArm64
  • iosSimulatorArm64
  • macosX64
  • macosArm64
  • tvosX64
  • tvosArm64
  • tvosSimulatorArm64
  • watchosX64
  • watchosArm64
  • watchosSimulatorArm64
  • linuxX64
  • linuxArm64
  • mingwX64

Download

Dependencies are hosted on Maven Central.

Android

dependencies {
    implementation("io.github.reactivecircus.cache4k:cache4k:x.y.z")
}

Multiplatform

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation("io.github.reactivecircus.cache4k:cache4k:x.y.z")
            }
        }
    }
}

Usage

Writing and reading cache entries

To create a new Cache instance using Long for the key and String for the value:

val cache = Cache.Builder<Long, String>().build()

To start writing entries to the cache:

cache.put(1, "dog")
cache.put(2, "cat")

To read a cache entry by key:

cache.get(1) // returns "dog"
cache.get(2) // returns "cat"
cache.get(3) // returns null

To overwrite an existing cache entry:

cache.put(1, "dog")
cache.put(1, "bird")
cache.get(1) // returns "bird"

Cache loader

Cache provides an API for getting cached value by key and using the provided loader: suspend () -> Value lambda to compute and cache the value automatically if none exists.

runBlockingTest {
    val cache = Cache.Builder<Long, User>().build()

    val userId = 1L
    val user = cache.get(userId) {
        fetchUserById(userId) // potentially expensive call (might be a suspend function)
    }

    // value successfully computed by the loader will be cached automatically
    assertThat(user).isEqualTo(cache.get(userId))
}

Note that loader is executed on the caller's coroutine context. Concurrent calls from multiple threads using the same key will be blocked. Assuming the 1st call successfully computes a new value, none of the loader from the other calls will be executed and the cached value computed by the first loader will be returned for those calls.

Any exceptions thrown by the loader will be propagated to the caller of this function.

Expirations and evictions

By default, Cache has an unlimited number of entries which never expire. But a cache can be configured to support both time-based expirations and size-based evictions.

Time-based expiration

Expiration time can be specified for entries in the cache.

Expire after access

To set the maximum time an entry can live in the cache since the last access (also known as ** time-to-idle**), where "access" means reading the cache, adding a new cache entry, or ** replacing an existing entry with a new one**:

val cache = Cache.Builder<Long, String>()
    .expireAfterAccess(24.hours)
    .build()

An entry in this cache will be removed if it has not been read or replaced after 24 hours since it's been written into the cache.

Expire after write

To set the maximum time an entry can live in the cache since the last write (also known as ** time-to-live**), where "write" means adding a new cache entry or replacing an existing entry with a new one:

val cache = Cache.Builder<Long, String>()
    .expireAfterWrite(30.minutes)
    .build()

An entry in this cache will be removed if it has not been replaced after 30 minutes since it's been written into the cache.

Note that cache entries are not removed immediately upon expiration at exact time. Expirations are checked in each interaction with the cache.

Size-based eviction

To set the maximum number of entries to be kept in the cache:

val cache = Cache.Builder<Long, String>()
    .maximumCacheSize(100)
    .build()

Once there are more than 100 entries in this cache, the least recently used one will be removed, where "used" means reading the cache, adding a new cache entry, or replacing an existing entry with a new one.

Getting all cache entries as a Map

To get a copy of the current cache entries as a Map:

val cache = Cache.Builder<Long, String>()
    .build()

cache.put(1, "dog")
cache.put(2, "cat")

assertThat(cache.asMap())
    .isEqualTo(mapOf(1L to "dog", 2L to "cat"))

Note that calling asMap() has no effect on the access expiry of the cache.

Deleting cache entries

Cache entries can also be deleted explicitly.

To delete a cache entry for a given key:

val cache = Cache.Builder<Long, String>().build()
cache.put(1, "dog")

cache.invalidate(1)

assertThat(cache.get(1)).isNull()

To delete all entries in the cache:

cache.invalidateAll()

Event listener

You can set an event listener as a lambda:

val cache1 = Cache.Builder<Long, String>()
    .eventListener { event ->
        println("onEvent: $event")
    }
    .build()

Or declare it as a class and share logic across many stores:

class FileDeleteEventListener : CacheEventListener<Long, File> {
    override fun onEvent(event: CacheEvent<Long, File>) {
        when(event) {
            is CacheEvent.Created -> {}
            is CacheEvent.Updated -> event.oldValue.delete()
            is CacheEvent.Evicted -> event.value.delete()
            is CacheEvent.Expired -> event.value.delete()
            is CacheEvent.Removed -> event.value.delete()
        }
    }
}
val fileDeleteEventListener = FileDeleteEventListener()

val cache1 = Cache.Builder<Long, File>()
    .eventListener(fileDeleteEventListener)
    .build()

val cache2 = Cache.Builder<Long, File>()
    .eventListener(fileDeleteEventListener)
    .build()

Cache entry event firing behaviors for mutative methods:

Initial value Operation New value Event
{} put(K, V) {K: V} Created(K, V)
{K: V1} put(K, V2) {K: V2} Updated(K, V1, V2)
{K: V} invalidate(K) {} Removed(K, V)
{K1: V1, K2: V2} invalidateAll() {} Removed(K1, V1), Removed(K2, V2)
{K: V} any operation, K expired {} Expired(K, V)
{K1: V1} put(K2, V2), K1 evicted {K2: V2} Created(K2, V2), Evicted(K1, V1)

Unit testing cache expirations

To test logic that depends on cache expiration, pass in a FakeTimeSource when building a Cache so you can programmatically advance the reading of the time source:

@Test
fun cacheEntryEvictedAfterExpiration() {
    private val fakeTimeSource = FakeTimeSource()
    val cache = Cache.Builder<Long, String>()
        .timeSource(fakeTimeSource)
        .expireAfterWrite(1.minutes)
        .build()

    cache.put(1, "dog")

    // just before expiry
    fakeTimeSource += 1.minutes - 1.nanoseconds

    assertThat(cache.get(1))
        .isEqualTo("dog")

    // now expires
    fakeTimeSource += 1.nanoseconds

    assertThat(cache.get(1))
        .isNull()
}

Credits

The library was ported from a kotlin / JVM cache which I contributed to dropbox/Store to help unblock Store's multiplatform support ( it was reverted before the 1.0 release as multiplatform wasn't a priority). Many thanks to Store's owners and contributors for reviewing and improving the original implementation.

License

Copyright 2021 Yang Chen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

YourKit

YourKit supports open source projects with innovative and intelligent tools for monitoring and profiling Java and .NET applications. YourKit is the creator of YourKit Java Profiler, YourKit .NET Profiler, and YourKit YouMonitor.

cache4k's People

Contributors

andreypfau avatar darkxanter avatar dcvz avatar ho2ri2s avatar javiersegoviacordoba avatar komdosh avatar luca992 avatar scottpierce avatar svok avatar xbaank avatar ychescale9 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cache4k's Issues

KMM New Memory Model

@ychescale9 I see that you enabled the new memory model in a previous commit. Do you plan to only support the new memory model in the future or do you wish to support both?

In the new memory model there would no longer be a need for IsoMap. Just safe concurrent access.

I'd be happy to work on an implementation if you let me know which route you were intending to take.

The instance of the TimeMark interface has been changed in Kotlin 1.7.0-Beta

Kotlin has just released 1.7.0-Beta.

But this version raise an exception:

java.lang.IncompatibleClassChangeError: Found interface kotlin.time.TimeMark, but class was expected
	at io.github.reactivecircus.cache4k.RealCache.recordWrite(RealCache.kt:236)
	at io.github.reactivecircus.cache4k.RealCache.put(RealCache.kt:136)
	at io.github.reactivecircus.cache4k.RealCache$get$3.invokeSuspend(RealCache.kt:112)

It's appear when we use expire.

Cache.Builder()
    .maximumCacheSize(100)
    .expireAfterWrite(5.minutes)
    .build<String, String>()

Kotlin 1.6.20

public object Monotonic : TimeSource by MonotonicTimeSource {
    override fun toString(): String = MonotonicTimeSource.toString()
}

Kotlin 1.7.0-Beta

public object Monotonic : TimeSource {
    override fun markNow(): ValueTimeMark = MonotonicTimeSource.markNow()
    override fun toString(): String = MonotonicTimeSource.toString()

    @ExperimentalTime
    @SinceKotlin("1.7")
    @JvmInline
    public value class ValueTimeMark internal constructor(internal val reading: ValueTimeMarkReading) : TimeMark {
        override fun elapsedNow(): Duration = MonotonicTimeSource.elapsedFrom(this)
        override fun plus(duration: Duration): ValueTimeMark = MonotonicTimeSource.adjustReading(this, duration)
        override fun minus(duration: Duration): ValueTimeMark = MonotonicTimeSource.adjustReading(this, -duration)
        override fun hasPassedNow(): Boolean = !elapsedNow().isNegative()
        override fun hasNotPassedNow(): Boolean = elapsedNow().isNegative()
    }
}

Same key Expiring twice

same key is expiring twice with this config

val cache = Cache.Builder<String, String>()
        .maximumCacheSize(1_00)
        .expireAfterWrite(2.seconds)
        .expireAfterAccess(5.seconds)
        .eventListener { event ->
            when (event) {
                is CacheEvent.Created -> {}
                is CacheEvent.Updated -> {}
                is CacheEvent.Evicted -> {
                    scope.launch {
                        println("Evicted ${event.key}")
                    }
                }
                is CacheEvent.Expired -> {
                    scope.launch {
                        println("Expire ${event.key}")
                    }
                }
                is CacheEvent.Removed -> {
                    println("Evicted")
                }
            }
        }
        .build()

here I launch different coroutine adding value in the cache


for (n in 0..100) {
        delay(100)
        scopeL.launch {
            schemas.put("n$n", "some value")
            println("n$n")
        }
    }

Result

...
adding n18
 adding n19
 adding n20
Expire n0
Expire n2
Expire n1
Expire n0
Expire n1
Expire n2
 adding n21
Expire n3
Expire n3
 adding n22
Expire n4
Expire n4
 adding n23
Expire n5
Expire n5
 adding n24
Expire n6
Expire n6
 adding n25
...

The same keys are expiring twice

Crash at Runtime Startup

Hey , Thanks for your work, I am interested in using your Lib in one of my projects : SpotiFlyer,
But unfortunately, It crashes as Runtime.

java.lang.NoSuchMethodError: No virtual method getINFINITE-UwyO8pc()D in class Lkotlin/time/Duration$Companion; or its super classes (declaration of 'kotlin.time.Duration$Companion' appears in /data/data/com.shabinder.spotiflyer/code_cache/.overlay/base.apk/classes3.dex)
        at io.github.reactivecircus.cache4k.CacheBuilderImpl.<init>(Cache.kt:109)
        at io.github.reactivecircus.cache4k.Cache$Builder$Companion.newBuilder(Cache.kt:99)

value is always returning null

code
fun storeCredentials(value: String) {
val cache = Cache.Builder<String, String>().build()
cache.put(KeyConstant.userCredentials, value)
val value = cache.get(KeyConstant.userCredentials)
print(value)
}

fun retrieveCredentials(): String? {
    val cache = Cache.Builder<String, String>().build()
    val creds = cache.get(KeyConstant.userCredentials)
    return creds
}

Duplicate class error since 0.10.0 (Stately)

In one of my projects Dependabot wants to update cache4k from 0.9.0 to 0.10.0 but now the build fails with

Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class co.touchlab.stately.HelpersJVMKt found in modules stately-common-jvm-2.0.0-rc1 (co.touchlab:stately-common-jvm:2.0.0-rc1) and stately-strict-jvm-2.0.0-rc1 (co.touchlab:stately-strict-jvm:2.0.0-rc1)

Note: This is an Android/JVM project, no multiplatform project.

ConcurrentModificationException will be reported when concurrent

java.util.ConcurrentModificationException
        java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:756)
        java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:778)
        io.github.reactivecircus.cache4k.RealCache.expireEntries(RealCache.kt:174)
        io.github.reactivecircus.cache4k.RealCache.put(RealCache.kt:120)

Non-daemon thread? [JVM]

Using this library for JVM I noticed that my app based on compose-jb won't exit properly.
I suspect that there is a thread running in the background that prevents shutdown, but I don't see a option to shutdown the cache.

I suggest that for time-based evictions you should use a daemon thread.

Cache is not synchronised by key

If you run this code

runBlocking {
  launch { cache.get(0) { fetchUser() } }
  launch { cache.get(0) { fetchUser() } }
  launch { cache.get(0) { fetchUser() } }
  launch { cache.get(0) { fetchUser() } }
}

suspend fun fetchUser(): Int {
  delay(100)
  println("Hitting the network to fetch user!")
  return 0
}

then the message "Hitting the network to fetch user!" is printed 4 times.

I would expect this cache to be synchronised by key in a way that when multiple threads are trying to read a value from a key in which the value is suspend then it will await till the value is computed and then deliver it, rather than calling it once per thread and then race to see who writes to the cache first.

Caching get clear after exit app

Hi,
put and get caching is working for the first time but when the user exits app and tries to get cache data it prints null.

Am using koltin
`
val cache = Cache.Builder()
.expireAfterAccess(1.minutes).build<Long, String>()

       saveValue.setOnClickListener { cache.put(1,"kona") }

    getValue.setOnClickListener { Log.d("value",""+cache?.get(1)) }`

Any info more needed I will provide

[Feature] Support listening to cache expiry

In my application, I'd like to cache instances of an object which require to be shut down properly via a shutdown() method.

I want to cache them, since idk how expensive they are to instantiate and sometimes they might be used frequently for a bit, before not being needed for a while.

The only problem is, I can't add an eviction listener to the Cache.
Something along the lines of

Cache.Builder.newBuilder()
        .expireAfterAccess(hours(4))
        .maximumCacheSize(50)
        .expiryListener { it -> it.shutdown }
        .build()

would be nice.

Support for null values and loader in the constructor

  1. In the current implementation null is returned when an element is not in the cache. There are use cases where the actual value itself is null. Support for null values would be good.

  2. The overloaded get method accepts a suspendable loader function. Writing get(key, () -> Value) in every place makes the code too verbose when the loading logic is same for all calls. So, having a cache which accepts loader in the constructor to create value in the cache if it doesn't already have would be nice.

We have implemented a wrapper with a getValue method which returns the value or null to solve these problems. Having such an API as part of the library would be good.

Dynamic TTL

Is it possible to set the expiration time during execution of the loader?

Trying to cache some oauth tokens whose expiration time is known when the loader gets them. Not before.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.