Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
740 stars 429 forks source link

Question with maxAge with DefaultCacheManager().putFile() #270

Closed Mayb3Nots closed 3 years ago

Mayb3Nots commented 3 years ago

So I want to cache my user's profile image. And when after a period, let's say 1 day, I want the cache to delete itself. I've been looking at the maxAge property in DefaultCacheManager().putFile(). Please note that I don't put a valid URL in the URL property, instead, i place a reference of my Firebase storage image location.

However according to the docs, When [maxAge] is passed and the eTag is not set the file will always be downloaded again. I don't want it to download again since it is not a valid URL, I just want it to delete itself. Then in post #139 Cached files are only deleted when they aren't used for some time (the maxAgeCacheObject) or when there are too many files in the cache (sorted on oldest by touched). is the maxAgeCacheObject referring to the maxAge property in putFile()? If so I have tested that after the FileInfo.validTill is passed the cache is still there and doesn't get removed.

So now I'm confused.

renefloor commented 3 years ago

This sentence from the doc is not really correct:

When [maxAge] is passed and the eTag is not set the file will always be downloaded again.

The file is downloaded again after maxAge is passed so to say, the maxAge indicates whether or not the file is still assumed to be valid. The eTag can be used to check on the server if the file is changed. As that cannot be done the file will always be downloaded when used after maxAge.

The maxAgeCacheObject is the one you are looking for, but this is a generic setting for all files. When a file is older than maxAgeCacheObject the file is deleted from the cache. So maxAgeCacheObject and maxAge are related, but referring to different properties and different behaviours. (I know the docs need to be improved on this.)

Mayb3Nots commented 3 years ago

@renefloor Currently I just check if getFileFromCache contains the current reference instead of a URL then if the FileInfo is null I download the data from firebase using the ref else if the object is not null I check the .validTill and check if it isAfter the current DateTime if it is then downloaded, remove the current file and put it in again. Because I tried to overwrite it and it doesn't seem to work. Is this a valid workaround for my requirements? Thank you for your time.

renefloor commented 3 years ago

That sounds like you do a lot of logic that can be done by the cache manager already, but it can work indeed.

Mayb3Nots commented 3 years ago

@renefloor Really? It can be done using cache manager? Care to give an example? Cause I put the reference instead of a real URL so downloading it again won't help.

renefloor commented 3 years ago

You can use this package https://pub.dev/packages/flutter_cache_manager_firebase is you use gs:// urls. Than the cache manager can redownload the files for you

mulderpf commented 1 month ago

This sentence from the doc is not really correct:

When [maxAge] is passed and the eTag is not set the file will always be downloaded again.

The file is downloaded again after maxAge is passed so to say, the maxAge indicates whether or not the file is still assumed to be valid. The eTag can be used to check on the server if the file is changed. As that cannot be done the file will always be downloaded when used after maxAge.

The maxAgeCacheObject is the one you are looking for, but this is a generic setting for all files. When a file is older than maxAgeCacheObject the file is deleted from the cache. So maxAgeCacheObject and maxAge are related, but referring to different properties and different behaviours. (I know the docs need to be improved on this.)

I just noticed this and went hunting for the meaning of eTag? Similar to the OP I am caching a file instead of a URL (I made up a URL) but it's in local storage instead of Firestore. The documentation hasn't changed and it suggests that there is no point to setting max age without setting eTag but I cannot find an explanation of what to use this field for. Where can I find out what it means?