Asp.net page SqlCacheDependency cache instance,

Source: Internet
Author: User

Asp.net page SqlCacheDependency cache instance,

Cache Technology is a very practical technology in asp.net programming, and is also a common technology in large-scale web programming. This article describes this in the form of an instance. The details are as follows:

SqlCacheDependency SQL cache on the asp.net page:

The Cahce/SqlCachePage. aspx page code is as follows:

<% @ Page Language = "C #" MasterPageFile = "~ /Site. master "AutoEventWireup =" true "CodeFile =" SqlCachePage. aspx. cs "Inherits =" Cahce_SqlCachePage "Title =" Page SQL cache "%> <% @ OutputCache Duration =" 999999 "SqlDependency =" VS2005_Test: sqlcache "VaryByParam =" none "%> <asp: content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" runat = "Server"> <p> Duration = "999999" SqlDependency = "VS2005_Test: sqlcache "VaryByParam =" none "<br/> if it is SqlServer2005, change it to SqlDependency =" CommandNotification <br/> note the configuration in the configuration file </p> <% = DateTime. now %> </p> </asp: Content>

SQL cache of the data source control:

The Cahce/SqlCachePage. aspx page code is as follows:

<% @ Page Language = "C #" MasterPageFile = "~ /Site. master "AutoEventWireup =" true "CodeFile =" sqlcacheperformancecontrol. aspx. cs "Inherits =" Cahce_SqlCacheDataSourceControl "Title =" SQL cache of data source controls "%> <asp: content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" runat = "Server"> <p> set the following attributes for the DataSource control: EnableCaching = "True" SqlCacheDependency = "VS2005_Test: sqlcache "CacheDuration =" Infinite "<br/> for SqlServer2005, change it to SqlDependency =" CommandNotification <br/> pay attention to the configuration in the configuration file </p> <% = DateTime. now %> </p> <asp: SqlDataSource ID = "sqlperformance1" runat = "server" EnableCaching = "True" SqlCacheDependency = "VS2005_Test: sqlcache "CacheDuration =" Infinite "ConnectionString =" <% $ ConnectionStrings: SqlConnectionString %> "SelectCommand =" SELECT * FROM [SqlCache] "> </asp: SqlDataSource> <asp: gridView ID = "GridView1" runat = "server" performanceid = "sqlperformance1" AllowSorting = "True"> </asp: GridView> </p> </asp: Content>

Related configurations in web. config:

<ConnectionStrings> <add name = "SqlConnectionString" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ VS2005_Test.mdf; Integrated Security = True; User Instance = True "providerName =" System. data. sqlClient "/> </connectionStrings> <system. web> <caching> <sqlCacheDependency enabled = "true" pollTime = "10000"> <databases> <add name = "VS2005_Test" connectionStringName = "SqlConnectionString"/> </ Databases> </sqlCacheDependency> <! -- If it is SqlServer2005, only the following settings are required, because SqlServer supports notification-based Cache invalidation <sqlCacheDependency enabled = "true"/> --> </caching> </system. web>

Note:For SQL Server 2005, the notification-based Cache is invalid. You do not need to set aspnet_regsql. You must set the attribute SqlDependency = "CommandNotification ". Before executing an SQL query for the first time, you must call the System. Data. SqlClient. SqlDependency. Start () method somewhere in the application. This method should be placed in the Application_Start () event of the global. asax file.

We hope that the cache technology described in this article will be helpful for your asp.net program design.


Several Methods for clearing page cache in ASPNET

When you use the dialog mode in asp.net, you will find that each time the page is opened with the same content, the page content is not refreshed, this is the reason for the cache,
The solution is as follows:
First, ASP. NET clears the page cache.
Response. Buffer = true;
Response. ExpiresAbsolute = System. DateTime. Now. AddSeconds (-1 );
Response. Expires = 0;
Response. CacheControl = "no-cache ";
Response. AddHeader ("Pragma", "No-Cache ");
The second is the HTML method <HEAD <META HTTP-EQUIV = "Pragma" CONTENT = "no-cache"
<MetaHTTP-EQUIV = "Cache-Control" CONTENT = "no-cache"
<META HTTP-EQUIV = "Expires" CONTENT = "0" </HEAD the third is to pass a parameter to the page when calling the original page again: href = "****. ASPX? Random ()"
The last option is to disable caching on the page.
The rational use of cache in web development can effectively improve the performance of the website. However, in some cases, the existence of cache may cause many problems.
For example, the cache may cause repeated data submission and the verification code image cannot be correctly displayed.
And so on. In this case, we need to disable the page cache function.
Our common practice is to send a no-cache command, but we found that this command is valid for IE but not Firefox, because, using this command, Firefox does not cache HTTPS pages, but will still cache HTTP pages. This is a BUG in Firefox. The solution is simple, that is, replacing no-cache with no-store, send no-store and no-cache commands at the same time
In ASP.net, add the following code to the page that does not require Caching:

Aspnet page Cache

1. It can be said that, But. net is stored in the memory, and it is stored locally.
2. Yes. The default cache for. net seems to last for nearly half an hour. I usually set it to 15 minutes.
3. Correct
4. There will be no difference, but. net loading is slow for the first time, but it is undeniable that pure static pages like htm will be faster.
---------------------------------------
1. I don't know what your site map is. If static links do not involve logical permissions, you can use static files.
2. server configuration is generally high, so execution of. net is definitely faster than local. The speed depends on the network speed of the client.

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.