react-native-cacheable-image icon indicating copy to clipboard operation
react-native-cacheable-image copied to clipboard

How to clean up all the cache image

Open luojinghui opened this issue 8 years ago • 6 comments

in code, How to clean up all the cache image?

luojinghui avatar Dec 09 '16 10:12 luojinghui

That's a good point. Function to remove all cached images would be very useful.

abelaska avatar Dec 12 '16 13:12 abelaska

Agreed. There should be a mechanism to remove the cached file. I think a static function for removing all cached images would work. Something like CacheableImage.clearCache();

For individual components I am thinking a property like 'clearCacheOnDestroy' as a boolean. Once set, on componentWillUnmount() it should check to see if the prop is set and if so remove it from the cache.

Thoughts ?

jayesbe avatar Dec 12 '16 14:12 jayesbe

So, now only manually to delete the cache, and can not function to delete these pictures?

luojinghui avatar Dec 14 '16 04:12 luojinghui

I was looking at the source code and it actually has a method that deletes the cached file (_deleteFilePath, here it is in use).

This method argument is filePath, which is calculated here at line 52, and uses cachePath and cacheKeys as variables.

Both variables are assigned in _processSource method.

So what would I do in order to delete a specific image from the cache:

  • Create another method to calculate cacheKey (since cachePath is essentially source's URI host). Of course use this method in _processSource.
  • Create a static method, let's say: clearCachedFile which would take a string (the URI), calculate host and cacheKey, then creates filePath variable and finally call _deleteFilePath with this variable.

@jayesbe What do you think? If is there something I'm missing let me know.

If this is fine for you I'd happily create a PR with this.

wachunei avatar Jan 22 '17 12:01 wachunei

@wachunei This makes sense to me. PR is definitely welcome.

jayesbe avatar Jan 30 '17 00:01 jayesbe

@jayesbe will look into this next days!

wachunei avatar Jan 30 '17 01:01 wachunei