Caching in ASP.Net
 
Caching Programmatically
 
The Cache property of the Page.HttpContext class can be used to store and retrieve data in the cache.
 
The Caching in ASP.NET is implemented on the bases of Hashtable with the Least Recently Used (LRU) model. The Namespace System.Web.Caching.Cache or Cache Class is used to implement the Data Caching.
 
Storing Data in Cache object
 
You can store data in the cache object using several techniques: using simple assignment, using the 'Add' method, and using the 'Insert' method.
 
Here are some lines of code that demonstrate the 3 approaches:
 
Cache ("Key1") = "This is text num1";
 
By using the 'Add' method you can set more parameters other than the key / value. You can set the expiration time, the priority, the dependency on another object, and a notification to your application to be sent when that key is deleted from the cache.
 
Cache.Add ("Key2", "This is text num2", Nothing, Caching.Cache.NoAbsoluteExpiration,
 
System.TimeSpan.FromMinutes (2), CacheItemPriority.Default, Nothing)
 
Using “Insert? method you can simply create a new key and assign a value to it. You can still optionally set other parameters like in the 'Add' method.
 
Cache.Insert ("Key3", "This is text number 3");
 
You can store data in the cache object in the form of key-value pairs as shown in the previous example.
 
Retrieving Data from Cache object
 
You can access the cached data by using their keys just like the 'Session' and 'Application' objects, but before attempting to retrieve a cached value you must check for its existence as shown in the following example:
 
Label1.Text = Cache.Item("Key1")
 
Removing Data from Cache Object
 
To remove a given key from the cache object, just use the 'Remove' method and supply the name of the key you need to remove as in the following line of code.
 
Cache.Remove("Key1");
 
When you use the 'Add' method or the 'Insert' method to add new data to your cache object, you can specify an 'OnRemove' callback function that will be called when the data is removed. This is the only event the cache object can provide.
 
Cache Dependencies
 
In ASP.NET, by using the cache object you can store and invalidate cache items in the cache based on several types of dependencies. It is a method through which the cache data can be made dependant on any file, a key or database. The possible dependencies for Cache object are following:
 
• Dependencies based on Key

• Dependencies based on File

• Dependencies based on Time

• Dependency based on SQL
 
Dependencies based on Key
 
When the cache item is dependant on another cache item, then key dependency is used. These cached items in the application cache are stored in key/value pairs. The key dependency allows an item to be dependent on the key of another item in the application cache. When the original item is removed, the item that has the key dependency is also removed.
 
Example:
 
Dim keyarray() As String = New String() {"mykey1"}

Cache.Insert("mykey2", value, New CacheDependency(Nothing, keyarray))
 
This code specified that the “mykey2? is dependant on the keyarray. It means whenever the “mykey1? value in the cache is altered; the “mykey2? will be removed from the cache.
 
Dependencies based on File
 
When the cache item is dependant on a file, then file dependency is used. In file based dependency an item in the cache is dependent on an external file. If the file is modified or deleted, the cached item is removed.
 
Cache.Insert ("mykey3", object, New CacheDependency(Server.MapPath("myfile.xml")))
 
This code specified that the “mykey3? is dependant on the file “myfile.xml?. It means whenever the file “a.xml? is altered; the “mykey3? will be removed from the cache.
 
Dependencies based on SQL
 
In SQL based dependency, an item in the cache is dependent on modification in a table in a Microsoft SQL Server 2005, SQL Server 2000, or SQL Server 7.0 database. For SQL Server 2005, an item can be dependent on a row in a table.
 
Dependencies based on Time
 
The caching is said to be time dependent when you are able to set time to expire and ASP.NET automatically removes items from the cache when they expire. Another technique for caching is based on time. For example, the cache can expire on a certain date, or it will only be available for a certain period of time. There are two ways in which you can implement time-based caching:
 
Absolute Expiration: The cache is set to expire on a particular date and time.

Sliding Expiration: The cache is set to expire after a certain period of inactivity.