Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mori-atsushi/kotlin-cacheable
An annotation-based caching library for Kotlin Multiplatform
https://github.com/mori-atsushi/kotlin-cacheable
kotlin-compiler-plugin kotlin-multiplatform
Last synced: about 1 month ago
JSON representation
An annotation-based caching library for Kotlin Multiplatform
- Host: GitHub
- URL: https://github.com/mori-atsushi/kotlin-cacheable
- Owner: mori-atsushi
- License: apache-2.0
- Created: 2023-11-03T03:01:23.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-13T15:26:13.000Z (3 months ago)
- Last Synced: 2024-09-14T06:26:33.337Z (3 months ago)
- Topics: kotlin-compiler-plugin, kotlin-multiplatform
- Language: Kotlin
- Homepage:
- Size: 322 KB
- Stars: 98
- Watchers: 3
- Forks: 2
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - mori-atsushi/kotlin-cacheable - An annotation-based caching library for Kotlin Multiplatform (Kotlin)
- kmp-awesome - kotlin-cacheable - Annotation-based caching library (Libraries / 📦 Storage)
README
# Kotlin Cacheable
Kotlin Cacheable is an annotation-based caching library for Kotlin Multiplatform.
## Setup
This library is published to Maven Central.
Add the Maven Central repository to your project's `pluginManagement` and
`dependencyResolutionManagement` sections.```kotlin
// setting.gradle.kts
pluginManagement {
repositories {
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
repositories {
mavenCentral()
}
}
```Enable the Gradle plugin for the module where you want to use this library.
```kotlin
// build.gradle.kts
plugins {
kotlin(/* ... */)
id("com.moriatsushi.cacheable") version "0.0.3"
}
```You also need to add the `cacheable-core` dependency to the module.
```kotlin
// build.gradle.kts
dependencies {
implementation("com.moriatsushi.cacheable:cacheable-core:0.0.3")
}
```## Basic Usage
You can use `@Cacheable` annotation to cache the result of specified functions.
When you call a function with this annotation, it will return the cached value if the function is
called with the same arguments.Here is an example:
```kotlin
@Cacheable
fun getSomething(key: String): Something {
/* ... */
}
```This is equivalent to the following code:
```kotlin
private var cache = mutableMapOf()fun getSomething(key: String): Something =
cache.getOrPut(key) { /* ... */ }
```If you use the cacheable function within a class, the cache is only shared within an instance of the
class.You can also use `@Cacheable` annotation for a suspend function.
```kotlin
class Repository(private val api: Api) {
@Cacheable
suspend fun getUser(id: String): User =
api.getUser(id)
}
```## APIs
There are 2 parameters you can specify to `@Cacheable` annotation.* `maxCount`: The maximum number of cached values. If the number of cached values exceeds this
value, the cache with the oldest access time will be removed. If you don't specify this parameter,
the number of cached values will be unlimited.
* `lock`: When this is `true`, the function is guaranteed to be called only once even if the
function is called multiple times with the same arguments at the same time. Otherwise, the
function may be called multiple times with the same arguments at the same time. The default value
is false.```kotlin
class SomeClass {
@Cacheable(maxCount = 10, lock = true)
fun getSomething(key: String): Something {
// ...
}
}
```