. NET Database Cache dependency policy implementation

Source: Internet
Author: User

Working with large portals typically requires caching technology. This web accelerator
In PHP and Java is generally used based on squid to do.
Of course the Windows. NET platform is also available for squid with Windows versions.
After this, let's look at the. NET-brought cache policy. It was used by Microsoft's PetShop;
One, based on the database trigger (set cache dependency policy, when the data in the database changes, triggering cache invalidation, but Microsoft provides the solution currently only support SQL Server, if it is Oracle need to implement the trigger interface itself).
Second, the file-dependent policy (you can set the cache depends on the specific key values in the file, when the data update, the update file key values to force the cache invalidation).

and then

The previous two kinds of course are different from the page cache. The page cache may be rendering content that has expired. Caching Policy Polling is basically the latest content.

Let's step through this process:
First command cmd

Open the DOS interface

?
cd D:\Windows\Microsoft.NET\Framework\v2.0.50727

Note: Here is the system installation path.

Then here we set the cache policy dependency on the database fjmr_db.

?
aspnet_regsql -C "Data Source=.\SQLSERVER05;Integrated Security=True;Initial Catalog=fjmr_DB" -ed  

Then here we inject dependency on the table n_news (cache invalidation when table n_news has an add-on operation)

?
aspnet_regsql -C "Data Source=localhost;Integrated Security=True;Initial Catalog=fjmr_DB" -et -t N_News

Then configure the cache policy node under Web. config

1     <Caching>
2 <!--Set Database Cache dependency Policy Polling time MS whether enabled -
3 <SqlCacheDependencyPolltime= "the"enabled= "true">
4 <databases>
5 <!--database name, database link, polling time Ms -
6 <Addname= "fjmr_db"connectionStringName= "Database link Configuration"Polltime= "the"/>
7 </databases>
8 </SqlCacheDependency>
9
Ten </Caching>

Once you have configured polling for SQL cache dependencies, you can use SQL dependency on the page output cache.
Here is the page cache expiration time 9,999 seconds! , which means that the database will not be read for 9,999 seconds if it is not updated.
Of course if the database n_news is updated. Then the page will be refreshed immediately!

1 <%@ Page Language="C #" %>
2  <%@ OutputCache Duration="9999"VaryByParam="None"
3 SqlDependency="fjmr_db:n_news" %>

If you want a page to depend on more than one database table, you need to set the Sqldependecy attribute to a semicolon-delimited list of database and database table names.

Example: fjmr_db:n_news;fjmr_db: Other tables

You can also use the <%@ OutputCache%> directive in a user control to use polling for SQL cache dependencies.
That is, you can implement partial page caching using polling SQL cache dependencies.
To use polling for SQL cache dependencies on SqlDataSource and ObjectDataSource controls, you only need to set the Sqlcache-dependency property.
The database name must correspond to the database name that is configured in the Web configuration file <sqlCacheDependency> element!

< Asp:sqldatasource
ID = "Srcnews"
ConnectionString= "<%$ connectionstrings:news%>"
SelectCommand= "SELECT * from N_news"
enablecaching= "true"
SqlCacheDependency= "Fjmr_db:n_news"
onselecting= "Srcnews_selecting"
Runat= "Server"/>

We can also use polling SQL cache dependency on the cache object, using the SqlCacheDependency object to represent a polling SQL cache dependency.
When a DataTable is added to the cache, the class is used as a parameter to Cache.Insert ().
If you need to create a dependency on multiple database tables,
You need to create multiple SqlCacheDependency objects and represent multiple dependencies using an instance of the Aggregate-cachedependency class.

1 DataTable News=(DataTable) cache["_news"];
2  if(News== NULL)
3 {
4 News=Getnewsfromdb ();
5 SqlCacheDependency sqldepend= New
6 SqlCacheDependency ("MyDatabase", "_news");
7 Cache.Insert ("_news", news, Sqldepend);
8 }
9 Grdnews.datasource=News
Ten Grdnews.databind ();
One }

Finally, this caching strategy only applies to general medium-sized cache acceleration. When it comes to large-scale systems, it often uses nosql technology, a distributed cache server.

. NET Database Cache dependency policy implementation

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.