Three policies for Cache expiration in ASP. NET Cache

Source: Internet
Author: User

We add three buttons on the page and double-click the button to create an event handling method. The three buttons use different expiration policies to add ASP. NET cache.

 
 
  1. <Asp: Button ID="Btn_InsertNoExpirationCache" Runat="Server" Text="Insert cache Never Expires"
  2. OnClick="Btn_InsertNoExpirationCache_Click" /> 
  3. <Asp: Button ID="Btn_InsertAbsoluteExpirationCache" Runat="Server" Text= "Insert absolute time
  4. Expired cache"OnClick="Btn_InsertAbsoluteExpirationCache_Click" /> 
  5. <Asp: Button ID="Btn_InsertSlidingExpirationCache" Runat="Server" Text= "Insert change time
  6. Expired cache"OnClick="Btn_InsertSlidingExpirationCache_Click" /> 

The three button Click events are handled as follows:

 
 
  1. protected void btn_InsertNoExpirationCache_Click(object sender, EventArgs e)  
  2. {  
  3.     DataSet ds = GetData();  
  4.     Cache.Insert("Data", ds);  
  5. }  
  6. protected void btn_InsertAbsoluteExpirationCache_Click(object sender, EventArgs e)  
  7. {  
  8.     DataSet ds = GetData();  
  9.     Cache.Insert("Data", ds,null, DateTime.Now.AddSeconds(10), TimeSpan.Zero);  
  10. }  
  11. protected void btn_InsertSlidingExpirationCache_Click(object sender, EventArgs e)  
  12. {  
  13.     DataSet ds = GetData();  
  14.     Cache.Insert("Data", ds, null, DateTime.MaxValue, TimeSpan.FromSeconds(10));  

Let's analyze the three ASP. NET cache expiration policies.

◆ Never expire. Directly assign the cached Key and Value.

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

◆ The change time expires smoothly ). DateTime. MaxValue indicates that the absolute time expiration policy is not used, and TimeSpan. FromSeconds (10) indicates that the cache expires after 10 seconds of no access.

Here, we use the Insert () method to add cache. In fact, the Cache also has an Add () method to Add items to the Cache. The difference is that the Add () method can only Add items not included in the cache. If an existing item in the cache is added, it will fail but will not throw an exception), and Insert () the method can overwrite the original items.

Note: Unlike the Application, you do not need to use the lock operation when inserting the ASP. NET Cache. The Cache will process concurrency by itself.

  1. Analysis on ASP. NET runtime environment Establishment
  2. ASP. NET Overview
  3. Analysis on the advantages of ASP. NET in eleven aspects
  4. Analysis on ASP. NET database connection pool settings
  5. How to Learn the nine steps of ASP. NET

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.