public async Task <IList <MenuLinkList> > LoadAllStoreLinkListsAsync(Store store, Language language)
        {
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }
            var cacheKey = CacheKey.With(GetType(), "LoadAllStoreLinkListsAsync", store.Id, language.CultureName);

            return(await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(StaticContentCacheRegion.CreateChangeToken());
                var result = new List <MenuLinkList>();
                var listsDto = await _cmsApi.GetListsAsync(store.Id);
                if (listsDto != null)
                {
                    result.AddRange(listsDto.Select(x => x.ToMenuLinkList()));
                }

                result = result.GroupBy(x => x.Name).Select(x => x.FindWithLanguage(language)).Where(x => x != null).ToList().ToList();

                var allMenuLinks = result.SelectMany(x => x.MenuLinks).ToList();
                var productLinks = allMenuLinks.OfType <ProductMenuLink>().ToList();
                var categoryLinks = allMenuLinks.OfType <CategoryMenuLink>().ToList();

                Task <Product[]> productsLoadingTask = null;
                Task <Category[]> categoriesLoadingTask = null;

                //Parallel loading associated objects
                var productIds = productLinks.Select(x => x.AssociatedObjectId).ToArray();
                if (productIds.Any())
                {
                    productsLoadingTask = _catalogService.GetProductsAsync(productIds, ItemResponseGroup.ItemSmall);
                }
                var categoriesIds = categoryLinks.Select(x => x.AssociatedObjectId).ToArray();
                if (categoriesIds.Any())
                {
                    categoriesLoadingTask = _catalogService.GetCategoriesAsync(categoriesIds, CategoryResponseGroup.Info | CategoryResponseGroup.WithImages | CategoryResponseGroup.WithSeo | CategoryResponseGroup.WithOutlines);
                }
                //Populate link by associated product
                if (productsLoadingTask != null)
                {
                    var products = await productsLoadingTask;
                    foreach (var productLink in productLinks)
                    {
                        productLink.Product = products.FirstOrDefault(x => x.Id == productLink.AssociatedObjectId);
                    }
                }
                //Populate link by associated category
                if (categoriesLoadingTask != null)
                {
                    var categories = await categoriesLoadingTask;
                    foreach (var categoryLink in categoryLinks)
                    {
                        categoryLink.Category = categories.FirstOrDefault(x => x.Id == categoryLink.AssociatedObjectId);
                    }
                }

                return result.ToList();
            }));
        }
        public IEnumerable <ContentItem> LoadStoreStaticContent(Store store)
        {
            var baseStoreContentPath = string.Concat(_basePath, "/", store.Id);
            var cacheKey             = CacheKey.With(GetType(), "LoadStoreStaticContent", store.Id);

            return(_memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(new CompositeChangeToken(new[] { StaticContentCacheRegion.CreateChangeToken(), _contentBlobProvider.Watch(baseStoreContentPath + "/**/*") }));

                var retVal = new List <ContentItem>();
                const string searchPattern = "*.*";

                if (_contentBlobProvider.PathExists(baseStoreContentPath))
                {
                    // Search files by requested search pattern
                    var contentBlobs = _contentBlobProvider.Search(baseStoreContentPath, searchPattern, true)
                                       .Where(x => _extensions.Any(x.EndsWith))
                                       .Select(x => x.Replace("\\\\", "\\"));

                    foreach (var contentBlob in contentBlobs)
                    {
                        var blobRelativePath = "/" + contentBlob.TrimStart('/');

                        var contentItem = _builder.BuildFrom(baseStoreContentPath, blobRelativePath, GetContent(blobRelativePath));
                        if (contentItem != null)
                        {
                            retVal.Add(contentItem);
                        }
                    }
                }

                return retVal.ToArray();
            }));
        }
Example #3
0
        public IEnumerable <ContentItem> LoadStoreStaticContent(Store store)
        {
            var baseStoreContentPath = string.Concat(_basePath, "/", store.Id);
            var cacheKey             = CacheKey.With(GetType(), "LoadStoreStaticContent", store.Id);

            return(_memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(new CompositeChangeToken(new[] { StaticContentCacheRegion.CreateChangeToken(), _contentBlobProvider.Watch(baseStoreContentPath + "/**/*") }));

                var retVal = new List <ContentItem>();
                const string searchPattern = "*.*";

                if (_contentBlobProvider.PathExists(baseStoreContentPath))
                {
                    // Search files by requested search pattern
                    var contentBlobs = _contentBlobProvider.Search(baseStoreContentPath, searchPattern, true)
                                       .Where(x => _extensions.Any(x.EndsWith))
                                       .Select(x => x.Replace("\\\\", "\\"));

                    // each content file  has a name pattern {name}.{language?}.{ext}
                    var localizedBlobs = contentBlobs.Select(x => new LocalizedBlobInfo(x));

                    foreach (var localizedBlob in localizedBlobs.OrderBy(x => x.Name))
                    {
                        var blobRelativePath = "/" + localizedBlob.Path.TrimStart('/');
                        var contentItem = _contentItemFactory.GetItemFromPath(blobRelativePath);
                        if (contentItem != null)
                        {
                            if (contentItem.Name == null)
                            {
                                contentItem.Name = localizedBlob.Name;
                            }

                            contentItem.Language = localizedBlob.Language;
                            contentItem.FileName = Path.GetFileName(blobRelativePath);
                            contentItem.StoragePath = "/" + blobRelativePath.Replace(baseStoreContentPath + "/", string.Empty).TrimStart('/');
                            LoadAndRenderContentItem(blobRelativePath, contentItem);
                            retVal.Add(contentItem);
                        }
                    }
                }

                return retVal.ToArray();
            }));
        }
        public ActionResult ResetCache()
        {
            //TODO: Replace to some other (maybe with using reflection)
            ThemeEngineCacheRegion.ExpireRegion();
            CartCacheRegion.ExpireRegion();
            CatalogCacheRegion.ExpireRegion();
            ContentBlobCacheRegion.ExpireRegion();
            CustomerCacheRegion.ExpireRegion();
            MarketingCacheRegion.ExpireRegion();
            PricingCacheRegion.ExpireRegion();
            QuoteCacheRegion.ExpireRegion();
            RecommendationsCacheRegion.ExpireRegion();
            StaticContentCacheRegion.ExpireRegion();
            StoreCacheRegion.ExpireRegion();
            TaxCacheRegion.ExpireRegion();
            SubscriptionCacheRegion.ExpireRegion();
            SecurityCacheRegion.ExpireRegion();

            return(StoreFrontRedirect("~/"));
        }