How would one add functionality to automatically update the cache when items expire and are evicted? The reason I ask is because I'd like to avoid a cache miss and rather just have it return the expired item; as it updates the value in the background. I can see this update kicked off either directly after noticing a cache miss, or by actively monitoring the next item to expire. Possibly an item with a value populating delegate could be stored in the cache, but this delegate would be called out-of-scope as it's called in a background thread, this would cause problems. So how is it possible to update the cache in the background to avoid a cache miss? I also realize that avoiding a cache miss on a first time call is different that subsequent calls.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@JunTaoLuo Any ideas?
I think you are looking for the PostEvictionCallback
functionality: https://github.com/aspnet/Caching/blob/ee13d634568fc5348e09fc090fab40233b1107cc/src/Microsoft.Extensions.Caching.Abstractions/CacheEntryExtensions.cs#L82-L121. Note that this is only available for IMemoryCache
and not available for IDistributedCache
If you have a cache miss, that may be because the expired or was removed due to memory pressure. If you are anyway going to add it again, why not simply use CacheItemPriority.NeverRemove so that it is not removed ?
Thanks, that is a good suggestion! I also thought of having 2 entries with different expiration dates. But this is better. However I still need a background thread to update the cache. I have found a solution that seems to address the problem. I’m still a little worried about the threading and potential locking issues. I haven’t fully tested this solution, but I did add it to my Stackoverflow post.
From: timemaster67 notifications@github.com
Sent: Tuesday, July 24, 2018 1:57:21 PM
To: aspnet/Docs
Cc: Carlo J. Bos; Author
Subject: Re: [aspnet/Docs] Is is possible to update the cache in the background to avoid cache misses? (#7716)
If you have a cache miss, that may be because the expired or was removed due to memory pressure. If you are anyway going to add it again, why not simply use CacheItemPriority.NeverRemove so that it is not removed ?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/aspnet/Docs/issues/7716#issuecomment-407532000, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJiNBL12rru_gIzeXrYRkf6ZlSv2dK5zks5uJ3whgaJpZM4VZUHh.
fixed in #7792
Is there any suitable replacement for UpdateCallback attribute found in System.Runtime.Caching in Microsoft.Extensions.Caching.Memory? UpdateCallback takes care of updating the cache in the background and during the process of cache update, application threads will continue to serve old cached object.
Most helpful comment
Is there any suitable replacement for UpdateCallback attribute found in System.Runtime.Caching in Microsoft.Extensions.Caching.Memory? UpdateCallback takes care of updating the cache in the background and during the process of cache update, application threads will continue to serve old cached object.