🗄️ Cache#
A high-performance, coroutine-based cache library for Kotlin Multiplatform.
It is designed for high concurrency and thread-safety, offering flexible configurations for size, time and policy-based eviction.
🎯 Supported Targets#
The following targets are supported:
| Platform | Targets |
|---|---|
| JVM & Android | jvm, android |
| Apple | ios, macos, tvos, watchos |
| Web | js, wasmJs |
| Native & Other | androidNative, linux, mingw, wasmWasi |
✨ Features#
- Coroutine-Based: Utilizes
suspendfunctions for non-blocking cache operations - Thread-Safe: Safe for concurrent access from multiple coroutines
- Size-Binding: You can enforce a maximum cache size
- Eviction Policies: Supports several strategies
- LRU: Least Recently Used
- MRU: Most Recently Used
- LFU: Least Frequently Used
- FIFO: First In, First Out
- FILO: First In, Last Out
- Time-Based Expiry: Configure entries to expire after write or after access
- Flexible API: Provides both
suspendfunctions for atomic operations non-suspendingtry...methods for fast, non-blocking lookups - AutoClosable: Can be used in
use { ... }blocks to release resources if needed.
🚀 Installation#
Integration using Version Catalog is highly recommended for aligned version usage.
Then add the dependency to your module:
🛠️ Usage#
You can initialize the cache with a type-safe DSL. The maxSize parameter is mandatory, while additional configuration options are optional.
The cache interface follows the suspend-first approach, providing common methods for cache as suspending operations.
suspend fun getCachedTitle(id: Int): String {
return cache.get(id) ?: "Default Title"
}
suspend fun putCachedTitle(id: Int, title: String) {
cache.put(id, title)
}
The get and put methods are also available as operator functions, like this:
suspend fun getCachedTitle(id: Int): String {
return cache[id] ?: "Default Title"
}
suspend fun putCachedTitle(id: Int, title: String) {
cache[id] = title
}