react-native-cached-image
                                
                                 react-native-cached-image copied to clipboard
                                
                                    react-native-cached-image copied to clipboard
                            
                            
                            
                        Keeping the cache from growing forever
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.
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
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?
Hey, have you maybe found out the solution?
Not really. We ended up not using this package, instead maintaining and pruning our own cache.
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.
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...
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.
Thanks @jonahgreenthal that's VERY helpful!!