/// <summary> /// Get a master key from the cache. If it is missing, re-populate the cache with the provided source /// </summary> public static async Task <T> GetOrLoadAsync <T>(this IRedisExpirableStore <T> cache, string key, Func <Task <T[]> > getAllItems) { var cachedItem = await cache.GetAsync(key); if (!Equals(cachedItem, default(T))) { return(cachedItem); } var allItems = (await getAllItems()).ToArray(); await cache.SetAsync(allItems); return(allItems.FirstOrDefault(item => key.Equals(cache.ExtractMasterKey(item)))); }
/// <summary> /// Get some master keys from the cache. If any of them is missing, re-populate the cache with the provided source /// </summary> public static async Task <IEnumerable <T> > GetOrLoadAsync <T>(this IRedisExpirableStore <T> cache, IEnumerable <string> keys, Func <Task <T[]> > getAllItems) { var expectedKeys = keys.ToArray(); var cachedItems = await cache.GetAsync(expectedKeys); if (cachedItems.Length == expectedKeys.Length) { return(cachedItems); } // some values are missing in the cache, repopulate with all items var allItems = (await getAllItems()).ToArray(); await cache.SetAsync(allItems); return(allItems.Where(item => expectedKeys.Contains(cache.ExtractMasterKey(item)))); }