Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cache System.Web.Caching in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about how to use cache System.Web.Caching in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

The difference between System.Web.Caching.Cache Insert and Add methods

Add ()

Object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)

Insert ()

Void Insert (string key, object value); / never expire void Insert (string key, object value, CacheDependency dependencies); / dependent void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration); / / absolute time expiration: void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback); / / dependency + callback void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback) / / dependency + priority + callback comparison, difference

a)。 The Insert method supports five overloads and is flexible to use, while the Add method must provide seven parameters

b)。 The Add method returns the data object of the cached item, and Insert returns Void

c)。 In the case of adding a duplicate cache (Key already exists), Insert replaces the item, while the Add method does nothing and returns the originally saved object object (Update 2014-03-18).

Expiration policy

a)。 Never expire

Insert (string key, object value)

b)。 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.

Example: Cache.Insert ("Data", ds,null, DateTime.Now.AddSeconds (10), TimeSpan.Zero)

c)。 Change time expiration (smooth expiration)

DateTime.MaxValue indicates that the absolute time expiration policy is not used, and TimeSpan.FromSeconds (10) indicates that the cache expires without access for 10 consecutive seconds.

Example: Cache.Insert ("Data", ds, null, DateTime.MaxValue, TimeSpan.FromSeconds (10))

Use Remove to clear all Cache

The overview clears the cache mainly through the Remove () method, but only by passing in a Key. The GetEnumerator () method is used to get all cached items. MoveNext () is used to move the location to the next cache item. If you want to clear all caches, because the Cache class does not provide a RemoveAll () method, you can do this in the following ways:

Public void removeAllCache () {IDictionaryEnumerator DicCache = HttpRuntime.Cache.GetEnumerator (); int count = HttpRuntime.Cache.Count; for (int I = 0; I < count; iTunes +) {DicCache.MoveNext (); HttpRuntime.Cache.Remove (DicCache.Entry.Key.ToString ()) }} Storage cache # region stores corresponding cache Cache cache = HttpRuntime.Cache; / / File cache depends on cache.Insert ("CC", "dependency test", new CacheDependency (@ "D:\ 123.txt")) / / add a line of code to the about.aspx page at this time. When you change the D:123.txt, the cache ["cc"] will be emptied immediately / / 30 seconds later and will expire and be removed immediately. Cache.Insert ("DD", "absolute expiration test", null, DateTime.Now.AddSeconds (30), System.Web.Caching.Cache.NoSlidingExpiration) will not be discussed. / / flexible expiration time, cache.Insert expires when cache is not used for 10 seconds ("EE", "sliding expiration test", null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds (10)); / / file weight level cache.Add ("FF", "cache importance level", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds (30), CacheItemPriority.High, null) / / when the server releases system memory, cache items with this priority level are most likely to be deleted from the cache. / / Low = 1Jing-when the server releases system memory, cached items with this priority level are more likely to be deleted from the cache than items assigned System.Web.Caching.CacheItemPriority.Normal priority. / / BelowNormal = 2Gen-when the server releases system memory, cached items with this priority level are likely to be deleted from the cache, second only to System.Web.Caching.CacheItemPriority.Low / / Normal = 3pint-the default value for cached items priority is System.Web.Caching.CacheItemPriority.Normal. / / Default = 3GI-when the server releases system memory, cache items with this priority level are less likely to be deleted than those assigned System.Web.Caching.CacheItemPriority.Normal priority. / / AboveNormal = 4 memory-cached items with this priority level are least likely to be deleted from the cache when the server frees system memory. / / High = 5Jing-when the server releases system memory, cache entries with this priority level will not be automatically deleted from the cache. However, items with this priority level will be removed along with other items based on their absolute or adjustable expiration time / / NotRemovable = 6, / / file weight level + Callback cache.Add ("GG", "buffer removal notification", null, DateTime.Now.AddSeconds (10), Cache.NoSlidingExpiration, CacheItemPriority.Low, Show) # endregion / / callback public void Show (string key, object value, CacheItemRemovedReason reason) {Cache cache = HttpRuntime.Cache; Cache.Insert ("GG", "cache is cleared! The cache has been cleared! The cache has been cleared! The cache has been cleared! The cache has been cleared! The cache has been cleared! Cache is cleared!);} get cache # region get corresponding cache / / directly open this page, output cache dependency test / / after changing D:\ 123.txt, after refreshing, the output is empty, indicating that the Cache is dependent on D:\ 123.txt 's Response.Write (HttpContext.Current.Cache ["CC"]) / / after a continuous refresh of 30 seconds, the absolute expired test Response.Write (HttpContext.Current.Cache ["DD"]) will not be output; / / if it is constantly refreshed, it will continue to be output, but when it is refreshed after more than 10 seconds, the sliding cache test Response.Write (HttpContext.Current.Cache ["EE"]) will not be output. / / File weight level Response.Write (HttpRuntime.Cache ["FF"]); / / Test callback function Response.Write (HttpRuntime.Cache ["GG"]); # endregion above is all the content of the article "how to use System.Web.Caching in C#". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report