The difference between 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 expiresvoid Insert(string key, object value, CacheDependency dependencies);//relyvoid Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);//Absolute time expires:void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback);//Dependency + callbackvoid Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);//rely+Priority+Callback
Compare, difference
a). The Insert method supports 5 types of overloads and is flexible to use, while the Add method must provide 7 parameters;
b). The Add method can return the data object of the cached item, and Insert returns Void;
c). When adding a duplicate cache (Key already exists), Insert will replace the item, while the Add method does not perform any operation and returns the original saved object (Update 2014-03-18).
Expiration strategy
- a). Never expires
Insert(string key, object value);
- b). Absolute time expires
(10) means that the cache expires after 10 seconds, which means that the smooth expiration strategy is not used.
example:("Data", ds,null, (10), );
- c). Change time expires (smooth expiration)
It means that the absolute time expiration strategy is not used, and (10) means that the cache expires without access for 10 consecutive seconds.
example:("Data", ds, null, , (10));
Empty all caches with Remove
Overview Clearing the cache is mainly done through the Remove() method, but only one can be cleared by passing in a Key. The GetEnumerator() method is used to get all cache items. MoveNext() is used to move the position to the next cache item. If you want to clear all caches, since the Cache class does not provide the RemoveAll() method, it can be implemented in the following ways:
public void removeAllCache() { IDictionaryEnumerator DicCache = (); int count = ; for (int i = 0; i < count; i++) { (); (()); } }
Save cache
#region Store corresponding cache Cache cache = ; //File cache dependency ("CC", "Dependency Testing", new CacheDependency(@"D:\")); //Add a line of code to the page at this time. When changing D:, cache["cc"] will be cleared immediately //It will expire in 30 seconds, remove immediately, no negotiation ("DD", "Absolutely expired test", null, (30), ); //Elastic expiration time, expires when the cache is not used for 10 seconds ("EE", "Sliding Expiration Test", null, , (10)); //File weight level ("FF", "Cache Importance Level", null, , (30), , null); // When the server frees system memory, cache items with this priority level are most likely to be deleted from the cache. //Low = 1,----------------- When the server frees system memory, cache items with this priority level are more likely to be deleted from the cache than items assigned to the priority level. //BelowNormal = 2,------------------- When the server frees system memory, a cache item with this priority level is likely to be deleted from the cache, and its possibility of being deleted is second only to having //Normal = 3,------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ //Default = 3,------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ //AboveNormal = 4,---------------------------- When the server frees system memory, cache items with this priority level are least likely to be deleted from the cache. //High = 5,------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- However, items with this priority level will be removed along with other items based on the absolute expiration time of the item or the adjustable expiration time of the item. // NotRemovable = 6, //File weight level +Callback ("GG", "Buffer removal notification", null, (10), , , Show); #endregion //Callback public void Show(string key, object value, CacheItemRemovedReason reason) { Cache cache = ; ("GG", "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! The cache has been cleared! The cache has been cleared!"); }
Get cache
#region Get the corresponding cache //Open this page directly and output cache dependency test //After changing D:\, refreshing and outputting empty, indicating that the cache is dependent on D:\ (["CC"]); //After continuous refresh of 30, no absolute expired test will be output again (["DD"]); //If you keep refreshing, the output will continue, but if it is refreshed after more than 10 seconds, it will not be output again. Sliding cache test (["EE"]); //File weight level (["FF"]); //Test the callback function (["GG"]); #endregion
This is all about this article about caching in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.