1. Replace Cache
You can use substitution to dynamically update the cache page.
[Csharp]
Public static string GetTime (HttpContext context)
{
Return DateTime. Now. ToString ();
}
Aspx
[Html]
Cached Time: <% = DateTime. Now. ToString () %>
<Br/> Page Time:
<Asp: Substitution ID = "Substitution1" runat = "server" MethodName = "GetTime"/>
Finally, enable cache output on the page.
Result: The Page Time is different each Time. The cached Time is updated every 5 seconds.
Tips: Disable cache output
[Csharp]
This. Response. Cache. SetNoServerCaching ();
2. Data Dependency
Depending on the database page and associated with the corresponding SQL statement, set the cache method:
[Html]
<% @ Outputcache duration = "5" varybyparam = "none" SqlDependency = "CommandNotification" %>
Once the page is cached, if DML is used to operate the data source, she will send a notification to the dependent server. After the server receives the notification, it will remove the original query page from the cache.
If any data source on the page does not need to be cached, you can bypass SqlDependency and use the AddCacheDependency code for the required query statement as follows:
[Csharp]
Using (SqlConnection conn = new SqlConnection (connectionString ))
{
String SQL = @ "SELECT U. UserId,
U. UserName,
D. BorrowDate,
D. BorrowMoney,
D. RevertDate,
D. IsRevert
FROM dbo. UserInfo AS U
Inner join dbo. Debt AS D
On u. UserId = D. UserId ";
Conn. Open ();
SqlCommand sqlCmd = new SqlCommand (SQL, conn );
SqlCacheDependency dep = new SqlCacheDependency (sqlCmd );
GridView1.DataSource = sqlCmd. ExecuteReader ();
GridView1.DataBind ();
This. Response. AddCacheDependency (dep );
}
Note that the following code must be added to the Global. asax file:
[Csharp]
Void Application_Start (object sender, EventArgs e)
{
// Code that runs on application startup
System. Data. SqlClient. SqlDependency. Start (ConfigurationManager. deleetpipeline ["DebtDB"]. ToString ());
}
Void Application_End (object sender, EventArgs e)
{
// Code that runs on application shutdown
System. Data. SqlClient. SqlDependency. Stop (ConfigurationManager. deleetpipeline ["DebtDB"]. ToString ());
}
Result: When the current page request is executed, the generated page is placed in the output cache. If DML is used to update, delete, and insert the data source
[Html]
<% @ OutputCache Duration = "10" VaryByParam = "None" VaryByCustom = "userId" %>
A notification will be received in sequence,
Remove the page from the cache. Refresh the page and you will see the latest information.
3. Use varyByCustom to cache pages
The following code uses query strings to cache pages. You can cache pages using cookies and browsers.
[Html]
<Div>
Cache time: <% = DateTime. Now. ToString () %>
UserId:
<% = UserId %>
<Br/>
<Asp: GridView ID = "GridView1" runat = "server">
</Asp: GridView>
</Div>
Query string: userId
[Html]
<% @ OutputCache Duration = "10" VaryByParam = "None" VaryByCustom = "userId" %>
[Csharp]
Protected string userId;
Public readonly string connectionString = ConfigurationManager. deleetask[ "DebtDB"]. ToString ();
Protected void Page_Load (object sender, EventArgs e)
{
If (Request. QueryString ["userId"]! = Null)
{
UserId = Request. QueryString ["userId"]. ToString ();
}
Using (SqlConnection conn = new SqlConnection (connectionString ))
{
String SQL = @"
Select u. UserId,
U. UserName,
D. BorrowDate,
D. BorrowMoney,
D. RevertDate,
D. IsRevert
FROM dbo. UserInfo AS U
Inner join dbo. Debt AS D
On u. UserId = D. UserId
Where d. UserId = "+ userId;
Conn. Open ();
SqlCommand sqlCmd = new SqlCommand (SQL, conn );
GridView1.DataSource = sqlCmd. ExecuteReader ();
GridView1.DataBind ();
}
}
Note that we need
Override GetVaryByCustomString
If you do not re-write, you cannot guarantee that the corresponding information is filtered out based on the UserId query for caching.
For example, when I first query the related information of UserId: 1, and then click the related information of Userid: 2, it is exactly the same. The corresponding userid: 2 information is displayed only when the cache expires.
[Csharp]
Public override string GetVaryByCustomString (HttpContext context, string custom)
{
If (custom = "userId ")
{
Return context. Request. QueryString ["userId"]. ToString ();
}
Return base. GetVaryByCustomString (context, custom );
}