C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Easy Way to Cache Static Data in ASP.NET


Posted Date: 01 Jul 2009    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: pradeepMember Level: Silver    
Rating: 1 out of 5Points: 10



This article will demonstrate an efficient way to keep look up data cached. Take note that this example is designed for smaller datasets. It probably would not be a good idea to keep large amounts of data in memory, but this example works very well for smaller tables. This sample uses the Northwind database that you can download here.

In order to run the sample, you must change the connection string "NorthwindConnectionString" in the web.config to point to your copy of Northwind.

This example is designed to return data that is updated nightly. In my production application, the data was being called from DB2. For simplicity and so that anyone can run the sample, this example uses SQL Server instead.

//Returns the next 6:00AM when the data will have been updated.
private static DateTime cacheExpirationDateTime

{ get
{
if (DateTime.Now.Hour < 6)
return DateTime.Today.AddHours(6);
else
return DateTime.Today.AddDays(1).AddHours(6);
}
}

private static DataTable dtTerritories
{ get
{
//Check if the item exists in the Cache, if not add it.
if (System.Web.HttpContext.Current.Cache["dtTerritories"] == null)
{
DataTable dtTerritories =
GetLiveData("select * from dbo.Territories order by TerritoryDescription asc");
dtTerritories.PrimaryKey = new DataColumn[] { dtTerritories.Columns[0] };
//Add PrimaryKey for good measure. If filter on the key it will be quicker.
System.Web.HttpContext.Current.Cache.Add(
"dtTerritories", dtTerritories, null, cacheExpirationDateTime,
Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null);
}
return (DataTable)System.Web.HttpContext.Current.Cache["dtTerritories"];
}
}

//Use a DataView to avoid making database calls on static data
public static DataTable getTerritories(int regionID)
{
DataView dv = new DataView(dtTerritories);
dv.RowFilter = "RegionID = " + regionID.ToString();
return dv.ToTable();
}


This example is clearly not the only approach to doing this. ASP.NET offers many ways of accessing / storing data. This article is just one easy way... In this scenario, I returned DataTables for simplicity and ease of coding. In my actual project (not this example), I had completed my AS400 (DB2) DataAccess class, which returned data from 10 reference tables in less than 2 hours!




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Static Data  .  Cache  .  ASP.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SilverLight for Beginners
Previous Resource: Using function point to quote a software
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use