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