简体   繁体   中英

caching a page with data from database asp.net

I have a main page with categories of items. The categories of items come from database. But the data does not changes frequently. So what I want is to cache the page and after one month or so, the data should get updated with fresh data coming from db. I know in asp.net there is output cache and data cache.

How can I achieve the above functionalities?

I know there is

<%@ OutputCache %>

that can be used for caching a page.

Any information you provide will be useful to me but I am looking for expert opinion.

I know it's too broad a topic, but it is very useful thing, so it should not be flagged.

One of the best approach can be the use of an advanced feature of ASP.NET output caching SqlCacheDependency Class, to Output Caching direct with the SQL Server.

You can configure SQL Server and ASP.NET to cache page requests, reducing server workload, until the data on which the page depends has been updated in SQL Server. SQL cache dependency is useful for data that remains comparatively static.

http://msdn.microsoft.com/en-us/library/e3w8402y(v=vs.100).aspx

You will need to follow some steps :

First, you will need to enable cache notification for SQL Server, something like :

aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees

Then, configure the Web page for caching, something like :

<%@ OutputCache Duration="3600" SqlDependency="Northwind:Employees" VaryByParam="none" %>

Then, you will need to specify caching details in the Web.config file, something like :

<!-- caching section group -->
<caching>
  <sqlCacheDependency enabled = "true" pollTime = "1000" >
    <databases>
      <add name="Northwind" 
         connectionStringName="NorthwindConnectionString1"
         pollTime = "1000"
      />
    </databases>
  </sqlCacheDependency>
</caching>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM