0

Is it possible to do the above?

My scenario is weather graphics with URLs that remain the same, while the underlying image actually changes. Here are the cases I want to cover: - Inside the same session of my app (typically 2-5min), I never want to reload the images from the web - After 15 minutes or so, the image has likely changed, and thus even if I have a cached version, I want to dump it. - When trying to reload images WHILE OFFLINE, any image (including old) is better than no image, so I want to show it from a disc cache.

Is this setup possible? It didn't seem immediately obvious if its feasible with UIL.

Thanks for the great library!

RealCasually
  • 3,593
  • 3
  • 27
  • 34

1 Answers1

2

I think this is solution for you:

File cacheDir = StorageUtils.getCacheDirectory(context); // or any other folder
MemoryCacheAware<String, Bitmap> memoryCacheCore 
          = new LruMemoryCache(4 * 1024 * 1024); // or any other implementation

MemoryCacheAware<String, Bitmap> memoryCache 
          = new LimitedAgeMemoryCache<String, Bitmap>(memoryCacheCore, 15 * 60);
DiscCacheAware discCache = new LimitedAgeDiscCache(cacheDir, 15 * 60);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .memoryCache(memoryCache)
        .discCache(discCache)
        ...
        .build();

UPD: UIL always search needed image in memory cache at first. Then UIL search it in disc cache. And then it downloads image from network.

If you use "limited age" memory cache or disc cache then bitmap or image file will be deleted from cache after timeout (actually they will be deleted during search in cache). Logic is following:

  1. Search bitmap in memory cache
    • needed bitmap is there
      • bitmap was added to cache more than specified time ago
        • delete it from memory cache, go to step 2
      • bitmap was added to cache recently
        • get the bitmap, display it. End.
    • no needed bitmap in cache, go to step 2
  2. Search image file in disc cache
    • needed image is there
      • image was added to cache more than specified time ago
        • delete it from disc cache, go to step 3
      • image was added to cache recently
        • decode image to bitmap, display it. End.
    • no needed image in cache, go to step 3
  3. Download image

Don't forget enable caching (in display options, DisplayImageOptions).

nostra13
  • 12,377
  • 3
  • 33
  • 43
  • Thanks for this answer, could you provide some explanation? Does the memory cache fallback to the disc cache? When would the network get used? As stated in the question, the first attempt should be from the network except if it is in the memory (time limited) cache. If the network fails, and its not in the memory cache, then it should fall back to the disc cache. Thanks! – RealCasually Mar 25 '13 at 21:38
  • @DerekJohnson Added explanation to the answer. – nostra13 Mar 27 '13 at 20:53
  • Thanks for a great explanation. This does not solve the issue as I only want to load from disc cache if the network fails (and assuming its not in the memory cache). How can I reverse the order of those? Memory -> Network -> Disc? – RealCasually Mar 28 '13 at 07:28
  • You can create own implementation of disc cache which will check networks connection and decide whether cached image should be used or deleted (and re-downloaded). – nostra13 Mar 28 '13 at 11:59