Ejemplo n.º 1
0
        /// <summary>
        ///     Merges the provided descriptions with those found in cache
        /// </summary>
        /// <param name="culture">A <see cref="CultureInfo" /> specifying the language of the <code>descriptions</code></param>
        /// <param name="description">A <see cref="MarketDescriptionDTO" /> containing market description in specified language</param>
        private void Merge(CultureInfo culture, MarketDescriptionDTO description)
        {
            Contract.Requires(culture != null);
            Contract.Requires(description != null);

            if (_isDisposed)
            {
                return;
            }

            try
            {
                _semaphoreCacheMerge.Wait();
                var cachedItem = _cache.GetCacheItem(GetCacheKey(description.Id, description.Variant));
                if (cachedItem == null)
                {
                    cachedItem = new CacheItem(GetCacheKey(description.Id, description.Variant),
                                               MarketDescriptionCacheItem.Build(description, _mappingValidatorFactory, culture, CacheName));
                    _cache.Add(cachedItem, _cacheItemPolicy);
                }
                else
                {
                    ((MarketDescriptionCacheItem)cachedItem.Value).Merge(description, culture);
                }
            }
            catch (Exception e)
            {
                if (!(e is InvalidOperationException))
                {
                    throw;
                }
                ExecutionLog.Warn("Mapping validation for MarketDescriptionCacheItem failed.", e);
            }
            finally
            {
                if (!_isDisposed)
                {
                    _semaphoreCacheMerge.Release();
                }
            }
        }
        /// <summary>
        /// Merges the provided descriptions with those found in cache
        /// </summary>
        /// <param name="culture">A <see cref="CultureInfo"/> specifying the language of the <code>descriptions</code></param>
        /// <param name="description">A <see cref="MarketDescriptionDTO"/> containing market description in specified language</param>
        private async Task MergeAsync(CultureInfo culture, MarketDescriptionDTO description)
        {
            Guard.Argument(culture, nameof(culture)).NotNull();
            Guard.Argument(description, nameof(description)).NotNull();

            if (_isDisposed)
            {
                return;
            }

            try
            {
                await _semaphoreCacheMerge.WaitAsync().ConfigureAwait(false);

                var cacheId    = GetCacheKey(description.Id, description.Variant);
                var cachedItem = _cache.GetCacheItem(cacheId);
                if (cachedItem == null)
                {
                    cachedItem = new CacheItem(cacheId, MarketDescriptionCacheItem.Build(description, _mappingValidatorFactory, culture, CacheName));
                    _cache.Add(cachedItem, new CacheItemPolicy {
                        SlidingExpiration = OperationManager.VariantMarketDescriptionCacheTimeout
                    });
                }
                else
                {
                    ((MarketDescriptionCacheItem)cachedItem.Value).Merge(description, culture);
                }
            }
            catch (Exception e)
            {
                if (!(e is InvalidOperationException))
                {
                    throw;
                }
                ExecutionLog.LogWarning(e, "Mapping validation for MarketDescriptionCacheItem failed.");
            }
            finally
            {
                _semaphoreCacheMerge.ReleaseSafe();
            }
        }
        /// <summary>
        /// Merges the provided descriptions with those found in cache
        /// </summary>
        /// <param name="culture">A <see cref="CultureInfo"/> specifying the language of the <code>descriptions</code></param>
        /// <param name="descriptions">A <see cref="IEnumerable{MarketDescriptionDTO}"/> containing market descriptions in specified language</param>
        private void Merge(CultureInfo culture, IEnumerable <MarketDescriptionDTO> descriptions)
        {
            Guard.Argument(culture, nameof(culture)).NotNull();
            Guard.Argument(descriptions, nameof(descriptions)).NotNull().NotEmpty();

            var descriptionList = descriptions as List <MarketDescriptionDTO> ?? descriptions.ToList();

            try
            {
                _semaphoreCacheMerge.Wait();
                foreach (var marketDescription in descriptionList)
                {
                    try
                    {
                        var cachedItem = _cache.GetCacheItem(marketDescription.Id.ToString());
                        if (cachedItem == null)
                        {
                            cachedItem = new CacheItem(marketDescription.Id.ToString(), MarketDescriptionCacheItem.Build(marketDescription, _mappingValidatorFactory, culture, CacheName));
                            _cache.Add(cachedItem, _cacheItemPolicy);
                        }
                        else
                        {
                            ((MarketDescriptionCacheItem)cachedItem.Value).Merge(marketDescription, culture);
                        }
                    }
                    catch (Exception e)
                    {
                        if (!(e is InvalidOperationException))
                        {
                            throw;
                        }

                        ExecutionLog.LogWarning($"Mapping validation for MarketDescriptionCacheItem failed. Id={marketDescription.Id}", e);
                    }
                }
                _fetchedLanguages.Add(culture);
            }
            finally
            {
                if (!_isDisposed)
                {
                    _semaphoreCacheMerge.Release();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Merges the provided descriptions with those found in cache
        /// </summary>
        /// <param name="culture">A <see cref="CultureInfo" /> specifying the language of the <code>descriptions</code></param>
        /// <param name="descriptions">
        ///     A <see cref="IEnumerable{MarketDescriptionDTO}" /> containing market descriptions in
        ///     specified language
        /// </param>
        private void Merge(CultureInfo culture, IEnumerable <MarketDescriptionDTO> descriptions)
        {
            Contract.Requires(culture != null);
            Contract.Requires(descriptions != null && descriptions.Any());

            var descriptionList = descriptions as List <MarketDescriptionDTO> ?? descriptions.ToList();

            try
            {
                _semaphoreCacheMerge.Wait();
                foreach (var marketDescription in descriptionList)
                {
                    try
                    {
                        var cachedItem = _cache.GetCacheItem(marketDescription.Id.ToString());
                        if (cachedItem == null)
                        {
                            cachedItem = new CacheItem(marketDescription.Id.ToString(),
                                                       MarketDescriptionCacheItem.Build(marketDescription, _mappingValidatorFactory, culture,
                                                                                        CacheName));
                            _cache.Add(cachedItem, _cacheItemPolicy);
                        }
                        else
                        {
                            ((MarketDescriptionCacheItem)cachedItem.Value).Merge(marketDescription, culture);
                        }
                    }
                    catch (Exception e)
                    {
                        if (!(e is InvalidOperationException))
                        {
                            throw;
                        }

                        ExecutionLog.Warn(
                            $"Mapping validation for MarketDescriptionCacheItem failed. Id={marketDescription.Id}", e);
                    }
                }

                _fetchedLanguages.Add(culture);
            }
            finally
            {
                if (!_isDisposed)
                {
                    _semaphoreCacheMerge.Release();
                }
            }

            //var c = _cache.Count();
            //ExecutionLog.Debug($"InvariantMarketDescriptionCache count: {c}.");
            //foreach (var keyValue in _cache.Where(s=>s.Key != null))
            //{
            //    var ci = (MarketDescriptionCacheItem)keyValue.Value;
            //    if (ci.Mappings != null)
            //    {
            //        foreach (var mapping in ci.Mappings)
            //        {
            //            if (mapping.OutcomeMappings != null)
            //            {
            //                foreach (var outcomeMapping in mapping.OutcomeMappings)
            //                {
            //                    if (outcomeMapping.ProducerOutcomeNames.Count != ci.FetchedLanguages.Count)
            //                    {
            //                        ExecutionLog.Error($"Market {ci.Id}: problem with outcome mapping {outcomeMapping.OutcomeId} and mapped marketId {outcomeMapping.MarketId}");
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            //var cacheItem = _cache.First();
        }