Giter VIP home page Giter VIP logo

cache4k's Introduction

cache4k

CI Maven Central License

In-memory Cache for Kotlin Multiplatform.

Work in progress.


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.newBuilder().build<Long, String>()

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.newBuilder().build<Long, User>()

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

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.newBuilder()
    .expireAfterAccess(24.hours)
    .build<Long, String>()

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.newBuilder()
    .expireAfterWrite(30.minutes)
    .build<Long, String>()

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.newBuilder()
    .maximumCacheSize(100)
    .build<Long, String>()

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.newBuilder()
    .build<Long, String>()
    
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.newBuilder().build<Long, String>()
cache.put(1, "dog")

cache.invalidate(1)

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

To delete all entries in the cache:

cache.invalidateAll()

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.newBuilder()
        .fakeTimeSource(fakeTimeSource)
        .expireAfterWrite(1.minutes)
        .build<Long, String>()

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

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.

cache4k's People

Contributors

ychescale9 avatar

Watchers

 avatar

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.