MayakaApps / Kache

Kache is a lightweight Kotlin Multiplatform caching library that supports both in-memory and persistent caches and supports different eviction strategies (LRU, FIFO, MRU, and FILO).
https://mayakaapps.github.io/Kache/latest
Apache License 2.0
131 stars 6 forks source link

Support a maximum age for cache entries #111

Open sschuberth opened 1 year ago

sschuberth commented 1 year ago

Some cache entities should not be cached forever but expire after a certain amount of time, e.g. to force doing an expensive network request to fetch the original data to ensure it's still valid. For this, I'd propose to add e.g. a var maxAge: Long property to Configuration.

FYI, in our DiskLru wrapper we implemented this like so:

https://github.com/oss-review-toolkit/ort/blob/e484f98f6375d78ce6f7934edf9728863c36ed40/utils/common/src/main/kotlin/DiskCache.kt#L115-L123

MSDarwish2000 commented 1 year ago

Thank you for this suggestion. This seems to be a good feature proposal for v2.1 or v2.2. I think we need to focus more on getting v2.0 polished with the current feature set but surely, let's keep this issue open.

MSDarwish2000 commented 1 year ago

@sschuberth Meanwhile, as a workaround for this limitation, you can use the filesystem's "last modified date" or "created date" as it is more efficient than you're current approach. It is possible as Kache gives you direct access to the file rather than an InputStream. Your current approach is to write the date to a separate file for each entry which seems to be more expensive.

sschuberth commented 9 months ago

From the Kache v2.1.0-beta01 release notes:

Add time-based expiration to InMemoryKache

Wohoo, thanks for implementing this! I plan to give this a try soon!

MSDarwish2000 commented 9 months ago

@sschuberth Thank you for staying interested in this library. I'm looking forward to your feedback to prepare the first stable release of the library after revamping the documentation and testing it more.

I am afraid that this feature is currently limited to the in-memory cache not the persistent cache which is the equivalent for DiskLruCache that you mentioned here.

Adding it to the persistent cache would likely mean refactoring the current implementation of it in the in-memory cache though I expect it to be binary compatible with the current version, so no worries.

For the implementation details:

sschuberth commented 8 months ago

I am afraid that this feature is currently limited to the in-memory cache not the persistent cache which is the equivalent for DiskLruCache that you mentioned here.

Thanks for pointing that out, I indeed initially missed that.

I'd appreciate your suggestions and feedback.

Thanks, but I'm probably too far away from the code base to give meaningful advice.