CacheInstance - VerstSiu/kotlin_extension GitHub Wiki

duration and unit is optional. And their default values are 2000L and TimeUnit.MILLISECONDS for current version(1.0).

See also:

API List

package com.ijoic.ktx.guava.cache

/*
 * Cache
 */
fun<R, T> cache([duration: Long,] [unit: TimeUnit,] creator: () -> T): ReadOnlyProperty<R, T>

fun<R, T, PARAM> cacheFun([duration: Long,] [unit: TimeUnit,] creator: (PARAM) -> T): ReadOnlyProperty<R, (PARAM) -> T>
fun<R, T, P1, P2> cacheFun([duration: Long,] [unit: TimeUnit,] creator: (P1, P2) -> T): ReadOnlyProperty<R, (P1, P2) -> T>

/*
 * Weak cache
 */
fun<R, T> weakCache([duration: Long,] [unit: TimeUnit,] creator: () -> T): ReadOnlyProperty<R, T>

fun<R, T, PARAM> weakCacheFun([duration: Long,] [unit: TimeUnit,] creator: (PARAM) -> T): ReadOnlyProperty<R, (PARAM) -> T>
fun<R, T, P1, P2> weakCacheFun([duration: Long,] [unit: TimeUnit,] creator: (P1, P2) -> T): ReadOnlyProperty<R, (P1, P2) -> T>

Usage

singleton cache property:

class MyClass {

  private val toolbox by cache(20L, TimeUnit.SECONDS) { ToolBox() }
}

singleton param cache property:

class MyPreference private constructor(context: Context) {

  companion object {
    /**
     * Returns weak preference instance.
     */
    val getInstance by cacheFun { it: Context -> MyPreference(it) }
  }
}
⚠️ **GitHub.com Fallback** ⚠️