Asp. NET cache three strategies that expire cache

Source: Internet
Author: User

Original: ASP. NET cache three strategies that expire cache

We add three buttons on the page and double-click the button to create an event-handling method, and three buttons add the ASP. NET cache with a different expiration policy.



<asp:button id= "Btn_insertnoexpirationcache" runat= "server" Text= "Insert never expire cache"
onclick= "Btn_insertnoexpirationcache_click"/>
<asp:button id= "Btn_insertabsoluteexpirationcache" runat= "server" text= "Insert absolute time
Expired Cache "onclick=" Btn_insertabsoluteexpirationcache_click "/>
<asp:button id= "Btn_insertslidingexpirationcache" runat= "server" text= "Insert Change time
Expired Cache "onclick=" Btn_insertslidingexpirationcache_click "/>

The Click event of the three buttons is handled as follows:


protected void Btn_insertnoexpirationcache_click (Objectsender, EventArgs e)
{
DataSet ds = GetData ();
Cache. Insert ("Data", DS);
}
protected void Btn_insertabsoluteexpirationcache_click (object sender, EventArgs e)
{
DataSet ds = GetData ();
Cache.Insert ("Data", Ds,null, DateTime.Now.AddSeconds (Ten), TimeSpan.Zero);
}
protected void Btn_insertslidingexpirationcache_click (object sender, EventArgs e)
{
DataSet ds = GetData ();
Cache.Insert ("Data", DS, NULL, DateTime.MaxValue, Timespan.fromseconds (10));
}

Let's analyze these three kinds of ASP. NET Cache expiration policy.

Never expire. The Key and value of the direct assignment cache are

Absolute time expires. DateTime.Now.AddSeconds (10) indicates that the cache expires after 10 seconds, and TimeSpan.Zero indicates that the smoothing expiration policy is not used.

Change time expires (smooth expiration). DateTime.MaxValue indicates that the absolute time expiration policy is not used, and Timespan.fromseconds (10) indicates that the cache expires for 10 seconds without access.

Here, we all use the Insert () method to add the cache. In fact, the cache also has an Add () method that can also add items to the buffer. The Add () method can only add items that are not in the cache unless an existing entry in the cache fails (but does not throw an exception), and the Insert () method overwrites the original item.

Note: Unlike application , there is no need to use lock operations when inserting the ASP, and the cache handles concurrency on its own.

Asp. NET cache three strategies that expire cache

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.