Пример #1
0
        public ActionResult Index(int page = 1)
        {
            int    pagesize    = homepagesize;
            var    query       = _db.Blogs.Include("posts").Where(b => b.isApproved == true);
            string categoryIds = string.Empty;
            bool   hideHarmony = false;
            bool   cachable    = true;

            if (isHarmony)
            {
                query = query.Where(b => b.isHarmony);
            }
            else
            {
                var opt = _blogUtil.GetUserOption(User.Identity.Name, o => new { o.homepageCategories, o.homepageHideHarmony, o.homepageTagBlacklist });
                categoryIds = opt.homepageCategories;
                hideHarmony = opt.homepageHideHarmony;
                List <int> catIdList    = new List <int>();
                List <int> tagBlacklist = new List <int>();
                if (!string.IsNullOrEmpty(categoryIds))
                {
                    catIdList = JsonConvert.DeserializeObject <List <int> >(categoryIds);
                }
                if (!string.IsNullOrEmpty(opt.homepageTagBlacklist))
                {
                    tagBlacklist = JsonConvert.DeserializeObject <List <int> >(opt.homepageTagBlacklist);
                    cachable     = false;
                }
                if (catIdList.Count > 0 || tagBlacklist.Count > 0)
                {
                    catIdList = _catUtil.GetCategoryWithSubcategories(catIdList);
                    query     = BlogHelper.getFilteredQuery(_db, query, tagBlacklist, catIdList, featuredBlogId);
                }
                if (hideHarmony == true)
                {
                    query = query.Where(b => !b.isHarmony);
                }
            }

            if (featuredBlogId != null && featuredBlogId.Count > 0)
            {
                ViewBag.featuredBlog = featuredBlogId;
                query = query.OrderByDescending(b => featuredBlogId.Contains(b.BlogID)).ThenByDescending(b => b.BlogDate);
            }
            else
            {
                query = query.OrderByDescending(b => b.BlogDate);
            }

            int itemCount = -1, pageCount = -1, pageNumber = 1;
            ConcurrentDictionary <string, X.PagedList.IPagedList <BlogDisplay> > cache = null;

            if (cachable)
            {
                var firstPageKey = CacheService.GetHomePageListKey(1, isHarmony, hideHarmony, categoryIds);
                cache = _cache.Get <ConcurrentDictionary <string, X.PagedList.IPagedList <BlogDisplay> > >(CacheService.HomePageCacheKey)
                        ?? new ConcurrentDictionary <string, X.PagedList.IPagedList <BlogDisplay> >();
                if (cache.Count > 0 && cache.ContainsKey(firstPageKey))
                {
                    itemCount = cache[firstPageKey].TotalItemCount;
                    pageCount = cache[firstPageKey].PageCount;
                }
            }
            if (pageCount < 0)
            {
                itemCount = query.Count();
                pageCount = (int)Math.Ceiling(itemCount / (double)pagesize);
            }
            if (page > 0 && page <= pageCount)
            {
                pageNumber = page;
            }

            var cachekey = CacheService.GetHomePageListKey(pageNumber, isHarmony, hideHarmony, categoryIds);

            X.PagedList.IPagedList <BlogDisplay> model;
            if (cache == null || !cache.TryGetValue(cachekey, out model))
            {
                if (cache != null)
                {
                    _cache.Set(CacheService.HomePageCacheKey, cache, new MemoryCacheEntryOptions()
                    {
                        Priority = CacheItemPriority.NeverRemove
                    });
                }
                var harmonysettings = _appSettings.HarmonySettings;
                if (!isHarmony || harmonysettings.WhitelistTags == null)
                {
                    model = query.Select(b => new BlogDisplay {
                        blog = b,
                        tag  = _db.TagsInBlogs.Where(t => t.BlogID == b.BlogID).Select(t => t.tag)
                    }).ToPagedList(pageNumber, pagesize, itemCount);
                }
                else
                {
                    model = query.Select(b => new BlogDisplay {
                        blog = b,
                        tag  = _db.TagsInBlogs.Where(t => t.BlogID == b.BlogID &&
                                                     !harmonysettings.WhitelistTags.Contains(t.TagID)).Select(t => t.tag)
                    }).ToPagedList(pageNumber, pagesize, itemCount);
                }
                if (cache != null)
                {
                    cache.TryAdd(cachekey, model);
                }
            }

            if (Request.IsAjaxRequest())
            {
                return(PartialView(model));
            }
            return(View(model));
        }