ASP. NET data cache

Source: Internet
Author: User
Note: This article is from the network.
What is ASP. NET data cache learning? How to use ASP. NET data cache? Before talking about ASP. NET data caching, you should first talk about using parameter caching on the page. As mentioned earlier, cache settings Varybyparam = "NONE" If there is no parameter, you can also set varybyparam. The set parameter corresponds to the query string value sent with the get method attribute, or to the parameter 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 Value Including none, asterisks (*), and any valid query string or post parameter names . Simply put, it is set to the querystring name we use on the page.
Let's look at an example:
< % @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Date. aspx. CS " Inherits = " Date " % >
< % @ Outputcache duration = " 60 " Varybyparam = " Customerid " % >

<Asp: hyperlink ID = " Hyperlink1 " Runat = " Server " Navigateurl = " ~ /Date. aspx? Cu Stomerid = 16 " > 16 < / ASP: hyperlink>
<Asp: hyperlink ID = " Hyperlink2 " Runat = " Server " Navigateurl = " ~ /Date. aspx? Cu Stomerid = 19 " > 19 < / ASP: hyperlink>
Click "16" and "19" respectively after the operation to select different data based on the two keywords. At this time, two cache pages will be created based on the two parameters we passed, remember the displayed time after each keyword is clicked, and refresh it repeatedly to see if the time has changed! Now let's talk about data caching.

ASP. NET data cache (data caching)

In the system. Web. caching namespace, there is a class "cache". We can cache data through this class.

The simplest cache method is cache ["mycachestring"] = "My csdn blog !!! "; Create a cache by assigning values, and then retrieve the cache by assigning values: mylabel. TEXT = cache ["mycachestring"]. tostring (); this method is very simple to use, but the function is limited, in orderThe cache. insert () method should be used for better cache customization.For example:

You only need to put the gridview in the page.

ASP. NET data cache parameter description

Cache. insert (string, object, cachedependency, datetime, timespan) 1 is the cache name, 2 is the cached data object, 3 is the cache key dependency, usually null, 4 is the expiration time. If the relative expiration time is used, it is set to noabsoluteexpiration, and 5 is the adjustable expiration time. If parameter 4 uses a fixed expiration time, this parameter must be set to noslidingexpiration. Oh, isn't it a bit dizzy? Here are two examples to illustrate the expiration time problem.

Cache. insert ("ds", DV, null, datetime. Now. addminutes (5), system. Web. caching. cache. noslidingexpiration );
In this exampleAfter the cache is created, it will expire in 5 minutes.
Cache. insert ("ds", DV, null, system. Web. caching. cache. noabsoluteexpiration, timespan. fromminutes (5 ));

In this example, after the cache is created,The expiration time is adjustable.For example, the cache expiration time set for seconds should be, but if someone accesses the cache at, the expiration time will be adjusted to, and so on ......

Let's create a test in vs2005 to check the performance changes before and after cache usage! No. It took 0.43 seconds before the cache was completed, and 0.08 seconds after the cache was used, the performance was more than 5 times different !!!

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.