public async Task <ProductAssociationSearchResult> SearchProductAssociationsAsync(ProductAssociationSearchCriteria criteria) { if (criteria == null) { throw new ArgumentNullException(nameof(criteria)); } var cacheKey = CacheKey.With(GetType(), "SearchProductAssociationsAsync", criteria.GetCacheKey()); return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(AssociationSearchCacheRegion.CreateChangeToken()); var result = AbstractTypeFactory <ProductAssociationSearchResult> .TryCreateInstance(); if (!criteria.ObjectIds.IsNullOrEmpty()) { using (var repository = _catalogRepositoryFactory()) { //Optimize performance and CPU usage repository.DisableChangesTracking(); var dbResult = await repository.SearchAssociations(criteria); result.TotalCount = dbResult.TotalCount; result.Results = dbResult.Results .Select(x => x.ToModel(AbstractTypeFactory <ProductAssociation> .TryCreateInstance())).ToList(); } } return result; })); }
protected virtual Task LoadProductsAssociationsAsync(IEnumerable <Product> products, WorkContext workContext) { if (products == null) { throw new ArgumentNullException(nameof(products)); } foreach (var product in products) { //Associations product.Associations = new MutablePagedList <ProductAssociation>((pageNumber, pageSize, sortInfos, @params) => { var criteria = new ProductAssociationSearchCriteria { PageNumber = pageNumber, PageSize = pageSize, ProductId = product.Id, ResponseGroup = ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.Inventory | ItemResponseGroup.ItemWithVendor }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } if (@params != null) { criteria.CopyFrom(@params); } var cacheKey = CacheKey.With(GetType(), "SearchProductAssociations", criteria.GetCacheKey()); var searchResult = _memoryCache.GetOrCreateExclusive(cacheKey, cacheEntry => { cacheEntry.AddExpirationToken(CatalogCacheRegion.CreateChangeToken()); cacheEntry.AddExpirationToken(_apiChangesWatcher.CreateChangeToken()); return(_productsApi.SearchProductAssociations(criteria.ToProductAssociationSearchCriteriaDto())); }); //Load products for resulting associations var associatedProducts = GetProductsAsync(searchResult.Results.Select(x => x.AssociatedObjectId).ToArray(), criteria.ResponseGroup).GetAwaiter().GetResult(); var result = new List <ProductAssociation>(); foreach (var associationDto in searchResult.Results) { var productAssociation = associationDto.ToProductAssociation(); productAssociation.Product = associatedProducts.FirstOrDefault(x => x.Id.EqualsInvariant(productAssociation.ProductId)); result.Add(productAssociation); } return(new StaticPagedList <ProductAssociation>(result, pageNumber, pageSize, searchResult.TotalCount ?? 0)); }, 1, ProductSearchCriteria.DefaultPageSize); } return(Task.CompletedTask); }