I am trying to ensure that (once a cache item is populated) even if it's refresh object takes a long time to retrieve, the original cache item can be requested.
using System; using System.Runtime.Caching; namespace LongCacheTest { class Program { static void Main(string[] args) { Cacher.Add("key", "object", Get); Console.ReadLine(); } public static Object Get() { // load big file from disck return "stuff"; } } public class Cacheable { public delegate Object CacheEntryGet(); public Object Obj { get; set; } public CacheEntryGet Getter { get; set; } } public class Cacher { private static MemoryCache _cache = null; private static MemoryCache Cache => _cache = _cache ?? new MemoryCache("long"); private static CacheItemPolicy Policy => new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(1)), Priority = CacheItemPriority.Default, RemovedCallback = DidRemove }; public static void DidRemove(CacheEntryRemovedArguments args) { Cache.Add(args.CacheItem.Key, args.CacheItem.Value, Policy); var item = args.CacheItem.Value as Cacheable; item.Obj = item.Getter(); Cache.Remove(args.CacheItem.Key); Cache.Add(args.CacheItem.Key, item, Policy); } public static void Add(string key, Object obj, Cacheable.CacheEntryGet getter) { var item = new Cacheable{ Obj = obj, Getter = getter }; Cache.Add(key, item, Policy); } } } The practice here is to provide a method Getter and cache it alongside the cached Object. When the cache item expires, it is reinserted and the Getter method is called to get the refresh object. Once the fresh object is retrieved it replaces the original cache item.