public static T Get <T>(this IDistributedCache source, string key, TimeSpan time, Func <T> fetch) where T : class { if (source.Exists(key)) { return(source.Get <T>(key)); } var result = fetch(); if (result != null) { var stringToPersist = Newtonsoft.Json.JsonConvert.SerializeObject(result); var data = Encoding.UTF8.GetBytes(stringToPersist); var options = new DistributedCacheEntryOptions().SetSlidingExpiration(time); source.Set(key, data, options); } return(result); }