Pages

Thursday, 23 February 2012

Attaching SQL Server Cache Dependencies in ASP.NET

         In addition to changing settings in the OutputCache directive to activate SQL Server cache invalidation,you can also set the SQL Server cache invalidation programmatically,which can set as follows.
VB
Dim myDependency As SqlCacheDependency = New SqlCacheDependency("Nortwind","Customers")
Response.AddCacheDependency(myDependency)
Response.Cache.SetValidUtilExpires(true)
Response.Cache.SetExpires(DateTime.Now.AddMinutes(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
C#
SqlCacheDependency myDependency = new SqlCacheDependency( " Northwind " , " Customers " ) ;
Response.AddCacheDependency( myDependency ) ;
Response.Cache.SetValidUntilExpires( true ) ;
Response.Cache.SetExpires(DateTime.Now.AddNinutes(60)) ;
Response.Cache.SetCacheability(HttpCacheability.Public) ;
         You first create an instance of the SqlCacheDependency object,assigning it the value of the database and the table at the same time.The SqlCacheDependency class takes the following parameters;
                 SqlCacheDependency ( databseEntryName As String,tablename As String)
         You use this parameter construction if you working with SQL Server 7.0 or with SQL Server 2000.If you working with SQL Server 2005 use the following;
                 SqlCacheDependency(sqlCmd As System.Data.SqlClient.SqlCommand)
         After the SqlCacheDependency class is in place ,you add the dependency to the Cache object and set some of the properties of the Cache object as well.You can do this either programmatically or through the OutputCache directive.

No comments:

Post a Comment