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

Changing when cache refreshes happen #147

Merged
merged 2 commits into from
Mar 2, 2021
Merged

Conversation

Turnerj
Copy link
Member

@Turnerj Turnerj commented Feb 10, 2021

What is the change?

Previously, refreshing of cache values could occur in the current thread if:

  • There is no existing cache value; OR
  • There is an existing value AND it has expired AND there is no local lock on the cache key

The new behaviour, from changes to CacheStack, mean that refreshing of cache values only occur in the current thread if:

  • There is no existing cache value; OR
  • There is an existing value AND it has expired

When refreshes occur in the background, the cache can only return what it already has.

Why change this behaviour?

The main reason for this behaviour change is so calling GetOrSetAsync will never return an expired cache item. The catch with the previous requirement for "no local lock on the cache key" would mean that under some circumstances, the cache would intentionally return an expired entry (truly expired, not just stale) if it was in the process of refreshing. The change means that the current thread will wait for the refreshed value if the cache entry has expired.

Additionally, as part of this change, there was also a race condition between checking the cache for an existing item and refreshing the cache entry. There are no specific locks for fetching cache items however there are during refreshes. If the code detected no existing items in the cache, it would start the cache refreshing process. The refreshing process itself wasn't checking if a value already existed and whether it even needed to refresh. This change then prevents unnecessary refreshes occurring when they didn't need to.

@Turnerj Turnerj added the enhancement New feature or request label Feb 10, 2021
@Turnerj Turnerj force-pushed the value-refresh-updates branch from 936cc00 to 1cedac6 Compare February 10, 2021 06:16
@Turnerj Turnerj force-pushed the value-refresh-updates branch from 1cedac6 to 67deced Compare March 2, 2021 12:37
@Turnerj Turnerj merged commit bc7d2c7 into master Mar 2, 2021
@Turnerj Turnerj deleted the value-refresh-updates branch March 2, 2021 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant