Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Advanced Image Caching Guide

Alexander Grebenyuk edited this page Sep 11, 2015 · 10 revisions

Preliminary: Image Caching Guide

Memory Cache Revalidation

DFImageManager has two separate cache storages (as for URL fetching). The first one is NSURLCache for on-disk caching which handles revalidation transparently (via NSURLSession classes). The second one is NSCache memory storage for decompressed images (instances of UIImage class). NSCache doesn't have any built-in revalidation techniques, however, it automatically removes cached objects when the cache is full and when the application enters background. For most applications that's all the "revalidation" they need, but there might be cases that require a little more control.

NSCache doesn't remove cached objects until the cache is either full or the application enters background. It is possible that the cached object will stay in cache for a very long period of time. However, DFImageManager doesn't use just NSCache for in-memory caching - it uses DFImageCache that adds additional functionality to NSCache. One of the DFImageCache features is the time-based cached objects expiration. You can set the expirationAge property of the DFImageRequestOptions to specify the amount of time to elapse before memory-cached images associated with a request are considered to have expired. For example, you might want to revalidate user avatars very frequently, but don't revalidate some static resources at all.

Another thing you can do is remove all cached objects from the memory cache when the user leaves the current screen. That makes all the sense in many situations, chances are those images are not going to be needed on the other screens anyway.