public T GetCachedData <T>(Func <T> dataGetter, string key, CoordinatedResetCacheTime resetCacheTime = CoordinatedResetCacheTime.OnHour) { return(GetCachedDataAsync(() => Task.FromResult(dataGetter.Invoke()), key, resetCacheTime).Result); }
public async Task <T> GetCachedDataAsync <T>(Func <Task <T> > dataGetter, string key, CoordinatedResetCacheTime resetCacheTime = CoordinatedResetCacheTime.OnHour) { return(await GetCachedDataAsync(token => dataGetter.Invoke(), key, CancellationToken.None, resetCacheTime)); }
public T GetCachedData <T>(Func <CancellationToken, T> dataGetter, string key, CancellationToken cancellationToken, CoordinatedResetCacheTime resetCacheTime = CoordinatedResetCacheTime.OnHour) { return(GetCachedDataAsync(token => Task.FromResult(dataGetter.Invoke(token)), key, cancellationToken, resetCacheTime).Result); }
public async Task <T> GetCachedDataAsync <T>(Func <CancellationToken, Task <T> > dataGetter, string key, CancellationToken cancellationToken, CoordinatedResetCacheTime resetCacheTime = CoordinatedResetCacheTime.OnHour) { TimeSpan timeToReset; switch (resetCacheTime) { case CoordinatedResetCacheTime.OnHour: timeToReset = GetLengthOfTimeUntilNextHour(); break; case CoordinatedResetCacheTime.OnMinute: timeToReset = GetLengthOfTimeUntilNextMinute(); break; } AsyncCachePolicy cachePolicy = Policy.CacheAsync(_pollyMemoryCacheProvider.MemoryCacheProvider, timeToReset); Context context = new Context($"{nameof(CoordinatedResetCache)}_{key}"); // collapser policy used to prevent concurrent calls retrieving the same data twice T result = await _collapserPolicy.WrapAsync(cachePolicy).ExecuteAsync(_ => dataGetter.Invoke(cancellationToken), context); return(result); }