Involvement and raster caching

I am trying to display a list with a lot of (deleted) images. I am trying to use a volley to complete a task.

Volleyball works somewhat, but not good enough. ImageLoader.get volley has the following code snippet:

final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight); // Try to look up the request in the cache of remote images. Bitmap cachedBitmap = mCache.getBitmap(cacheKey); if (cachedBitmap != null) { // Return the cached bitmap. ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null); imageListener.onResponse(container, true); return container; } 

However, getCacheKey creates the following key:

 /** * Creates a cache key for use with the L1 cache. * @param url The URL of the request. * @param maxWidth The max-width of the output. * @param maxHeight The max-height of the output. */ private static String getCacheKey(String url, int maxWidth, int maxHeight) { return new StringBuilder(url.length() + 12).append("#W").append(maxWidth) .append("#H").append(maxHeight).append(url).toString(); } 

i.e. It adds some β€œmetadata”, such as width and height to the key.

This key never causes a hit, and if the image is not in the L1 cache, it is displayed on the Internet. When an image is downloaded online, it is stored in the disk’s cache, but volleyball saves it with a URL (and only a URL) as a key.

Is this the expected behavior? Did I miss something?

+6
source share
6 answers

The reason you don't get any hits is because Volley's default behavior for caching disks depends on the HTTP headers of the element you request (in your case, the image).

How Volley Works:

  • ImageLoader checks the L1 cache for the image (the memory cache provided by ImageLoader in its constructor). If a reverse image is available.
  • The request is processed by RequestQueue . It checks L2 (disk cache) for the image.
  • If you find the disk cache, check the expiration time of the image. If not expired, return.
  • Download the image and return it.
  • Save image in caches.

If you want the default settings to work, the images should have a Cache-Control header, for example max-age=??? , where question marks indicate a sufficient number of seconds from the moment it is downloaded.

If you want to change the default behavior, I'm not sure, but I think you need to change the code a bit.

Look at the CacheDispatcher class in the Volley source.

+9
source

Can you post your class that implements ImageCache.

I just looked at it myself and realized in my code: I did not add a bitmap to the cache when it loaded it from disk, so it will always reload it from disk every time.

This is a simple example of what I mean and where I was wrong

 @Override public Bitmap getBitmap(String cachKey) { Bitmap b = null; //check the memory first b = memoryCache.get(cacheKey); if(b == null){ //memory cache was null, check file cache b = diskLruImageCache.getBitmap(cacheKey); // this is where it needs to be added to your memory cache if(b != null){ memoryCache.put(url, b); } } return b; } 
+1
source

I tracked this issue in my application today. I set the maximum cache size in KB in the constructor, but reported the size in bytes in sizeOf (), so nothing was cached.

This answer set me straight.

+1
source

You may be using NetworkImageView to load images. You can use ImageView and ImageLoader to do the same. Using ImageLoader, the metadata in the key is similar to "# W0 # H0" for any image size.

 ImageLoader imageLoader = getImageLoader(); imageLoader.get(url, ImageLoader.getImageListener(imageView, defaultDrawable, errorDrawable)); 
+1
source

Volleyball will not cache anything unless cache control is specified in the response header.

Check out the implementation of the HttpHeaderParser class in Volley.

Caching can be based on maximum or E-tag. Check the response header and determine everything installed there. It will look something like this.

Cache-Control -> public, max-age = 300

Cache Header Information

0
source

This is the exact way you want it to work.

  • Click on the URL and get the image when it is not available.
  • Download the cache image, if available.
-1
source

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


All Articles