ASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Caching Application Data > Notifying an Application When an Item is Deleted from the Cache
ASP.NET Web Applications ASP .NET Caching Features Caching Application Data
ASP.NET provides the CacheItemRemovedCallback delegate. It defines the signature to use when you write event handlers to respond when an item is deleted from the cache. ASP.NET also provides the CacheItemRemovedReason enumeration, which you can use to make event handlers dependent upon the reason the item is deleted.
- Create a local variable that raises the event for the CacheItemRemovedCallback delegate. For example, the following code creates an
onRemove
local variable of type CacheItemRemovedCallback
.
private static CacheItemRemovedCallback onRemove = null;
NOTE: The variable must be of this type to be used in the onRemoveCallback parameter of the Cache.Add or Cache.Insert method in Step 4.
- Create an event handler to respond when the item is removed from the cache. For example, the following code recreates a database query when an item added to the cache in Step 4 is removed. It also places the data response in the cache.
NOTE: Using the members of the CacheItemRemovedReason enumeration to create conditional code for the method in this step is an option open to you.
public void RemovedCallback ( string k, object v, CacheItemRemovedReason r )
SqlConnection myConnection = new SqlConnection (
"server=localhost;uid=sa;pwd=;database=pubs" );
SqlDataAdapter myCommand = new SqlDataAdapter ( "select * from Authors", myConnection );
DataSet ds = new DataSet ( );
myCommand.Fill ( ds, "Authors" );
DataView Source = new DataView ( ds.Tables [ "Authors" ] );
Cache [ "myData" ] = Source;
}
Public Sub RemovedCallback ( k As String, v As Object, r As CacheItemRemovedReason )
Dim myConnection As New SqlConnection ( _
"server=localhost;uid=sa;pwd=;database=pubs" )
Dim myCommand As New SqlDataAdapter ( "select * from Authors", myConnection )
Dim ds As New DataSet ( )
myCommand.Fill ( ds, "Authors" )
Dim Source As New DataView ( ds.Tables ( "Authors" ) )
Cache ( "myData" ) = Source
End Sub |
|
C# |
VB |
NOTE: This event handler must use the same signature as the CacheItemRemovedCallback delegate.
- Create an instance of the CacheItemRemovedCallback delegate that calls the event handler. The following code calls the method created in Step 2.
onRemove = new CacheItemRemovedCallback ( this.RemovedCallback );
- Add the item to the cache by using either the Cache.Add method or Cache.Insert method. You must specify the local variable, created in Step 1, in the onRemoveCallback parameter. The following code uses the Insert method to add an item to the cache with a key of
"myData"
and a value of Source
. It defines the onRemove
variable in the onRemoveCallback parameter.
Cache.Insert ( "myData", Source, null, DateTime.Now.AddMinutes ( 2 ), NoSlidingExpiration,
CacheItemPriority.High, CacheItemPriorityDecay.Slow, onRemove );
When the item added in Step 4 is removed from the cache for any reason, the RemovedCallback
method is called, and the code within it generates a new database query and adds the resulting dataset to the cache.
Adding Items to the Cache Retrieving Values of Cached Items Deleting Items from the Cache