Changing when cache refreshes happen #147
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the change?
Previously, refreshing of cache values could occur in the current thread if:
The new behaviour, from changes to
CacheStack
, mean that refreshing of cache values only occur in the current thread if: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.