private void AddCompetitor(URN id, CompetitorDTO item, CultureInfo culture, bool useSemaphore) { if (_cache.Contains(id.ToString())) { try { var ci = (CompetitorCI)_cache.Get(id.ToString()); if (useSemaphore) { _semaphoreCacheMerge.Wait(); } ci?.Merge(item, culture); } catch (Exception ex) { ExecutionLog.LogError($"Error adding competitor for id={id}, dto type={item?.GetType().Name} and lang={culture.TwoLetterISOLanguageName}.", ex); } finally { if (useSemaphore && !_isDisposed) { _semaphoreCacheMerge.Release(); } } } else { _cache.Add(id.ToString(), new CompetitorCI(item, culture, _dataRouterManager), GetCorrectCacheItemPolicy(id)); } if (item?.Players != null && item.Players.Any()) { foreach (var player in item.Players) { AddPlayerCompetitor(player, item.Id, culture, false); } } }
private async Task AddCompetitorAsync(URN id, CompetitorDTO item, CultureInfo culture, bool useSemaphore) { if (_cache.Contains(id.ToString())) { try { var ci = (CompetitorCI)_cache.Get(id.ToString()); if (useSemaphore) { await WaitTillIdIsAvailableAsync(_mergeUrns, id).ConfigureAwait(false); } ci?.Merge(item, culture); } catch (Exception ex) { ExecutionLog.LogError(ex, $"Error adding competitor for id={id}, dto type={item?.GetType().Name} and lang={culture.TwoLetterISOLanguageName}."); } finally { if (useSemaphore && !_isDisposed) { await ReleaseIdAsync(_mergeUrns, id).ConfigureAwait(false); } } } else { _cache.Add(id.ToString(), new CompetitorCI(item, culture, _dataRouterManager), GetCorrectCacheItemPolicy(id)); } if (item?.Players != null && item.Players.Any()) { var tasks = item.Players.Select(s => AddPlayerCompetitorAsync(s, item.Id, culture, false)); await Task.WhenAll(tasks).ConfigureAwait(false); } }