Beispiel #1
0
        public async Task <IActionResult> CacheContentAsync <T>(
            SectionServerCached component,
            IEnumerable <int> categoryIds,
            Func <Task <T> > dataLoader,
            int?page = null)
        {
            if (!cachePolicy.CanCache(component, new RequestOptions()
            {
                PageNumber = page
            }))
            {
                return(Json(await dataLoader()));
            }

            var key = keyGenerator.ContentGenerateKey(component.Name, categoryIds, page);

            string json;

            if (!string.IsNullOrEmpty(json = contentCache.GetContent(key)))
            {
                return(JsonString(json));
            }

            var content = await dataLoader();

            json = SunJson.Serialize(content);
            contentCache.CacheContent(key, json);
            return(JsonString(json));
        }
Beispiel #2
0
        public static Type GetBySection(SectionServerCached section)
        {
            string sectionTypeName = section.Data.GetType().Name;
            string presenterName   =
                sectionTypeName.Substring(0, sectionTypeName.Length - "ServerSection".Length) +
                "Presenter";

            return(Types[presenterName]);
        }
        public virtual async Task <IActionResult> GetMaterials(GetMaterialsRequest materialsRequest)
        {
            SectionServerCached section = sectionsCache.GetSectionServerCached(materialsRequest.SectionName, User.Roles);

            if (section == null)
            {
                return(BadRequest($"No component {materialsRequest.SectionName} found in cache"));
            }

            MaterialsServerSection sectionData = section.GetData <MaterialsServerSection>();

            if (!("," + sectionData.CategoriesNames + ",").Contains("," + materialsRequest.CategoryName + ","))
            {
                return(BadRequest(
                           $"Can not show {materialsRequest.CategoryName} in {materialsRequest.SectionName} section"));
            }

            CategoryCached category = categoriesCache.GetCategory(materialsRequest.CategoryName);

            if (category == null)
            {
                return(BadRequest($"Can not find {materialsRequest.CategoryName} category"));
            }


            MaterialsShowOptions options = new MaterialsShowOptions
            {
                ShowDeleted = materialsRequest.ShowDeleted,
                CategoryId  = category.Id,
                Page        = materialsRequest.Page,
                Sort        = MaterialsSortOptionsService.MaterialsSortOptions[materialsRequest.Sort]
            };

            using var scope = serviceProvider.CreateScope();
            IMaterialsQueryPresenter materialsQueryPresenter =
                (IMaterialsQueryPresenter)scope.ServiceProvider.GetRequiredService(MaterialsPresenterTypes.GetBySection(section));

            return(await CacheContentAsync(category, category.Id,
                                           () => materialsQueryPresenter.GetMaterialsByCategoryAsync(options), new RequestOptions()
            {
                Sort = materialsRequest.Sort,
                PageNumber = materialsRequest.Page
            }));
        }
        public virtual async Task <IActionResult> GetMaterialsFromMultiCategories(GetMaterialsRequest materialsRequest)
        {
            SectionServerCached section = sectionsCache.GetSectionServerCached(materialsRequest.SectionName, User.Roles);

            if (section == null)
            {
                return(BadRequest($"No component {materialsRequest.SectionName} found in cache"));
            }

            MaterialsServerSection sectionData = section.GetData <MaterialsServerSection>();

            var сategories = categoriesCache.GetAllCategoriesWithChildren(sectionData.CategoriesNames);

            IList <CategoryCached> categories = authorizationService.GetAllowedCategories(User.Roles,
                                                                                          сategories.Values, operationKeysContainer.MaterialAndCommentsRead);

            if (categories.Count == 0)
            {
                return(BadRequest("No categories to show"));
            }

            IEnumerable <int> categoriesIds = categories.Select(x => x.Id).ToArray();

            MaterialsSortOptionsService.MaterialsSortOptions.TryGetValue(materialsRequest.Sort,
                                                                         out Func <IQueryable <Material>, IOrderedQueryable <Material> > sort);

            var options = new MaterialsShowOptions
            {
                CategoriesIds = categoriesIds,
                Page          = materialsRequest.Page,
                PageSize      = sectionData.PageSize,
                Sort          = sort
            };

            using var scope = serviceProvider.CreateScope();
            IMaterialsQueryPresenter materialsQueryPresenter =
                (IMaterialsQueryPresenter)scope.ServiceProvider.GetRequiredService(MaterialsPresenterTypes.GetBySection(section));

            return(await CacheContentAsync(section, categoriesIds,
                                           () => materialsQueryPresenter.GetMaterialsFromMultiCategoryAsync(options), materialsRequest.Page));
        }
 public bool CanCache(SectionServerCached component, RequestOptions options)
 {
     return(true);
 }
Beispiel #6
0
 public bool CanCache(SectionServerCached component, int?page = null)
 {
     return((!page.HasValue || page == 1) && component.IsCacheData);
 }
Beispiel #7
0
 public bool CanCache(SectionServerCached component, RequestOptions options)
 {
     return(options.PageNumber.HasValue && options.PageNumber == 1);
 }
Beispiel #8
0
 public bool CanCache(SectionServerCached component, RequestOptions options)
 {
     return((!options.PageNumber.HasValue || options.PageNumber.Value == 1) && component.IsCacheData);
 }