我们可以使用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)
}