Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to set expiration policy for cache? #40

Closed
fudiwei opened this issue May 18, 2017 · 10 comments
Closed

Is there any way to set expiration policy for cache? #40

fudiwei opened this issue May 18, 2017 · 10 comments

Comments

@fudiwei
Copy link

fudiwei commented May 18, 2017

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 fudiwei changed the title Is there any way to set expiration policy for caching? Is there any way to set expiration policy for cache? May 18, 2017
@kfiroo
Copy link
Owner

kfiroo commented Jun 5, 2017

@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.

@fudiwei
Copy link
Author

fudiwei commented Jun 6, 2017

@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.

@chatras
Copy link

chatras commented Jun 10, 2017

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?

@kfiroo
Copy link
Owner

kfiroo commented Jun 11, 2017

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.

@kfiroo
Copy link
Owner

kfiroo commented Jul 19, 2017

@fudiwei @chatras See discussion here #59

@jasongaare
Copy link

jasongaare commented Sep 22, 2017

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.

@jasongaare
Copy link

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?

@kfiroo
Copy link
Owner

kfiroo commented Sep 26, 2017

@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 :)

@jasongaare
Copy link

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

@kfiroo
Copy link
Owner

kfiroo commented Oct 5, 2017

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

@fudiwei fudiwei closed this as completed Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants