How To: Add Blog Posts to the CSCache with CS2007

Jun 16, 2007 14:21 · 242 words · 2 minutes read c# code communityserver site-update

After adding my new CS2007 theme, I wanted to take advantage of caching for my home page. There was some sample code in the default theme, but it wasn’t exactly what I needed:

protected override void OnInit(EventArgs e)
{
List recentPosts = CSCache.Get("HomePageSearch-"
+ CurrentCSContext.User.RoleKey) as List;
if (recentPosts == null)
{
SearchQuery query = new SearchQuery();
query.StartDate = DateTime.Now.AddDays(-10);
query.EndDate = DateTime.Now.AddDays(1);
query.PageSize = 5;

recentPosts = CSSearch.Search(query).Posts;
CSCache.Insert("HomePageSearch-"
+ CurrentCSContext.User.RoleKey, recentPosts, CSCache.MinuteFactor * 5);
}
RecentPostList.DataSource = recentPosts;

base.OnInit(e);
}

 

This does a query for all additions to the site in the last 10 days, including images, comments, etc. But I wanted just blog entries. Looking around on the web, I found a discussion on this in the CS Forums. Using this as guide, I got exactly what I needed:

protected override void OnInit(EventArgs e)
{
int NumberOfBlogPosts = 3;
List recentPosts = CSCache.Get("HomePageBlog-"
+ CurrentCSContext.User.RoleKey) as List;

if (recentPosts == null)
{
List FilteredPosts = new List();
SearchQuery query = new SearchQuery();
query.PageSize = 20;
query.ApplicationTypes = new ApplicationType[] { ApplicationType.Weblog };
recentPosts = CSSearch.Search(query).Posts;

int PostNumber = ;
foreach (IndexPost post in recentPosts)
if (PostNumber < NumberOfBlogPosts)
if (!post.Title.StartsWith("re:", true, null))
{
FilteredPosts.Add(post);
PostNumber++;
}


CSCache.Insert("HomePageBlog-"
+ CurrentCSContext.User.RoleKey, FilteredPosts, 300);
recentPosts = FilteredPosts;
}
RecentPostList.DataSource = recentPosts;
base.OnInit(e);
}

 

This finds 20 weblog posts, filters out everything that starts with “Re:”, adds it to the cache, and uses it as a datasource for the blogpost list.