. NET page data cache

Source: Internet
Author: User

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.
  • CacheProfile: name of the cache settings associated with this page. This is an optional property. The default value is a null string (""). The @ OutputCache directive contained in the user control (. ascx file) does not support this attribute. When this attribute is specified on the page, the attribute value must match the name of an available item in the outputCacheProfiles element in the outputCacheSettings section. If the name does not match the configuration file, an exception is thrown.
  • NoStore: A boolean value that determines whether to block the second-level storage of sensitive information. Setting this property to true is equivalent to executing the following code during the request: Response. cache. setNoStore (); included in the user control (. the @ OutputCache command in the ascx file does not support this attribute.
  • Shared: A boolean value to determine whether the output of a user control can be Shared by multiple pages. The default value is false. The @ OutputCache directive contained in the ASP. NET page (. aspx file) does not support this attribute.
  • SqlDependency: the string value that identifies a group of database/table name pairs. The output cache of pages or controls depends on these name pairs. Optional values: database/table name pair and CommandNotification. The CommandNotification value of the SqlDependency attribute is valid only on the webpage (. aspx. The user control can only use table-based round robin For the @ OutputCache command.
  • VaryByCustom: any text required by the custom output cache. If the value of this attribute is browser, the cache will vary with the browser name and major version information. If you enter a custom string, you must overwrite the GetVaryByCustomString method in the Global. asax file of the application.
  • VaryByHeader: a semicolon-separated HTTP header list, used to change the output cache. When this attribute is set to multiple headers, the output cache contains a different version of the Request Document for each specified header combination.

    Setting the VaryByHeader attribute will enable cache items in all HTTP 1.1 caches, not just in ASP. NET caches. The @ OutputCache command in the user control does not support this attribute.

  • VaryByParam: a semicolon-separated string list used to change the output cache. By default, these strings correspond to the query string values sent with the GET method attribute, or to parameters sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the Request Document for each specified parameter combination. Possible values include none, asterisks (*), and any valid query string or POST parameter names. This attribute or VaryByControl attribute is required when @ OutputCache command is used on ASP. NET pages and user controls. If it is not included, an analyzer error occurs. If you do not want to change the cache content by specifying parameters, set the value to none. If you want to change the output cache by using all parameter values, set the attribute to asterisk (*)).
  • VaryByControl: a semicolon-separated string list used to modify the output cache of a user control. These strings represent the ID attribute values of the ASP. NET Server Control declared in the user control. This attribute or VaryByParam attribute is required when @ OutputCache command is used on ASP. NET pages and user controls.
  • VaryByContentEncodings: a semicolon-separated string list used to change the output cache. Apply the VaryByContentEncodings attribute to the Accept-Encoding header to determine how different content codes can obtain cache responses.

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 cache

    Code
    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 ();
    }

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.