在LruCache中缓存二进制文件

时间:2019-06-28 09:59:47

标签: android android-lru-cache

我们可以使用LruCache缓存动态内容吗?内容可以是二进制格式的任何内容,例如JSON,位图等。我尝试使用InputStream,但无法正常工作。

private val mMemoryCache: LruCache<String, InputStream>

init {
        // Get memory class of this device
        val memClass = (mContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).memoryClass

        // Use 1/8th of the available memory for this memory cache
        mCacheSize = 1024 * 1024 * memClass / 8

        mMemoryCache = LruCache<String, InputStream>(mCacheSize)
}

要存储:

 fun storeToMemory(inputStream: InputStream, cacheKey: String) {
        mMemoryCache.put(cacheKey, inputStream)
    }

要检索:

fun queryCache(cacheKey: String?): InputStream {
        return mMemoryCache.get(cacheKey)  
   }

0 个答案:

没有答案