SqlCacheDependency multi-table-based Cache Application

Source: Internet
Author: User
SqlCacheDependency the table-based Cache is applied in sql2000 and slq20005 in many ways. Most of them create triggers on the tables to be detected, in addition, Asp.net uses the polling mechanism to check whether the table data is changed.
Procedure
First, ensure that the database has the corresponding basic structure, which can be configured through aspnet_regsql,
-S server name-U Login ID-P password-d database name-t name of the data table to be tracked-et
Or you can use code to create a basic structure.
SqlCacheDependencyAdmin. EnableNotifications (string connStr );
SqlCacheDependencyAdmin. EnableTableForNotifications (string connstr, string [] tables );
The same effect,
The following is the Demo code asp.net.
 
Private AggregateCacheDependency GetDependencty ()
{
AggregateCacheDependency aggDep = new AggregateCacheDependency ();
AggDep. Add (new SqlCacheDependency ("CacheTest", "UserList "));
AggDep. Add (new SqlCacheDependency ("CacheTest", "UrlList "));
Return aggDep;
}
---------------------
Private DataSet GetDataProxy ()
{
DataSet ds = HttpRuntime. Cache ["SQL2005_CacheTest_UserList"] as DataSet;
If (ds = null)
{
AggregateCacheDependency cd = getaggrespondependency ();
Ds = GetData ();
HttpRuntime. Cache. Insert ("SQL2005_CacheTest_UserList", ds, cd );
Response. Write ("the source is the database! ");
}
Return ds;
}
---------------------------

Protected void button#click (object sender, EventArgs e)
{
GridView1.DataSource = GetDataProxy ();
GridView1.DataBind ();

}

When the record set to be cached comes from multiple tables, you need to use the AggregateCacheDependency object,
AggregateCacheDependency ensures that when any object in the set is saved (CacheDependency), the associated cache data is also ineffective,
It also invalidates all objects in AggregateCacheDependency. This is important. If not, GetDependencty () in this example ()
Function:
AggDep. Add (new SqlCacheDependency ("CacheTest", "UserList "));
AggDep. Add (new SqlCacheDependency ("CacheTest", "UrlList "));
Two SqlCacheDependency values are added at the same time. If one of them does not cause another failure, httpruntime will continue to track another non-updated data table, that is, the polling operation will continue, however, according to the results of tracking SQL Server Profilter, when any item fails (data table update), the Round Robin will stop,
Until the user creates a new AggregateChacheDependency,

Finally, the table-based database cache must have the following configuration in the web. config file:
<System. web>
.....
<Caching>
<SqlCacheDependency enabled = "true" pollTime = "10000">
<Databases>
<Add name = "CacheTest" connectionStringName = "SQL2005CacheTest" pollTime = "10000"/>
</Databases>
</SqlCacheDependency>
</Caching>
</System. web>
PollTime is the consultation time. We recommend that you do not use this type of cache for less than 1 minute or longer.
Name = "CacheTest", in which CacheTest is used as the pass value of SqlCacheDependency (dbEntryName, table) constructor parameter dbEntryName.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.