I’m using Guava in my Android application for image loading and caching theyr filenames.
Here is my cache:
private static final LoadingCache<String, String> imageCache = CacheBuilder.newBuilder()
.softValues()
.initialCapacity(3000)
.maximumSize(3000)
.concurrencyLevel(12)
.expireAfterAccess(IMAGE_EXPIRATION_TIMEOUT, TimeUnit.SECONDS)
.build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
Log.d(TAG, "load " + key);
Bitmap bitmap = null;
final File imageFile = new File(cacheDir, "http---com-jWs-jpg");
return imageFile.getPath();
}
});
And using:
String filename = imageCache.get(imageUrl);
Log.e(TAG, ">>> i:cache size :"+ imageCache.size() +":"+ imageCache.stats() +":"+ imageCache.asMap());
return Drawable.createFromPath(filename);
My problem is: there is 12 unique URLs in my list, but I have missCount count too large:
i:cache size :6:CacheStats{hitCount=36, missCount=48, loadSuccessCount=48, loadExceptionCount=0, totalLoadTime=46569827...)
When I’m returning simple string (like file path or simply empty string), I have only 12 misses, and other get are the hits. What I’m doing wrong?
Okay, got it.
.softValues()was the cause, my values was COLLECTED by garbage collector. When I commented this, everything now works fine.