ASP.NET cache technology

xiaoxiao2021-03-06  61

Introduction A large number of website pages is a dynamic manner, create a generated page according to different requests submitted by the user. As we know, dynamic pages help provide customized dynamic content according to user requirements. Dynamic pages are also beneficial to get information updated at a time in the database. The disadvantage is to generate the same page for each user request to add system overhead.

To overcome this problem, some websites generate HTML static pages for all pages with page generating engines. However, this generated page is the same as the request content of all users.

ASP.NET provides caching technology helps us to maximize this problem. It can cache the output page, saved in the memory, caches the contents of the user request. The characteristics of the cache can be customized according to the immersion.

Cache a page to cache a page output, we must specify a @outputcache command at the top of the surface. The syntax is as follows:

<% @ Outputcache duration = 5 varybyparam = "none"%>

As you can see, this instruction has two properties. they are:

Duration - The content valid period is valid until the cached output. After the time exceeds the specified validity period, the expired cache content will be deleted and the page generated cache content will be reused in the next user request. This process will be repeated after the cache is over 10 seconds. VarybyParam - This property is must, and indicates that the query string parameters make changes to the cache. In the above code snippet, we specify that the VaryByParam property is "None", which means that the page content returned regardless of how the collected query string parameters is the same. If two requests to the same page, different query strings are contained, such as ... / pagecachingByParam.aspx? Id = 12 and ... / pageCachingByParam.aspx? Id = 15, this should generate different page content, The instruction should be:

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

Each of the page content of the two different requests will be cached by the length of time specified by the DURATION property. .

In order to specify a plurality of parameters, use a semicolon to separate the parameter name. If we specify that the VaryByParam property is "*", the cache content will vary depending on the different query string parameters passing.

Some dynamic pages should generate different contents according to different browsing. In this case, it is necessary to specify the cache output to be different due to different browsers. @Outputcache command to be changed to:

<% @ OutputCache Duration = 5 VarybyParam = "ID" VarybyCustom = "Browser"%>

This instruction not only allows the cache output to be different due to the browser, but also different from the browser version, such as IE5, IE 6, Netscape 4, Netscape 6 gets different output cache versions.

Cache local page Sometimes we may just want to cache a small part of a page. For example, we may use this page to have the same content on all browsing users, this page has a title. This title may be a text / image composition, and data per day may change. In this case, we just want to cache this title day.

The solution is to put the title content into a user control, and then specify that this user control should be cached. This technique is called a partial cache (FRAGN Caching).

In order to specify the user control that should be cached, we use the @outputcache directive, just like the usage of the entire page cache.

<% @ Outputcache duration = 10 VarybyParam = "none"%> In the above instruction, the user control cache validity period is the time (10 seconds) specified by the DURATION property (10 seconds). Regardless of the query string and browser type / version, the content of the cache output is the same. .

The data cache ASP.NET also supports a cache for object type data. We can store objects in memory and use them in different dynamic pages in our application. This feature can be achieved using the Cache class. The survival cycle of the cache is the same as the application. Objects can be stored in the cache in the form of 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 ();

In order to put objects into the cache, you can use the ADD method of the Cache class or different versions of the INSERT method. These methods may allow us to use the more powerful feature provided by this Cache class, which is an overload using the INSERT method:

Cache.insert ("Name", Strname, New CachedPependency (Server.mappath ("Name.txt"), DateTime.now.addminutes (2), Timespan.zero;

The first two parameters are key names and inserted objects. The third parameter is the CacheDependency type, which helps us with the dependency of the Name.txt file settings. So once this file changes, this value in the cache will be deleted. We can specify "null" value to indicate that there is no dependency. The fourth parameter specifies the time of deleting this value from the cache. The last parameter is a parameter that caches expiration time, which indicates the time interval from the last access cache time to it is deleted.

When the system memory is not available, the cache automatically removes the use of less items from memory. This process is called purification. We can specify priority for projects in the cache so that some projects can be prioritized:

Cache.insert ("Name", Strname, New Cachedependency (Server.Mappath ("Name.txt"), DateTime.now.addminutes (2), Timespan.zero, CacheItemPriority.high, NULL;

Enumeration Type CacheItemPriority Sets different priorities. CacheItemPriority.high assigns a high priority to the project to reduce the possibility of cache deletion.

Points If you are still an old ASP page and use the response.expires property cache page output, then they can also be preserved as ASP.NET supports this property. The INSERT method for the Cache class will override any existing items with items of the same key name. CacheItemPriority.NotRemovable priority values ​​can be used with the cache.insert method to set up a project's priority so that this item will not be deleted from the cache during purification.

In this paper, I will provide a summary of ASP.NET's cache technology to provide a summary of optional convenience. In order to maintain the shortness of this article, there is no detailed explanation and description.

A local cache can be used in a sub-control nested style that enables cached. I haven't tested how to take advantage of this, so there is no explanation of this technique. At the same time, the INSERT method of the Cache class has not discussed here. I hope this article is a good start to study a colorful ASP.NET world. Description This sample source code is downloaded. Various cache technology instances are provided in the source code. You can see a list of all examples using the Index.htm page.

转载请注明原文地址:https://www.9cbs.com/read-119149.html

New Post(0)