protected override async Task LoadAsync(AsyncCacheData <TKey, TValue> emptyData, CancellationToken token) { var operationCts = new CancellationTokenSource(); var combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, operationCts.Token, _localCts.Token); var combinedToken = combinedTokenSource.Token; //Keep this collection with 1 element bound. var collection = new BlockingCollection <TMap>(1); var cachePopulatorTask = emptyData.ValueTypeCanBeMerged ? Task.Factory.StartNew(() => PopulateCacheWithMerging(emptyData, collection, combinedToken, operationCts), combinedToken) : Task.Factory.StartNew( () => PopulateCacheWithoutMerging(emptyData, collection, combinedToken, operationCts), combinedToken); try { await _fetcher.FillAsync(collection, combinedToken).ConfigureAwait(false); } finally { collection.CompleteAdding(); using (operationCts) { using (combinedTokenSource) { using (collection) { await cachePopulatorTask.ConfigureAwait(false); } } } } }
protected override async Task LoadAsync(AsyncCacheData <TKey, TValue> emptyData, CancellationToken token) { _localCts.Token.ThrowIfCancellationRequested(); using (var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(_localCts.Token, token)) { combinedCts.Token.ThrowIfCancellationRequested(); try { await LoadFromFileAsync(emptyData, combinedCts.Token).ConfigureAwait(false); } catch (Exception e) { _logger.Error($"({_cacheName}) Error during load from file.", e); throw; } } }