FunCache is a Kotlin library that provides easy-to-use annotation for caching function results. By leveraging Kotlin Compiler Plugin, it ensures that caching is seamlessly integrated into your Kotlin code, offering improved performance with minimal boilerplate.
- Simple Annotation: Just add
@Cacheannotation to your function, and the library handles the rest. - Seamless Integration: Works with Kotlin's compiler plugin to automatically manage caching.
- Configurable: Customize cache behavior according to your needs.
- Thread-Safe: Ensures safe access to cache in multi-threaded environments.
- Note: Should only be used with "pure functions".
import org.jetbrains.kotlin.gradle.plugin.PLUGIN_CLASSPATH_CONFIGURATION_NAME
dependencies {
implementation("com.github.linhnvtit:fun-cache:1.0.0")
PLUGIN_CLASSPATH_CONFIGURATION_NAME("com.github.linhnvtit:fun-cache:1.0.0")
}repositories {
maven { url = uri("https://jitpack.io") }
}- Annotate your functions with @Cache to enable caching:
@Cache
fun fibonacci(num: Int): Int {
return when(num) {
0 -> 0
1 -> 1
else -> fibonacci(num-1) + fibonacci(num-2)
}
}
By adding the @Cache annotation, the fibonacci function's results will be cached, improving performance for repeated calls with the same parameters.
- FunCache also works fine with lambda functions too.
val lambdaFunc: (Int) -> Int = @Cache { TODO() }- You can customize the cache behavior using optional parameters in the @Cache annotation:
@Cache(capacity= 1000, strategy= CacheStrategy.LRU)
fun fibonacci(num: Int): Int { TODO() }- You can also clear the cache of an annotated function by passing its reflection to
FunCache.clearCache- Reference: Kotlin Reflection
FunCache.clearCache(::fibonacci)