Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Memory Cache RefreshAfter #36497

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

Closed
jemeric opened this issue Dec 17, 2018 · 2 comments
Closed

Memory Cache RefreshAfter #36497

jemeric opened this issue Dec 17, 2018 · 2 comments
Labels

Comments

@jemeric
Copy link

jemeric commented Dec 17, 2018

Is your feature request related to a problem? Please describe.

A frequent use case for caching is to reduce the number of expensive operations performed during web requests. However, sometimes data will change and to ensure we reach consistency we can expire cache entries after a period of time. The problem with this is that after the entry expires we will have a cache miss and the expensive operation will slow down at least one request (possibly many on a busy server).

Say our cached operation takes 5 seconds to complete, we need it to expire at least once per minute, and our instance is receiving 10 requests per second. Using the conventional expiry means as many as 50 requests per minute may be affected by the cache miss.

Describe the solution you'd like

I propose an alternative to the ExpiryDate, which I refer to as RefreshAfter. You set a short RefreshAfter time interval when you add your cache entry for a cache item that doesn't expire or has a long expiry. The RefreshAfter interval will be used to calculate the time after which the next refresh should occur (NextRefreshTime). When (time created/last updated - current time > NextRefreshTime) then the cache update operation (provided callback?) will be performed asynchronously one time in a thread-safe manner. End users will not be aware of this.

RefreshAfter provides no guarantee the cache will actually be refreshed at the NextRefreshTime, simply that it will be attempted at some point after that time. The refresh happens lazily and refresh rate will vary based on request frequency.

Ideally, in a distributed environment other instances will be aware the cache has been refreshed and push back their NextRefreshTime. Perhaps in a distributed cache each instance could track the time created/last updated and reset their own NextRefreshTime to a later date.

Describe alternatives you've considered

An alternative to this would be an eager approach to refreshing the cache, involving a background timer that would constantly attempt to refresh the cache at an interval. I consider this solution to be less useful as it would:

  • be harder to work into a distributed cache system
  • be harder to maintain/monitor/debug
  • consume more system resources and lead to unnecessary refreshes at times of low traffic

Another alternative would be to re-include the UpdateCallback, but I saw this was intentionally dropped from the project (see below).

Additional context

Cache refreshing solutions have been proposed before:
http://pdalinis.blogspot.com/2013/06/auto-refresh-caching-for-net-using.html
https://stackoverflow.com/questions/44723017/in-memory-caching-with-auto-regeneration-on-asp-net-core
https://www.programmingmusings.com/index.php/2016/09/27/auto-refresh-thread-safe-cache-in-net/

UpdateCallback Request:
aspnet/Caching#31

And has been implemented in a popular library in Java:
https://github.com/google/guava/wiki/CachesExplained#refresh

@analogrelay analogrelay transferred this issue from dotnet/extensions May 15, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-Extensions-Caching untriaged New issue has not been triaged by the area owner labels May 15, 2020
@analogrelay analogrelay added this to the Future milestone May 15, 2020
@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Jul 1, 2020
Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Apr 1, 2025
Copy link
Contributor

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

@dotnet-policy-service dotnet-policy-service bot removed this from the Future milestone Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants