Simple learning of ASP. NET cache applications

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 cache.

<Asp: button id = "btn_insertnoexpirationcache" runat = "server" text = "insert cache Never Expires"
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 three button click events are handled as follows:

protected void btn_insertnoexpirationcache_click (Object sender, eventargs e)
{< br> dataset DS = getdata ();
cache. insert ("data", DS);
}< br> protected void btn_insertabsoluteexpirationcache_click (Object sender, eventargs e)
{< br> dataset DS = getdata ();
cache. insert ("data", DS, null, datetime. now. addseconds (10), timespan. zero);
}< br> protected void btn_insertslidingexpirationcache_click (Object sender, eventargs e)
{< br> dataset DS = getdata ();
cache. insert ("data", DS, null, datetime. maxvalue, timespan. fromseconds (10);
}

Let's analyze the three expiration policies.
· never expire. You can directly assign values to the cached key and value
· absolute time expiration. Datetime. Now. addseconds (10) indicates that the cache expires after 10 seconds, and timespan. Zero indicates that the smooth expiration policy is not used.
· change time expired (smooth expiration ). 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 in the cache. If an existing item in the cache is added, it will fail (but will not throw an exception), while insert () the method can overwrite the original items.
Note: Unlike the application, you do not need to use the lock operation when inserting the cache. The cache will process the concurrency by itself.

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.