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

Question: best way to proactively refresh accessed items #912

Closed
tjackowiak opened this issue Apr 16, 2023 · 3 comments
Closed

Question: best way to proactively refresh accessed items #912

tjackowiak opened this issue Apr 16, 2023 · 3 comments

Comments

@tjackowiak
Copy link

tjackowiak commented Apr 16, 2023

I use Caffeine to store responses of a slow API that can take a few seconds to respond.

I understand that Caffeine does not have native support for proactively refreshing items in the refresh window. I want to implement this approach in my code, but I only want to refresh the items accessed at least once after they were added.

What is the suggested way of retrieving information that an item has been accessed after it has been created or updated?

  1. By setting both expireAfterWrite and expireAfterAccess, I can get the write and read time of an item with FixedExpiration.ageOf and see if they are different.
    Is this a good approach? I understand that setting both of these values is not the best and may not be supported in the future Variable refresh #504, [Feature Request] Time-based Refresh #261 (comment)
  2. Currently I am setting expireAfter with custom Expiry policy. I see no way to get this information in this case. Is there any?
  3. Is there any other way not related to the expiration policy?

BTW. Thanks for this great library and your support!

@ben-manes
Copy link
Owner

The cache will try to hold minimal metadata, so if it wasn't required then we'll drop it to save memory. For example the expireAfter only maintains a single duration and not the create / read / update times.

You can always add your own metadata by wrapping your type to add a timestamp or two. You can set those as atomic fields to read and write from, such as on access or write. I suspect this will be easier than trying to use our metadata when it's not a straightforward match and lets you modify the policies if your goals diverge (such as if you want to not use access time for the expiration policy).

Hope that helps!

@tjackowiak
Copy link
Author

Thanks! Yes, this approach makes more sense. I wanted to verify that there was no ready solution that I would just duplicate.

@ben-manes
Copy link
Owner

wonderful, let me know if you have more questions or run into problems. Good luck!

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

2 participants