Guava File Caching

I am using Guava in an Android application to download and cache .rs file names.

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: there are 12 unique URLs on my list, but I have too missCount:

 i:cache size :6:CacheStats{hitCount=36, missCount=48, loadSuccessCount=48, loadExceptionCount=0, totalLoadTime=46569827...) 

When I return a simple line (for example, the path to a file or just an empty line), I have only 12 misses, and the other get are hits. What am I doing wrong?

+4
source share
1 answer

Ok, got it.

.softValues() was the reason my values ​​were COLLECTED by the garbage collector. When I commented on this, everything now works fine.

+4
source

Source: https://habr.com/ru/post/1401530/


All Articles