Caching a dataset for improving performance in ASP.NET

In a website that I recently completed www.Christian-Jokes.net the left navigation bar listed the categories of jokes from the database. So on every page load I was querying the database to load the categories. Obviously this was a bad idea. I needed to cache the categories in memory without querying the database on every page load. The categories were placed in the master page. In the master page page load event I wrote the following code to cache the categories of jokes

Dim JokeCategories As Data.DataSet
JokeCategories = Cache("JokeCategories")
If JokeCategories Is Nothing Then
JokeCategories = GetJokeCategories()'This fn returns the categories
Cache.Insert("JokeCategories", JokeCategories, Nothing, DateAdd(DateInterval.Minute, 180, Now()), Web.Caching.Cache.NoSlidingExpiration)
End If
RepeaterJokecategories.datasource=Jokecategories RepeaterJokeCategories.Databind()

Just 5 lines of code and a great boost in performance !

Now the categories are loaded from the database only if they are not present in the cache. Once loaded the categories are placed in the cache for 180 minutes

One Response to “Caching a dataset for improving performance in ASP.NET”

  1. Why would someone devote an entire site to jokes about Christians? That just seems wrong somehow.

    ;-)

Leave a Reply