. Net dependent Cache

Source: Internet
Author: User

Cache-it can improve the performance of the site.

Using the cache in websites with large traffic but few updates can greatly improve the running efficiency;

In. net, we provide a good caching mechanism, including page caching, data caching, and dependency caching.

The benefit of dependency caching is that it is related to the change of dependent items. For example, if the data in the cachetable table is changed, the data that has been cached will be clear and the new data will be read again, data can be updated in real time.

In the first few days, I learned the SQL dependency cache and recorded the implementation process and steps:

Environment: Windows 2003 + IIS6, sql2008 + vs2008

 

1. Create the database cachedb in sql2008 and create the table cachetable.

Run the aspnet_regsql.exe application under the Visual Studio command prompt (you can also find C:/program files/Microsoft Visual Studio 9.0/VC> in cmd to run it)

Run asp.net_regsql.exe directly to create other feature tables of the selected database, such as aspnet_roles.

The following uses the database cachedb and data table cachetable as an example;

To create a database dependent cache, run the following command:

Aspnet_regsql-s localhost-U sa-P 123456-D cachedb-ed // open the dependent database, and then create the dependent data table

Aspnet_regsql-s localhost-U sa-P 123456-D cachedb-et-T cachetable // create a dependent data table

 

After running the preceding command, the data table aspnet_sqlcachetablesforchangenotification is displayed.

 

Parameters of the aspnet_regsql command:

-? Displays the help function of the tool;

-S is followed by the database server name or IP address;

-U is followed by the login username of the database;

-P is followed by the database login password;

-E this function is used for Windows integrated verification;

-D the following parameter indicates which database adopts the sqlcachedependency function;

-T followed by the sqlcachedependency function for which table the parameter is used;

-Ed allows sqlcachedependency for databases;

-Dd prohibits the use of sqlcachedependency for databases;

-Et allows sqlcachedependency for data tables;

-DT prohibits the use of sqlcachedependency for data tables;

-Lt lists the tables in the current database that have used the sqlcachedependency function.

 

The above steps are implemented in the database.

 

2. Configure the Web. config of the website

Add the cache configuration in the system. Web node. The Code is as follows:

This method mainly uses the data query round-robin method.

 

 

<System. Web> <br/> <! -- Dependency cache --> <br/> <caching> <br/> <! -- Five-second round robin --> <br/> <sqlcachedependency enabled = "true" polltime = "5000"> <br/> <databases> <br/> <Add name = "cachedb" connectionstringname = "sqlserver"/> <br/> </databases> </P> <p> </sqlcachedependency> </P> <p> </caching> <br/> </system. web>

 

Parameter description: polltime: the rotation time (millisecond), "cachedb": indicates the dependent database, sqlserver: indicates the database connection string, in connectionstringname.

Iii. Code Implementation

1. PairPage output CacheUse polling SQL cache Dependencies
According to the Web. config configuration above
<% @ Outputcache duration = "6000" varybyparam = "NONE" sqldependency = "cachedb: cachetable" %> cachetable is a data table on the page to be cached

When the cachetable data table is changed, new data is displayed after refreshing.

2. PairData CacheUse polling SQL cache Dependencies

Implement dependency caching for multiple tables. The Code is as follows:

// Use the gridview to bind the data </P> <p> Public void binddata () <br/>{< br/> string [] tablename = new string [] {"cache", "cachetable"}; // The cache table and cachetable table are used here, the method for establishing the dependency of the database in the cache table is as follows: cachetable. <Br/> aggregatecachedependency aggrespondependency = new aggregatecachedependency (); // instantiate the dependency for loading multiple instances sqlcachedependency <br/> dataset DS = cache ["gridview"] As dataset; // read data from the cache <br/> If (DS = NULL) <br/>{< br/> DS = dbhelper. executeadapter ("select * From cachetable inner join cache on cachetable. id = cache. cacheid "); // read data from the database and return the access method in sqlhelper for dataset and executeadapter </P> <p> foreach (string table in tablename) <br/>{< br/> aggrespondependency. add (New sqlcachedependency ("cache", table )); // load the dependent instance to the array <br/>}< br/> // sqlcachedependency dependency = new sqlcachedependency ("cache", "cachetable "); // directly instantiate the dependent cache for a single table <br/> cache. insert ("gridview", DS, aggrespondependency); // Add to cache <br/> response. write (datetime. now. tostring (); // output the current time to test whether the cache is successful. </P> <p >}</P> <p> gridview1.datasource = Ds; <br/> gridview1.databind (); <br/>}

 

 

The above code is the dependent cache used in the data cache

 

Note: In web. config, dbcahce and dbcache: cahcetabel in the page output cache must be the same as the case in which aspnet_regsql.exe is used to create a data cache.

 

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.