https://github.com/be-hase/caffeine-coroutines
A simple extension that adds Kotlin Coroutines support to caffeine
https://github.com/be-hase/caffeine-coroutines
cache caffeine kotlin
Last synced: 3 months ago
JSON representation
A simple extension that adds Kotlin Coroutines support to caffeine
- Host: GitHub
- URL: https://github.com/be-hase/caffeine-coroutines
- Owner: be-hase
- License: mit
- Created: 2024-05-17T12:08:10.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-14T19:32:45.000Z (3 months ago)
- Last Synced: 2025-03-14T20:33:02.017Z (3 months ago)
- Topics: cache, caffeine, kotlin
- Language: Kotlin
- Homepage:
- Size: 188 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# caffeine-coroutines

## Motivation
There is a caching library called [Caffeine](https://github.com/ben-manes/caffeine),
which is used as a de facto standard in Java.I will make this library usable with Kotlin Coroutines.
## Install
### Gradle
```kotlin
implementation("dev.hsbrysk:caffeine-coroutines:{{version}}")
```### Maven
```xml
dev.hsbrysk
caffeine-coroutines
{{version}}```
## How to use
There is almost no difference from Caffeine.
The only thing you need to know is that by using *`buildCoroutine`*,
you can obtain a coroutine-compatible Cache instance.```kotlin
suspend fun main() {
val cache: CoroutineCache = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofMinutes(5))
.buildCoroutine() // Use buildCoroutineval value = cache.get("key") {
delay(1000) // You can use suspend functions.
"value"
}
println(value)
}
```Of course, it also supports the Loading Cache style.
```kotlin
suspend fun main() {
val cache: CoroutineLoadingCache = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofMinutes(5))
.buildCoroutine { // Use buildCoroutine
delay(1000) // You can use suspend functions.
"value"
}val value = cache.get("key")
println(value)
}
```## Philosophy
### We will primarily focus on coroutine support
We respect the widely used Caffeine API.
Introducing our own API would confuse users.
It would also make adoption more difficult and would be troublesome when discontinuing it after adoption.## Contributing
If there are any issues, please feel free to send a pull request.