Set page cache in source code
@ OutputCache: control the output cache policies of user controls contained in ASP. NET pages or pages in a declarative manner.
Main attributes:
- Duration: the cache time for pages or user controls (in seconds ). Set this attribute on the page or user control to create an expiration Policy for the HTTP response from the object and automatically cache the page or user control output. This is a required attribute. If this attribute is not included, a analyzer error occurs.
- Location: Specifies a valid value to control the Location of the resource's output Cache HTTP response. The default value is Any. The @ OutputCache directive contained in the user control (. ascx file) does not support this attribute.
Member name |
Description |
| Any |
The output cache can be located on the browser client that generates the request, the proxy server that participates in the request (or any other server), or the server that processes the request. |
| Client |
The output cache is located on the browser client that generates the request. |
| Downstream |
The output cache can be stored on any HTTP 1.1 cacheable device, except for the source server. This includes the proxy server and the client sending the request. |
| Server |
The output cache is located on the Web server that processes the request. |
| None |
Disable the output cache for the requested page. |
| ServerAndClient |
The output cache can only be stored on the source server or the client sending the request. The proxy server cannot cache the response. |
Example:
- Add the following code to the page source code. The current page is cached for 100 seconds.
<% @ OutputCache Duration = "100" VaryByParam = "none" %>
- In the cs code, add the following code below using, and cache the current page for 100 seconds.
[PartialCaching (100)]
- Adjust the page cache in the user control in the cs code.
// The test. ascx user control sets the page cache.
PartialCachingControl pcc = LoadControl ("test. ascx") as PartialCachingControl;
// If the page cache in the control is greater than 10 seconds
If (pcc. CachePolicy. Duration> TimeSpan. FromSeconds (10 ))
{
// Set the page cache in the control to 20 seconds
Pcc. CachePolicy. SetExpires (DateTime. Now. Add (TimeSpan. FromSeconds (20 )));
// Set whether the cache and call are absolutely expired
Pcc. CachePolicy. SetSlidingExpiration (false );
}
// Add the control to the page
Controls. Add (pcc );
- Populate the GridView with the page data cacheCode
Protected void Page_Load (object sender, EventArgs e)
{
// Determine whether the cache contains "key" data. If yes, read the data. Otherwise, read the data in the xml file.
DataSet ds = new DataSet ();
If (Cache ["key"] = null)
{
Ds. ReadXml (Server. MapPath ("~ /XMLFile. xml ");
This. GridView1.DataSource = ds;
This. GridView1.DataBind ();
}
Else
{
Ds = (DataSet) Cache ["key"];
This. GridView1.DataSource = ds;
This. GridView1.DataBind ();
}
}
Protected void DisplayCacheInfo ()
{
// Number of caches
String cacheCount = Cache. Count. ToString ();
// Enumerate cache
IDictionaryEnumerator cacheEnum = Cache. GetEnumerator ();
// Print the name of each cache on the screen
While (cacheEnum. MoveNext ())
{
This. Label1.Text + = cacheEnum. Key. ToString ();
}
}
Protected void Buttonadd_Click (object sender, EventArgs e)
{
// Add cache
If (Cache ["key"] = null)
{
DataSet ds = new DataSet ();
Ds. ReadXml (Server. MapPath ("~ /XMLFile. xml ");
Cache. Insert ("key", ds, new System. Web. Caching. CacheDependency (Server. MapPath ("XMLFile. xml ")));
}
DisplayCacheInfo ();
}
Protected void Buttonselect_Click (object sender, EventArgs e)
{
// Retrieve cache
If (Cache ["key"]! = Null)
DisplayCacheInfo ();
}
Protected void Buttondelete_Click (object sender, EventArgs e)
{
// Remove the cache
If (Cache ["key"]! = Null)
{
Cache. Remove ("key ");
}
DisplayCacheInfo ();
}