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

Keeping the cache from growing forever

Open jonahgreenthal opened this issue 6 years ago • 8 comments

I want to make sure my cache doesn't take up too much space on the user's device.

I'm pretty flexible on how to do this — I'd be fine with automatically expiring images after a given amount of time, limiting the cache to a particular total size, etc.

Does the ttl prop do what I want? That is: if I specify a ttl, can I be reasonably confident that the image will be removed from storage (and the disk space freed) after that amount of time?

If not, is there another way to accomplish this? I'm hoping to avoid having to get into background processes, since it seems like this problem might be solved already.

Thanks! And if there's a different place I should be asking, please let me know.

jonahgreenthal avatar Jan 23 '19 16:01 jonahgreenthal

Yes I believe so.

As stated in the docs;

ttl: PropTypes.number: //the number of seconds each url will stay in the cache. default 2 weeks

arik-gamerlink avatar Feb 05 '19 01:02 arik-gamerlink

Thanks; yes, I read the docs, but I'm unsure if ttl is a minimum, maximum, exact guarantee, or what. When you say "I believe so", are you stating something that you think based on having also read the docs, or are you saying that you have tested this or otherwise can guarantee that that's the behavior?

jonahgreenthal avatar Feb 05 '19 02:02 jonahgreenthal

Hey, have you maybe found out the solution?

fpintaric avatar Jun 28 '19 15:06 fpintaric

Not really. We ended up not using this package, instead maintaining and pruning our own cache.

jonahgreenthal avatar Jun 28 '19 15:06 jonahgreenthal

Thanks for the answer. Currently facing the challenge of having to render multiple images on every app opening, and the more I look at the offered solutions, the more I think we're going to have to write it on our own.

fpintaric avatar Jun 28 '19 15:06 fpintaric

hi @jonahgreenthal thanks for sharing updates with the community, have you published your solution as open source library or shared it anywhere? actually, we're stuck at the same problem and we'd like to get advice regarding that and how we should proceed and why you preferred to implemented your solution, is it only the caching size problem? what about you know clearing the caching after it gets certain limit, happy to hear more from you...

engmonsh avatar Jun 25 '20 18:06 engmonsh

It's not published, but here's the gist:

  • Write a dead simple cache class. We use react-native-fs.
  • Create a function that goes through the cache directory and deletes files older than some threshold
  • Run that function however often you like. We have it run when the app is started; you could do started-or-foregrounded, or something like that, if you want it to be more often. Or, presumably, set a timeout. I don't know a ton about RN background task execution. We don't really care how often it runs; "occasionally" is fine.

jonahgreenthal avatar Jun 25 '20 18:06 jonahgreenthal

Thanks @jonahgreenthal that's VERY helpful!!

engmonsh avatar Jun 28 '20 11:06 engmonsh