public virtual async Task <CatalogProduct[]> GetByIdsAsync(string[] itemIds, string respGroup, string catalogId = null)
        {
            var itemResponseGroup = EnumUtility.SafeParseFlags(respGroup, ItemResponseGroup.ItemLarge);

            var cacheKey = CacheKey.With(GetType(), nameof(GetByIdsAsync), string.Join("-", itemIds), itemResponseGroup.ToString(), catalogId);
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                var products = Array.Empty <CatalogProduct>();

                if (!itemIds.IsNullOrEmpty())
                {
                    using (var repository = _repositoryFactory())
                    {
                        //Optimize performance and CPU usage
                        repository.DisableChangesTracking();

                        //It is so important to generate change tokens for all ids even for not existing objects to prevent an issue
                        //with caching of empty results for non - existing objects that have the infinitive lifetime in the cache
                        //and future unavailability to create objects with these ids.
                        cacheEntry.AddExpirationToken(ItemCacheRegion.CreateChangeToken(itemIds));
                        cacheEntry.AddExpirationToken(CatalogCacheRegion.CreateChangeToken());

                        products = (await repository.GetItemByIdsAsync(itemIds, respGroup))
                                   .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                                   .ToArray();
                    }

                    if (products.Any())
                    {
                        products = products.OrderBy(x => Array.IndexOf(itemIds, x.Id)).ToArray();
                        await LoadDependenciesAsync(products);
                        ApplyInheritanceRules(products);

                        var productsWithVariationsList = products.Concat(products.Where(p => p.Variations != null)
                                                                         .SelectMany(p => p.Variations)).ToArray();

                        // Fill outlines for products and variations
                        if (itemResponseGroup.HasFlag(ItemResponseGroup.Outlines))
                        {
                            _outlineService.FillOutlinesForObjects(productsWithVariationsList, catalogId);
                        }
                        //Add change tokens for products with variations
                        cacheEntry.AddExpirationToken(ItemCacheRegion.CreateChangeToken(productsWithVariationsList));

                        //Reduce details according to response group
                        foreach (var product in productsWithVariationsList)
                        {
                            product.ReduceDetails(itemResponseGroup.ToString());
                        }
                    }
                }

                return(products);
            });

            return(result.Select(x => x.Clone() as CatalogProduct).ToArray());
        }
Exemplo n.º 2
0
        public virtual async Task <CatalogProduct[]> GetByIdsAsync(string[] itemIds, string responseGroup, string catalogId = null)
        {
            var itemResponseGroup = EnumUtility.SafeParseFlags(responseGroup, ItemResponseGroup.ItemLarge);

            var cacheKey = CacheKey.With(GetType(), "GetByIdsAsync", string.Join("-", itemIds), itemResponseGroup.ToString(), catalogId);

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                CatalogProduct[] result;

                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    result = (await repository.GetItemByIdsAsync(itemIds, itemResponseGroup))
                             .Select(x => x.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance()))
                             .ToArray();
                }

                await LoadDependenciesAsync(result);
                ApplyInheritanceRules(result);

                var productsWithVariationsList = result.Concat(result.Where(p => p.Variations != null)
                                                               .SelectMany(p => p.Variations)).ToArray();

                // Fill outlines for products and variations
                if (itemResponseGroup.HasFlag(ItemResponseGroup.Outlines))
                {
                    _outlineService.FillOutlinesForObjects(productsWithVariationsList, catalogId);
                }

                // Fill SEO info for products, variations and outline items
                if (itemResponseGroup.HasFlag(ItemResponseGroup.Seo))
                {
                    var objectsWithSeo = productsWithVariationsList.OfType <ISeoSupport>().ToList();
                    //Load SEO information for all Outline.Items
                    var outlineItems = productsWithVariationsList.Where(p => p.Outlines != null)
                                       .SelectMany(p => p.Outlines.SelectMany(o => o.Items));
                    objectsWithSeo.AddRange(outlineItems);
                    await _seoService.LoadSeoForObjectsAsync(objectsWithSeo.ToArray());
                }

                //Reduce details according to response group
                foreach (var product in productsWithVariationsList)
                {
                    product.ReduceDetails(itemResponseGroup.ToString());
                    cacheEntry.AddExpirationToken(ItemCacheRegion.CreateChangeToken(product));
                }

                return result;
            }));
        }