kfiroo/react-native-cached-image

Is there any way to set expiration policy for cache?

Closed this issue · 10 comments

Cache means NOT PERSISTENT.

Now, I must record the time when an image been cached, and invoke the delete method over a period of time manually. So, I think it could be better to provide an option to set expiration policy.

@fudiwei Hey, I see what you are saying.
My assumption, which might not suit all use cases, was that a url refers to a single resource, so busting the cache is not needed. If the resource changed, a new url will be used and a new cache entry will be created.
We also let the OS manage the cache dir so we don't have to do the cleanup ourselves.

@kfiroo Thank you for your response.

As far as I know, there isn't an effective mechanism to manage the cache dir in Android OS.

Thanks for this library! :)

So, how does the OS manage the cache? I am thinking of using this in my news app so there are lots of images that will possible be cached. Will it bloat the users device?

This is what I could find in the android developer docs https://developer.android.com/guide/topics/data/data-storage.html

When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.

Looks like the OS would clean this folder in order to free up space but I'm not sure how often.

Currently, managing the cache manually feels like a bit too much for this simple component.

@chatras If you could find a heuristic by which to clear your cache, or even specific URLs that are not needed anymore this should not be a problem for you.
We are using a similar method with our app.

@fudiwei @chatras See discussion here #59

Hi I have a similar use-case... basically I'm looking for a way to delete an image from the cache if that image hasn't been accessed in 48 hours.

Currently, I'm loading an image like so (testing with a 60 second ttl):
<CachedImage source={{ uri: post.attachment.original }} ttl={60} />

However when I iterate over the images in the cache using ImageCacheManager().getCacheInfo(), those images still seem to exist after the 60 seconds.

From my understanding, that ttl actually doesn't do anything until we attempt to fetch the same image again, then if the image is 'expired' it does a fresh fetch. What I'd like to do is essentially have either something like deletedExpiredImages on the Manager that would only clear cached images that are expired.

However, the other piece is I'd like to basically refresh the expiration time each time we attempt to fetch the image from the cache if that makes sense. So, back to my first sentence, I'm looking for a way to delete an image from the cache if that image hasn't been accessed in 48 hours.

You mentioned to chatras, "If you could find a heuristic by which to clear your cache, or even specific URLs that are not needed anymore this should not be a problem for you.
We are using a similar method with our app." How would one add such a heuristic using this library?

@jasongaare Hey,
Let's split the issue into 2:

1. Removing stale urls from the cache and their corresponding files from the fs.

We could add a method in ImageCacheManager that would go over all urls in the cache manager and call ImageCacheManager.deleteUrl for each expired url.
I thinks this is quite simple to implement, if you want to create a PR for it

When would you use this method?
What is the motivation behind using it? (saving disc space? making sure urls gets downloaded again?)

2. Using a different cache policy that expires a url that wasn't accessed in some time.

This one is a bit more tricky.
I think the right way to do this is to create your own cache manager that extends MemoryCache and follows the same interface.
This cache manager would save some extra info for each url, such as lastAccessed, and its own isExpired method that could take a look at that extra data.

Let me know if that makes sense or if I can help you in any other way :)

Thanks for the help - sorry for the delay in response. The main motivation here would be to limit network requests for the same image multiple times. I've had better experience with this library than the built-in caching for React Native.

I think implementing my own cache policy will be the way to go, and utilizing deleteUrl

@jasongaare Great! Please let me know how that works for you and if there is anything I could do to help you!