georstat/react-native-image-cache

Automatically cleanup old image

Closed this issue · 6 comments

namnm commented

Thanks for the library.

Can we have an API expose to cleanup old/unused image
For example if we set maximum storage 1GB, it will delete the old images exceed that storage.
Something like:
removeExceedStorage('1gb')

@namnm I'm implementing it now, this could have a performance impact though.

example flow:

  1. Get all files from cache folder
  2. Sort by last date of modification, newest first
  3. Check the user setting cache limit eg 1GB against the cache folder size
  4. iterate against all files and delete the ones that are over the 1GB threshold

I'll try to run some benchmark tests and see the outcome.

@namnm react-native-file-access does not support lstat (yet?), I 've contacted the author and I'm awaiting for his response.

lstat gets the files of the folder including all metadata eg. size, lastModified and we need them in order to sort and calculate the cached image file size.

Stay tuned and we'll implement it when @alpha0010 release the feature above.

namnm commented

This could be done like using sqlite, but may be too aggresive put it here.

@namnm we would still need to write the image on disk so we should avoid this “double” work, writing/checking sqlite tables and file system.

Solution would be to write image as base64 on sqlite but I’ve never seen such approach on caching image modules (in general, not only in react native).

namnm commented

I think sqlite should only be used for storing image stats, not the image itself.
So we still save the image to disk, but then also store image stats like file size, created time...
Then we can query the images we want to delete/modify in an efficient way.