Asp. NET's caching technology

Source: Internet
Author: User
Tags datetime insert
Introduction to Asp.net| Caching
A large number of site pages are dynamic, creating a build page based on different requests submitted by the user. As we know, dynamic pages help provide customized dynamic content based on user requirements. Dynamic pages are also good for getting updated data in the database every moment. The disadvantage is that generating the same page for each user request increases the system overhead.

To overcome this problem, some sites use the page generation engine to generate HTML static pages for all pages. However, the resulting page is the same for all user requests.

asp.net provides caching technology to help us solve this problem to the greatest extent possible. It can cache the output page, save it in memory, and cache the user's requested content. The characteristics of caching can be customized according to the pattern.

Caching a page
To cache the contents of a page output, we specify a @OutputCache command at the top of the front and the head. The syntax looks like this:

<%@ OutputCache duration=5 varybyparam= "None"%>

As you can see, this instruction has two attributes. They are:

Duration-How many seconds the content of the cached output is valid. After the time exceeds the specified validity period, the expired cache content is deleted and the page generation cache content is called back in the next user request. The process repeats again after 10 seconds of cached content.
VaryByParam-This attribute is required and indicates a different query string parameter that causes the cache to change. In the code snippet above, we specify that the VaryByParam property is "None", which means that the page content returned is the same regardless of how the query string parameters are passed.
If two requests for the same page contain different query string parameters, such as: .../pagecachingbyparam.aspx?id=12 and .../pagecachingbyparam.aspx?id=15, this should generate different page content, The instructions should be:

<%@ OutputCache duration=10 varybyparam= "id"%>

Each of the page contents for such two different requests will be specified by the cached Duration attribute for the length of time.

To specify a situation for multiple parameters, use semicolons to separate the parameter names. If we specify that the VaryByParam property is "*", the cached content will vary depending on the parameters of the different query string being passed.

Some dynamic pages are based on different browsing to generate different content. In this case, it is necessary to specify that the cached output is different for different browsers. @OutputCache command to read:

<%@ OutputCache duration=5 varybyparam= "id" varybycustom= "browser"%>

This instruction not only makes the cached output different from the browser, but also varies depending on the browser version, such as IE5, IE 6, Netscape 4, and Netscape 6, which have cached versions of different outputs.


Caching local pages
Sometimes we may just want to cache a small portion of a page. For example, we might use a page like this to have the same content for all browsing users, and this page has a title. This title may be a text/image composition, and the data may change every day. In this case, we will only want to cache the expiration date of the title day.

The solution is to put the header content into a user control and then specify that the user control should be cached. This technique is called local caching (fragment caching).

In order to specify the user controls that should be cached, we use @OutputCache directives, just as the entire page cache uses.

<%@ OutputCache duration=10 varybyparam= "None"%>

In the instructions above, the user control cache expiration is the time specified by the Duration property (10 seconds). The contents of the cached output are the same regardless of query string and browser type/version ...


Data caching
ASP.net also supports caching as object type data. We can store objects in memory and use them in different dynamic pages of our application. This feature can be achieved by using the Cache class. The lifetime of the cache is the same as for the application. objects can be stored in the cache in the form of key value pairs (name value pairs). Insert a string into the cache as shown below:

cache["name"]= "Smitha";

This stored string value can be obtained like this:

if (cache["name"]!= null)
label1.text= cache["Name". ToString ();

To insert objects into the cache, you can use the Add method of the cache class or a different version of the Insert method. These methods may allow us to use the more powerful features provided by this Cache class, and the following is an overload that takes advantage of the Insert method:

Cache.Insert ("Name", StrName,
New CacheDependency (Server.MapPath ("Name.txt"),
DateTime.Now.AddMinutes (2), TimeSpan.Zero);

The first two parameters are the key name and the object being inserted. The third parameter is the CacheDependency type, which helps us to set the dependency on the value of the file named Name.txt. So once this file has changed, the value in the cache will be deleted. We can specify a "null" value to indicate that there are no dependencies. The fourth parameter specifies when the value is deleted from the cache. The last parameter is an adjustable cache expiration parameter that indicates the interval from the last access cache time to the time it was deleted.

When the available system memory is low, the cache automatically deletes the less-used items from memory. This process is called purification (scavenging). We can assign priorities to items added to the cache so that some items are prioritized:

Cache.Insert ("Name", StrName,
New CacheDependency (Server.MapPath ("Name.txt"),
DateTime.Now.AddMinutes (2), TimeSpan.Zero,
Cacheitempriority.high, NULL);

Enumeration type CacheItemPriority sets a different priority for the member. Cacheitempriority.high assigns a high priority to the project so that the project is less likely to be cached for deletion.


Points
If you are still an old ASP page and use the Response.Expires property to cache page output, they can also be retained as ASP.net supports this property.
The Insert method of the Cache class overwrites any existing item with an item of the same key name.
Cacheitempriority.notremovable precedence can be used with the Cache.Insert method to set the priority of a project so that the item is not deleted from the cache during purification

Conclusion
In this article, I try to provide a general overview of the ASP.net caching technology in as many options as possible. No detailed explanations and instructions are provided to keep the brief summary of this article.

Local caching is available in a nested style with child controls that enable caching. I have not tested how to use this, so there is no explanation for this technique. At the same time the Insert method of the Cache class is not discussed here. I hope this article is a good beginning for the reader to study the colorful asp.net world.

Description
This article sample source download. Various caching technology instances are provided in the source code. Use the index.htm page to see a list of all the examples.


History
February, 2004-first version



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.