. NET4 Cache Expiration Policy excerpt

Source: Internet
Author: User
Tags dsn httpcontext

The following is the online search for information, for reference only:

Information One:
Asp. NET cache with cache expiration of three strategies (go from 51CTO)

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 using a different expiration policy.

  1. <asp:button ID="Btn_insertnoexpirationcache" runat= "Server" Text="Insert never expire cache" "
  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 Click event of the three buttons is 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 (Ten), 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 (Ten));
  15. }

Let's examine these three types of ASP. NET cache expiration policies.

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 difference is that the Add () method can only add items that are not in the cache, and if adding an existing item in the cache fails (but does not throw an exception), 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.

Source URL: http://developer.51cto.com/art/200907/140170.htm

Information Two: (personally think that this is more comprehensive)

caching techniques in. NET:  Caching is a technique that is widely used in computing to raise high performance, and it stores data with high frequency or high construction costs in memory.   in web development, performance is an important factor in the quality of your application, and caching is a great help in improving performance. Let's take a look at the problem that the cache can solve:  1. Performance-stores the data to avoid duplication of data creation, processing, and transmission, effectively improving performance. such as the cache of unchanged data, such as the list of countries, which can significantly improve the response speed of web programs;  2. Stability--a common application that often occurs when multiple requests are made to the same data, logical functions, and user interface. When the user base is very large, if each request is processed, the resources consumed is a great waste, but also cause system instability.  3. Availability-Sometimes, services that provide data information may stop unexpectedly, and if you use caching technology, you can still provide support to end users for a certain amount of time, improving the availability of your system.   Then let's take a look at the cache in asp: 1. In ASP. NET, a cache object dedicated to caching data is provided, which is scoped to the application domain. The lifetime is closely related to the application, and the cache object is recreated whenever the application starts. The main difference between it and the Application object is that it provides features specifically for cache management, such as dependency and expiration policies.  2. The cache object is defined in the System.Web.Caching namespace, and you can use the Cache property of the HttpContext class or the Cache property of the Page object to get a reference to the cache object that can store the. NET Framework in addition to storing key-value pairs.   Cache is to keep the data in memory for the next call, but after the data is updated, the in-memory data is not updated, still the previous data, how to make the cached data also updated? We adopt dependency and expiration policies. There are three ways to do this: 1. File dependency 2. Key-value-dependent (key dependency)  3. Time-based expiration policy   First we look at file dependencies. File dependencies are forcing the removal of cached data when some (some) of the files on the hard disk change. Example:  cachedependency dep = new CacheDependency (Server.MapPath ("File.xml"));  Cache.Insert ("Key", "value", DEP Note: file dependencies are by using Cache.Insert andAdded to the CacheDependency object that references the XML file.   Key-value dependency, which specifies that a data item in the cache is removed when it changes. For example, we have Key1 and Key2 two cache, Key2 is the key value depends on Key1, but key1 changes, Key2 will fail. Example://Create Key1 cache cache["Key1"]= "Key1 value";//Is cache Key2 key value depends on key1string[] dependencykey=new string[1];d ependencykey[0]= " Key1 "; CacheDependency dep=new CacheDependency (Null,dependencykey); Cache.Insert (" Key2 "," value2 ", DEP);  A time-based expiration policy that invalidates data according to a predefined time policy, which can be an absolute time (such as 18:00 of a date) or relative to the current time. It is not possible to use too short and too long an expiration time, not to create cached data that is not used, to cache stale data and to increase the burden of caching, so high concurrency tests can be used to determine the best value for the expiration time. Example://Absolute time expiresCache.Insert ("DSN", connectionstring, NULL, Datetime.now.addminutes (2), system.web.caching.cache.noslidingexpiration);Relative time expires (i.e. "sliding expiration" or "smooth expiration" as mentioned above)Cache.Insert ("DSN", connectionstring, NULL, System.web.caching.cache.noabsoluteexpiration, Timespan.fromseconds (Ten));


Similar wording:
Context. Cache.Insert (CacheKey, temp, DP, DateTime.MaxValue, TimeSpan.Zero);

---------------------------------------2013-7-10 begin---------------------------------------
[MethodImpl (methodimploptions.synchronized)]//Implement thread synchronization for this method
public void Testcache ()
{
Add
System.Web.Caching.CacheDependency DP = new System.Web.Caching.CacheDependency (null, NULL);
HTTPCONTEXT.CURRENT.CACHE.ADD ("Key", "value", DP, DateTime.Now.AddHours (2), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, NULL);

HttpContext.Current.Cache.Insert ("Key", "value");
HttpContext.Current.Cache.Insert ("Key", "value", DP);

Get
Object cache = httpcontext.current.cache["key"];

Remove
HttpContext.Current.Cache.Remove ("key");
}
---------------------------------------2013-7-10 End---------------------------------------

. NET4 Cache Expiration Policy excerpt

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.