OutputCache attributes (III)-VaryByHeader, VaryByCustom, and outputcache

Source: Internet
Author: User

OutputCache attributes (III)-VaryByHeader, VaryByCustom, and outputcache

Directory

  • OutputCache Concept Learning

  • OutputCache attributes (1)

  • OutputCache attributes (2)

  • OutputCache attributes (III)

 

VaryByHeader:A semicolon-separated HTTP header list used to change the output cache. When this feature is set to multiple headers, the output cache contains a different version of the Request Document for each specified header combination.

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

The configuration file and page for preparing the test code are as follows:

<System. web> <caching> <outputCacheSettings> <outputCacheProfiles> <! -- Name cache configuration name duration cache time (in seconds) enabled indicates that the cache is valid --> <add name = "outputCache60" duration = "60" enabled = "true" varyByParam = "none" location = "Any" varyByHeader = "User-Agent" /> </outputCacheProfiles> </outputCacheSettings> </caching> <compilation debug = "true"/> </system. web>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><%@ OutputCache CacheProfile="outputCache60"  %>


Open Firefox and IE to visit this page. We can see that the User-Agent in the HTTP request headers of Firefox and IE are inconsistent, as shown below:
The results of the two browsers are also inconsistent, as shown below:

We modify the parameters and use the Host parameter in HttpHeader as follows:

<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any"  varyByHeader="Host"/>

When the Host data in the HttpHeader requests from the two browsers is consistent, the browser data results can also be consistent:

 

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.

Google, IE, and Firefox browsers are used for testing. The Accept-Encoding of these three browsers is as follows:
Google: Accept-Encoding: gzip, deflate, sdch

IE: Accept-Encoding gzip, deflate

Firefox: Accept-Encoding gzip, deflate

Modify the configuration file as follows:

<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any"  varyByContentEncoding="sdch"/>

Enter the test address in the three browsers. Refresh the address and we will find that Firefox and IE are consistent in Data Reading cached data, while Google's data remains changing.

If we set the configuration file:

<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any"  varyByContentEncoding="sdch;gzip"/>

The cache of the three browsers will become invalid.

 

VaryByCustom indicates 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.

<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any"  varyByCustom="browser"/>

The test is performed on two machines. The Firefox version is 32.0.1 IE8 and the Firefox version is 32.0.1 IE9, as shown in, the Firefox cache data remains consistent, however, the IE data is not consistent (the version is different ).

If you enter a custom string, you must overwrite the GetVaryByCustomString method in the Global. asax file of the application. As shown in the Code:

<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByCustom="UserHostName"/>

The Global. asax file is added as follows:

    public override string GetVaryByCustomString(HttpContext context, string custom)    {                if (string.Equals(custom, "UserHostName", StringComparison.OrdinalIgnoreCase))        {            return Context.Request.UserHostName;        }        return base.GetVaryByCustomString(context, custom);    }

If we remove the varyByCustom = "UserHostName" code, when multiple clients access this page at the same time, the output content of multiple clients is consistent, however, when the custom cache mode varyByCustom = "UserHostName" is used, the cache of each client is differentiated by the UserHostName of their requests. The effect is as follows:

About varyByCustom recommended forum you can see: http://bbs.csdn.net/topics/390667018

 

VaryByControl gets or sets control identifiers separated by group numbers. These identifiers are contained on the current page or user control and used to change the current cache item. The cache dependency and control value of the current page, as shown in the following code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <% @ OutputCache Duration =" 60 "VaryByControl =" slt_VaryByControl "%> 

When we switch the slt_VaryByControl value, the cache mechanism outputs the new page or cache Page Based on the selected value.


VaryByHeader, VaryByContentEncodings, VaryByCustom, and VaryByControl are written here. If you have any questions, please correct them.

 

Author: Sakya bitter monk Source: http://www.cnblogs.com/woxpp/p/3980851.html this article copyright belong to the author and blog park a total, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection.

 


Briefly describe what do the Duration, VaryByParam, and VaryByControl attributes of the output cache @ OutputCache page command mean?

The control cache and the @ OutputCache command of the page output cache have both similarities and differences. The similarities between the two are that they are basically the same setup method, both of which are the @ OutputCache instruction strings with attributes set at the top of the file. There are two differences: first, the @ OutputCache command of the control cache is set in the user control file, and the @ OutputCache of the page output cache is set in the common ASP. NET file. Second, the @ OutputCache command in the control cache can only set six attributes: Duration, Shared, SqlDependency, VaryByControl, VaryByCustom, and VaryByParam. The @ OutputCache instruction string in the page output cache has up to 10 attributes. The preceding problems must be noted when setting the control cache. The following lists some examples of using the @ OutputCache command to set the control cache, with emphasis on Attribute applications such as VaryByParam and VaryByControl.

The @ OutputCache command in the user control sets the source code.

<% @ OutputCache Duration = "120" VaryByParam = "CategoryID; SelectedID" %>

The code above sets the cache validity period of the user control to 120 seconds, and allows you to use the CategoryID and SelectedID parameters to change the cache. By setting the VaryByParam attribute, instances of multiple user controls may be stored in the server cache. For example, for a page containing a user control, the following URL link may exist.

URL link of the page containing the user control

Localhost/... edid = 0
Localhost/... edid = 1

When you request a page with the above URL address, because of the @ OutputCache instruction setting in the control, especially the setting of the attribute VaryByParam, then, two versions of user control cache instances are stored in the server cache.

In addition to the VaryByParam attribute described above, the control cache settings also support the VaryByControl attribute. The VaryByParam attribute changes the cache based on the name/value pairs sent using POST or GET, while the VaryByControl attribute changes the cache through the server control contained in the user control file. The following is the sample code of the application of the VaryByControl attribute.

The @ OutputCache command in the user control sets the source code.

<% @ OutputCache Duration = "120" VaryByParam = "none" VaryByControl = "Category" %>

The above Code sets the cache validity period to 120 seconds, and the page does not change with any GET or POST parameters (even if the VaryByParam attribute is not used, it still needs to be explicitly declared in the @ OutputControl command ). If the user control contains the server control whose ID attribute is "Category" (for example, the drop-down box control ...... the remaining full text>
 
ASPNET and OutputCache cache Problems

Refer to Microsoft's MSDN,
OutputCache support
VaryByControl = "controlname"
VaryByCustom = "browser | customstring"
VaryByHeader = "headers"
VaryByParam = "parametername"
VaryByContentEncoding = "encodings"

You can use VaryByCustom,
Indicates 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.

Related Article

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.