public static List <BlogLinkItem> GetBlogLinksWithPostCounts(AgilityContentItem blogConfig, IAgilityContentRepository <AgilityContentItem> postsRepo, IAgilityContentRepository <AgilityContentItem> blogLinksRepo, List <DateTime> blogLinksDates, bool getCounts) { //blog links can be either Categories or Tags var cacheKey = string.Format("Agility.Components.Blog.GetBlogLinksWithPostCounts_{0}_{1}_{2}_{3}", blogConfig.ContentID, postsRepo.ContentReferenceName, (blogLinksRepo != null ? blogLinksRepo.ContentReferenceName : ""), (blogLinksDates != null ? blogLinksDates.Count : 0)); List <BlogLinkItem> links = HttpContext.Current.Cache[cacheKey] as List <BlogLinkItem>; if (links != null) { return(links); } lock (_getBlogLinksCountsCacheLock) { if (links != null) { return(links); } if (blogLinksDates != null && blogLinksDates.Any()) { links = blogLinksDates.Select(i => new BlogLinkItem() { Title = i.ToString("MMMM yyyy"), Url = string.Format("{0}?month={1}", BlogContext.GetBlogRootPath(blogConfig), i.ToString("MM-yyyy")), PostCount = (getCounts ? BlogUtils.GetPostCountByDate(postsRepo, i) : 0), }).ToList(); } else if (blogLinksRepo != null) { var items = blogLinksRepo.Items(); links = items.Select(i => new BlogLinkItem() { Title = i["Title"] as string, Url = i.BlogDynamicUrl(blogConfig, null), PostCount = (getCounts ? BlogUtils.GetPostCount(postsRepo, i) : 0), ImageUrl = GetBlogPostListingImageUrl(i), ImageLabel = GetBlogPostListingImageTitle(i), Excerpt = "" }).ToList(); } HttpContext.Current.Cache.Add(cacheKey, links, null, DateTime.Now.AddHours(2), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null); return(links); } }
public static List <AgilityContentItem> GetRelatedPosts(AgilityContentItem post, IAgilityContentRepository <AgilityContentItem> posts, bool randomize, int limit) { List <AgilityContentItem> relatedPosts = null; int maxLimit = 100; switch (post["RelatePostsBy"] as string) { case "BlogTags": relatedPosts = GetPosts(posts, -1, "", "", post["BlogTagsIDs"] as string, "", "", maxLimit, 0); break; case "BlogPosts": string relatedPostsIDs = post["RelatedPostsIDs"] as string; if (!string.IsNullOrEmpty(relatedPostsIDs)) { relatedPosts = GetPosts(posts, -1, relatedPostsIDs, "", "", "", "", maxLimit, 0); } randomize = false; break; default: //BlogCategories relatedPosts = GetPosts(posts, -1, "", post["CategoriesIDs"] as string, "", "", "", maxLimit, 0); break; } //ensure the current post isn't relating to itself relatedPosts.RemoveAll(i => i.ContentID == post.ContentID); if (randomize) { relatedPosts = BlogUtils.PickRandomItems <AgilityContentItem>(relatedPosts, limit).ToList(); } return(relatedPosts); }
public static int GetPostCountByDate(IAgilityContentRepository <AgilityContentItem> posts, DateTime month) { string filter = BlogUtils.GetSqlBetweenMonthRangeStatement("Date", month); return(GetPostCount(posts, filter)); }
public static List <AgilityContentItem> GetPosts(IAgilityContentRepository <AgilityContentItem> postsRepo, int authorID, string postsIDs, string categoriesIDs, string tagsIDs, string appendFilter, string sort, int take, int skip, out int totalCount) { string cacheKey = string.Format("Agility.Components.Blog.GetPosts_{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}", postsRepo.ContentReferenceName, postsIDs, authorID, categoriesIDs, tagsIDs, appendFilter, sort, take, skip); PostsResult postsResult = AgilityDependencyCache.Select(cacheKey) as PostsResult; if (postsResult != null) { totalCount = postsResult.TotalCount; return(postsResult.Posts); } lock (_getPostsCacheLock) { if (postsResult != null) { totalCount = postsResult.TotalCount; return(postsResult.Posts); } StringBuilder sbFilter = new StringBuilder(); var posts = new List <AgilityContentItem>(); if (authorID > 0) { sbFilter.AppendFormat("AuthorID = {0}", authorID); } if (!string.IsNullOrEmpty(postsIDs)) { if (sbFilter.Length > 0) { sbFilter.Append(" AND "); } sbFilter.AppendFormat("ContentID in ({0})", postsIDs); } if (!string.IsNullOrEmpty(categoriesIDs)) { if (sbFilter.Length > 0) { sbFilter.Append(" AND "); } sbFilter.Append(BlogUtils.GetSqlContainsStatement("CategoriesIDs", categoriesIDs, ',')); } if (!string.IsNullOrEmpty(tagsIDs)) { if (sbFilter.Length > 0) { sbFilter.Append(" AND "); } sbFilter.Append(BlogUtils.GetSqlContainsStatement("BlogTagsIDs", tagsIDs, ',')); } if (!string.IsNullOrEmpty(appendFilter)) { if (sbFilter.Length > 0) { sbFilter.Append(" AND "); } sbFilter.Append(appendFilter); } string rowFilter = sbFilter.ToString(); posts = postsRepo.Items(rowFilter, sort, take, skip, out totalCount); postsResult = new PostsResult(); postsResult.TotalCount = totalCount; postsResult.Posts = posts; //cache it, but clear cache when the Blog Post list is changed AgilityDependencyCache.Insert(cacheKey, postsResult, new List <string> { postsRepo.ContentReferenceName }, DateTime.Now.AddHours(1)); return(postsResult.Posts); } }