【发布时间】:2019-08-29 02:06:13
【问题描述】:
我可能只是对LruCache 应该如何工作感到困惑,但它不允许从一个实例访问保存在另一个实例上的对象吗?当然不是这样,否则它有点违背了拥有缓存的目的。
例子:
class CacheInterface {
private val lruCache: LruCache<String, Bitmap>
init {
val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt()
// Use 1/8th of the available memory for this memory cache.
val cacheSize = maxMemory / 8
lruCache = object : LruCache<String, Bitmap>(cacheSize) {
override fun sizeOf(key: String, value: Bitmap): Int {
return value.byteCount / 1024
}
}
}
fun getBitmap(key: String): Bitmap? {
return lruCache.get(key)
}
fun storeBitmap(key: String, bitmap: Bitmap) {
lruCache.put(key, bitmap)
Utils.log(lruCache.get(key))
}
}
val bitmap = getBitmal()
val instance1 = CacheInterface()
instance1.storeBitmap("key1", bitmap)
log(instance1.getBitmap("key1")) //android.graphics.Bitmap@6854e91
log(CacheInterface().getBitmap("key1")) //null
据我了解,缓存会一直存储,直到用户删除(手动或卸载应用程序),或者当超过允许的空间时被系统清除。我错过了什么?
【问题讨论】:
标签: android caching android-lru-cache