All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.keep.CacheUtils.kt Maven / Gradle / Ivy

package keep

import keep.exceptions.CacheLoadException
import keep.exceptions.CacheSaveException
import koncurrent.FailedLater
import koncurrent.Later
import koncurrent.later.catch
import koncurrent.later.then
import kotlinx.serialization.KSerializer
import kotlinx.serialization.serializer

/**
 * Save object [T] on to the [Cache] with a [key]
 *
 * @see [save]
 *
 * @return a [Later] that
 * - on success: resolves the saved object as it was cached
 * - on failure: rejects with a [CacheSaveException]
 */
inline fun  Cache.save(key: String, obj: T) = try {
    save(key, obj, serializer())
} catch (e: Throwable) {
    FailedLater(CacheSaveException(key, cause = e))
}

/**
 * Save object [T] on to the [Cache] with a [key] and an optional [serializer]
 *
 * @see [Cache.save]
 *
 * @return [Later] that
 * - on success: resolves the saved object as it was cached
 * - on failure: resolves with a null
 */
inline fun  Cache.saveOrNull(
    key: String, obj: T, serializer: KSerializer? = null
): Later = try {
    save(key, obj, serializer ?: serializer())
} catch (e: Throwable) {
    FailedLater(CacheSaveException(key, cause = e))
}.then {
    it as? T
}.catch { null }

/**
 * Load object [T] from the [Cache], that was saved with a [key] with an optional serializer [serializer]
 *
 * @see [load]
 *
 * @return [Later] that
 * - on success: resolves the saved object as it was cached
 * - on failure: resolves with a null
 */
inline fun  Cache.load(key: String) = try {
    load(key, serializer())
} catch (e: Throwable) {
    FailedLater(CacheLoadException(key, cause = e))
}

/**
 * Load object [T] from the [Cache] with a [key] and an optional [serializer]
 *
 * @see [Cache.load]
 *
 * @return a [Later] that
 * - on success: resolves the saved object as it was cached
 * - on failure: resolves with a null
 */
inline fun  Cache.loadOrNull(
    key: String, serializer: KSerializer? = null
) = try {
    load(key, serializer ?: serializer()) as Later
} catch (e: Throwable) {
    FailedLater(CacheLoadException(key, cause = e)) as Later
}.catch { null }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy