|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe concept of Caching was introduced in ASP.NET 1.X and has been improved significantly in ASP.NET 2.0. Caching allows you to store commonly used items in the memory and thus not create them from scratch when they are requested. There are different types of Caching available in the .NET framework. In this article I will introduce you to Caching dependencies. Caching DependenciesCaching can depend on several items which include user request, file, database tables, table rows, duration, user controls, query strings, browsers and also other cached items. Let's start with the Caching dependency on other cached items. Caching Dependency on Cached ItemsIt is interesting to note that your cached item can depend on other cached items. This means that if the item A is removed from the cache you can also remove item B from the cache. Let's start by creating and inserting the item A and item B in the protected void Button3_Click(object sender, EventArgs e)
{
// create item A and item B
string itemA = "ItemA";
string itemB = "ItemB";
Cache.Insert("ItemA", itemA, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero,
CacheItemPriority.Default, MyItemRemovedCallBack);
Cache.Insert("ItemB", itemB, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero,
CacheItemPriority.Default, MyItemRemovedCallBack);
}
In the code above I am creating two items " private void MyItemRemovedCallBack(string key, object value,
CacheItemRemovedReason reason)
{
// remove the item from the cache
if (key == "ItemA" ) Cache.Remove("ItemB");
else if (key.Equals == "ItemB") Cache.Remove("ItemB");
}
The key: This is the key which is used to identify the items in the Cache. reason: The variable "reason" is of type The code given below removes an item from the Cache and as soon as the item is removed the " protected void Btn_RemoveCacheItem(object sender, EventArgs e)
{
Cache.Remove("ItemA");
}
Cache Dependency on a FileNext, let's see the Cache dependency on a file. Making the Cache dependent on the file means that when the file contents changes the Cache is expired and the contents are fetched again. Take a look at the simple XML file below which contains the information about the web site menu. <?xml version="1.0" encoding="utf-8"?>
<MenuItems>
<MenuItem>
<Text>Home</Text>
</MenuItem>
<MenuItem>
<Text>About us</Text>
</MenuItem>
<MenuItem>
<Text>Contact us</Text>
</MenuItem>
<MenuItem>
<Text>Help</Text>
</MenuItem>
<MenuItem>
<Text>Feature</Text>
</MenuItem>
</MenuItems>
We will use the private void CreateMenu()
{
string menuPath = "MyFiles/Menu.xml";
string folderName = "MyFiles/";
DataSet ds = null;
if (Cache["Menu"] == null)
We will use the private void CreateMenu()
{
string menuPath = "MyFiles/Menu.xml";
string folderName = "MyFiles/";
DataSet ds = null;
if (Cache["Menu"] == null)
{
ds = new DataSet();
ds.ReadXml(Server.MapPath(menuPath));
// menu is created
Cache.Insert("Menu", ds, new System.Web.Caching.CacheDependency(
Server.MapPath(menuPath)),DateTime.Now.AddMinutes(60),
TimeSpan.Zero,
System.Web.Caching.CacheItemPriority.Default,
new System.Web.Caching.CacheItemRemovedCallback(
CacheItemRemovedCallBack));
DisplayCacheCreationTime("Object was not in the cache and created at:",
DateTime.Now.ToLongTimeString());
}
else
{
// menu is created from the cache
DisplayCacheCreationTime("Object was in the cache",String.Empty);
}
}
The You can also create a Cache dependency on the folder. This will mean that whenever a file or a subfolder is added, deleted then the Cache is expired and a new (fresh) copy is fetched. Cache Dependency on SQLOne of the biggest improvements in ASP.NET 2.0 Caching is the feature of Cache dependency on database tables. This means that the data will be present in the Cache as long as the table entries does not change. As, soon as the database table is changed the Cache is expired. You can enable the SQL Cache dependency by using the aspnet_regsql.exe command line tool. Simply, type the following command on the Visual Studio.NET 2005 command line. aspnet_regsql -ed -E -d School
The command above will enable the Cache dependency on the "School" database. The next step is to enable the caching on the individual table. You can do that by using the following line. aspnet_regsql -et -E -d School -t Users
The above line will enable the caching on the Users table which is contained in the School database. The next step is to create the <connectionStrings>
<add name="ConnectionString"
connectionString="Server=localhost;Database=School;
Trusted_Connection=true"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency pollTime="10000" enabled="true" >
<databases>
<add connectionStringName="ConnectionString" name="School"/>
</databases>
</sqlCacheDependency>
</caching>lt;/caching>
As, you have noticed that the The final step is to use the caching in your code. You can do this in various ways. Take a look at the following code which uses caching programmatically. private void BindData()
{
// if null then fetch from the database
if (Cache["Users"] == null)
{
// Create the cache dependency
SqlCacheDependency dep = new SqlCacheDependency("School", "Users");
string connectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT FirstName, LastName " +
"FROM Users", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
// put in the cache object
Cache.Insert("Users", ds, dep);
}
gvUsers.DataSource = Cache["Users"] as DataSet;
gvUsers.DataBind();
}
The line Caching in SQL Server 2005 have a different architecture then in SQL Server 2000. You don't have to write any lines in web.config to enable Caching in SQL Server 2005. Also, in SQL Server 2005 the Cache is only expired when the row is changed in the database table. ConclusionCaching when implemented correctly improves the performance of the application considerably. There is much more to learn in Caching and which I will cover in my upcoming articles.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||