Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sksamuel/aedile
Kotlin Wrapper for Caffeine
https://github.com/sksamuel/aedile
cache caffeine coroutines java kotlin
Last synced: 4 days ago
JSON representation
Kotlin Wrapper for Caffeine
- Host: GitHub
- URL: https://github.com/sksamuel/aedile
- Owner: sksamuel
- License: apache-2.0
- Created: 2022-09-19T01:42:29.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-23T15:50:50.000Z (16 days ago)
- Last Synced: 2025-01-02T10:58:43.957Z (6 days ago)
- Topics: cache, caffeine, coroutines, java, kotlin
- Language: Kotlin
- Homepage:
- Size: 243 KB
- Stars: 177
- Watchers: 6
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Aedile
![main](https://github.com/sksamuel/aedile/workflows/main/badge.svg)
[](https://central.sonatype.com/search?q=aedile)
[](https://s01.oss.sonatype.org/content/repositories/snapshots/com/sksamuel/aedile/)Aedile is a simple Kotlin wrapper for [Caffeine](https://github.com/ben-manes/caffeine) which prefers coroutines rather
than Java futures.See [changelog](changelog.md)
## Features
* **Suspendable functions:** Rather than use Java's `CompletableFuture`s, all operations on Aedile are suspendable and
executed in their own coroutines.
* **Backed by Caffeine:** This is not a new cache implementation with its own bugs and quirks, but a simple wrapper
around Caffeine which has been used on the JVM for years.
* **Kotlin durations:** Specify expiration and refresh times in `kotlin.time.Duration` rather than Java durations.
* **Kotlin functions:** Wherever a function is required - eg eviction listener - Aedile supports Kotlin functions
rather than Java's Function interface.## Usage
Add Aedile to your build:
```groovy
implementation 'com.sksamuel.aedile:aedile-core:'
```Next, in your code, create a cache configuration using the standard Caffeine builder. Then, instead of using the
`buildAsync` methods that Caffeine provides, use the `asCache` or `asLoadingCache` methods that Aedile provides.```kotlin
val cache = Caffeine.newBuilder().asCache()
```With this cache we can request values if present, or supply a suspendable function to compute them.
```kotlin
val value1 = cache.getIfPresent("foo") // value or nullval value2 = cache.get("foo") {
delay(100) // look ma, we support suspendable functions!
"value"
}
```The `asLoadingCache` method supports a generic compute function which is used if no specific compute function is
provided.```kotlin
val cache = Caffeine.newBuilder().asLoadingCache() {
delay(1) // look ma, we support suspendable functions!
"value"
}cache.get("foo") // uses default compute, will return "value"
cache.get("bar") { "other" } // uses specific compute function to return "other"
```## Configuration
When creating the cache, Aedile wraps the standard Caffeine configuration options, adding extension functions to make
it easier to use Kotlin types - such as `kotlin.time.Duration` rather than Java's `Duration`.For example:
```kotlin
val cache = Caffeine
.newBuilder()
.expireAfterWrite(1.hours) // supports kotlin.time.Duration
.maximumSize(100) // standard Caffeine option
.asCache()
```## Evictions
Caffeine provides different approaches to eviction:
* expireAfterAccess(duration): Expire entries after the specified duration has passed since the entry was last accessed
by a read or write. This could be desirable if the cached data is bound to a session and expires due to inactivity.* expireAfterWrite(duration): Expire entries after the specified duration has passed since the entry was created, or the
most recent replacement of the value. This could be desirable if cached data grows stale after a certain amount of
time.* expireAfter(expiry): Pass an implementation of `Expiry` which has methods for specifying that expiry should occur
either after a duration from insert, a duration from last refresh, or a duration from last read.* invalidate / invalidateAll: Programatically remove entries based on their key(s) or remove all entries. In the case of
a loading cache, any currently loading values may not be removed.You can specify a suspendable function to listen to evictions using the `withEvictionListener` method.
```kotlin
val cache = Caffeine
.newBuilder()
.expireAfterWrite(1.hours) // supports kotlin.time.Duration
.maximumSize(100) // standard Caffeine option
.asCache()
.withEvictionListener { key, value, cause ->
when (cause) {
RemovalCause.SIZE -> println("Removed due to size constraints")
else -> delay(100) // suspendable for no real reason, but just to show you can!!
}
}.asCache()
```## Removals
Similar to evictions, you can specify a suspendable function to listen to removals using the `withRemovalListener`
method.```kotlin
val cache = Caffeine
.newBuilder()
.asCache()
.withRemovalListener { key, value, cause ->
...
}.asCache()
```## Coroutine Context
Aedile will use the context from the calling function for executing the compute functions. You can
specify your own context by just switching the context like with any suspendable call.```kotlin
val cache = Caffeine.newBuilder().asCache()
val value = cache.get("foo") {
withContext(Dispatchers.IO) {
// blocking database call
}
}
}
```## Metrics
[Micrometer](https://micrometer.io) provides integration which wraps the Caffeine cache classes.
To use this, call `.underlying()` to get access to the wrapped Caffeine instance.For example:
```kotlin
CaffeineCacheMetrics(cache.underlying().synchronous(), "my-cache-name", tags).bindTo(registry)
```