kean/DFImageManager

Manually add/remove image to/from the cache

Closed this issue · 2 comments

Hi kean,

I have a use case where I need to manually remove one specific image from the cache (both memory and disk), and replace it with a new image.

I tried the following with no success:

DFCachedImageResponse *cachedImageResponse = [[DFCachedImageResponse alloc] initWithImage:image info:nil expirationDate:CACurrentMediaTime() + 1440.0f];
DFImageCache *cache = [DFImageCache new];
[cache storeImageResponse:cachedImageResponse forKey:[photoURL absoluteString]];

Running the above code raises a NSInvalidArgumentException:

-[DFImageRequest owner]: unrecognized selector sent to instance 0x17065e090
at DFImageManagerImageLoader.m line 114 (isEqual)
if (other.owner != _owner) {

I haven't found a clear/valid way on how accomplish this, could you point me in the right direction?

Thanks in advance,
Juan

kean commented

Hi @juanchoc,

There is no way to replace cached entries. Cache is merely a transient representation of the image from the original source (in most cases server).

There are two cache "layers":

  • The DFImageManager class provides a top level memory cache for decoded and processed images (see DFImageCaching protocol and DFImageCache class). More info
  • The class that conforms to DFImageFetching protocol (and fetches original image data) might provide it's own memory and/or persistent cache. For instance DFURLImageFetcher uses NSURLCache for that purposes. More info. And in some cases image are simply stored locally (see DFPhotosKitImageFetcher), there is no "cache".

And there are two scenarios:

  1. If your images are stored on the server than you should replace original images on the server. After your replace them on the server DFImageManager would automatically replace cache entries. DFImageManager supports HTTP cache revalidation and it also has some built-in features for memory cache revalidation.
  2. If you find a need to replace images manually from the application then you should have your own persistent storage (which is not cache). This is very application-specific. Then you might still use DFImageManager to fetch, decode, process, cache (mem cache) and display those images: you could either use NSURL with file:// scheme (local files) to access image data or implement your own image fetcher (see DFImageFetching protocol) that would have access to your persistent storage.

I tried the following with no success:

See above. DFImageCache is a memory cache that is used as a top-level cache by DFImageManager which generates special cache keys that are able to compare image requests (DFImageRequest). You can't access them directly.

kean commented

Closing this task. If you have more general questions, please use Stack Overflow. I'm subscribed for dfimagemanager tag.