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

commonMain.com.seiko.imageloader.intercept.MemoryCacheInterceptor.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
package com.seiko.imageloader.intercept

import com.seiko.imageloader.cache.memory.MemoryCache
import com.seiko.imageloader.cache.memory.MemoryKey
import com.seiko.imageloader.cache.memory.MemoryValue
import com.seiko.imageloader.request.ComposeImageResult
import com.seiko.imageloader.request.ImageResult
import io.github.aakira.napier.Napier

class MemoryCacheInterceptor(
    private val memoryCache: Lazy,
) : Interceptor {
    override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
        val (request, options, components) = chain

        val data = request.data
        val cacheKey = components.key(data, options) ?: return chain.proceed(request)

        val memoryCache = memoryCache.value
        val memoryCacheKey = MemoryKey(cacheKey)
        val memoryCacheValue = memoryCache[memoryCacheKey]
        if (memoryCacheValue != null) {
            Napier.d(tag = "Interceptor") { "read memory cache, data:$data" }
            return ComposeImageResult(
                request = request,
                image = memoryCacheValue.image,
            )
        }

        val result = chain.proceed(request)
        when (result) {
            is ComposeImageResult -> {
                memoryCache[memoryCacheKey] = MemoryValue(
                    image = result.image,
                )
            }
            else -> Unit
        }
        return result
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy