我正在缓存图像,以便更快地从网络共享和/或互联网下载。目前,该缓存位于内存中。
是否可以通过对象的深度大小来限制缓存(BufferedImageSize
之前)?
缓存初始化如下
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
....
imageCache = CacheBuilder.newBuilder()
.maximumSize(cacheSize)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(
new CacheLoader<String, DeferredImage>() {
public DeferredImage load(String pathname) throws IOException {
return new DeferredImage(pathname);
}
});
其中 DeferredImage
是 BufferedImage
的包装器,以便于在单独的线程中加载。
是否可以编写一些大小检查代码,该检查不考虑缓存中的条目数量,而是考虑它们的字节大小?
更新
我找到了maximumWeight() method但不明白这是否是我正在寻找的:文档中没有任何地方说明权重的总和是计算出来的。
最佳答案
简短描述位于Caches Explained - size based eviction :
Alternately, if different cache entries have different "weights" -- for example, if your cache values have radically different memory footprints -- you may specify a weight function with
CacheBuilder.weigher(Weigher)
and a maximum cache weight withCacheBuilder.maximumWeight(long)
. In addition to the same caveats as maximumSize requires, be aware that weights are computed at entry creation time, and are static thereafter.
在你的情况下,它是这样的:
imageCache = CacheBuilder.newBuilder()
.maximumWeight(maximumCacheSizeInBytes)
.weigher(new Weigher<String, DeferredImage>() {
public int weigh(String k, DeferredImage g) {
return g.size();
}
})
.build(
new CacheLoader<String, DeferredImage>() {
public DeferredImage load(String pathname) throws IOException {
return new DeferredImage(pathname);
}
});
值 DeferredImage
的类型可能会导致一些问题,因为将值插入缓存时需要知道其大小。额外的加载线程变得毫无用处,因为插入缓存的线程会阻塞,直到知道大小为止。
也许值得一提的替代方案:
Caffeine 的 API 和功能与 Guava 缓存类似,但支持异步加载器。这将允许更好地控制图像加载线程的数量。
EHCache 通过分析值的内存消耗来提供基于大小的逐出,因此它不需要 Weighter
函数。
关于java - 通过 Java 中对象的深(与浅)大小限制缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42814946/