Application of System. Web. caching in ASP. NET 2.0 (C #)
1. ApplyProgramAdd Cache
Public VoidInsert (StringKey, object value, cachedependency dependencies, datetime absoluteexpiration, timespan slidingexpiration)
Parameters
-
Key
-
The cache key used to reference this object.
-
Value
-
The object to be inserted into the cache.
-
Dependencies
-
The file dependency or cache key dependency of the inserted object. When any dependency item is changed, the object is invalid and removed from the cache. If no dependencies exist,
This parameter contains a null reference (nothing in Visual Basic ).
-
Absoluteexpiration
-
The time when the inserted object expires and is removed from the cache. If an absolute expiration time is usedSlidingexpirationThe parameter must be noslidingexpiration.
-
Slidingexpiration
-
The interval between the last access to the inserted object and the expiration time of the object. If the value is equivalent to 20 minutes, the object is accessed for 20 minutes at the last time.
It will expire and be removed from the cache. If you use an adjustable expiration timeAbsoluteexpirationThe parameter must be noabsoluteexpiration.
DS = dB. executedataset (dbcommand); // Insert the bject in the cache
Cache cache = system. Web. httpcontext. Current. cache;
Object oprovider = cache. Get (cachekey );
// Insert an absolute expired item into the application Cache
Cache. insert (cachekey, DS, null, datetime. Now. addminutes (30), timespan. Zero );
Cache. insert (cachekey, DS, null, datetime. Now. addminutes (30), cache. noslidingexpiration );
// Insert an item with adjustable expiration to the cache
Cache. insert (cachekey, DS, null, datetime. maxvalue, timespan. fromminutes (30 ));
Cache. insert (cachekey, DS, null, cache. noabsoluteexpiration, timespan. fromminutes (30 ));
PublicObject add (StringKey, object value, cachedependency dependencies, datetime absoluteexpiration, timespan slidingexpiration, cacheitempriority priority, cacheitemremovedcallback onremovecallback
Parameters
-
Key
-
The cache key used to reference this item.
-
Value
-
The entry to be added to the cache.
-
Dependencies
-
The file dependency or cache key dependency. When any dependency item is changed, the object is invalid and removed from the cache. If no dependency exists, this parameter contains
Null reference (nothing in Visual Basic ).
-
Absoluteexpiration
-
The time when the added object expires and is removed from the cache. If you use an adjustable expiration timeAbsoluteexpirationThe parameter must be noabsoluteexpiration.
-
Slidingexpiration
-
The interval between the last access to the added object and the expiration time of the object. If this value is equivalent to 20 minutes, the object will be accessed 20 minutes later.
It will expire and be removed from the cache. If an absolute expiration time is usedSlidingexpirationThe parameter must be noslidingexpiration.
-
Priority
-
The relative cost of the object, represented by cacheitempriority enumeration. This value is used when the cache exits the object; objects with lower costs before objects with higher costs
Removed from the cache.
-
Onremovecallback
-
The delegate called when an object is removed from the cache (if provided ). When you delete an application object from the cache, you can use it to notify the application.
Public VoidAdditemtocache (Object sender, eventargs e) {itemremoved =False; Onremove =NewCacheitemremovedcallback (This. Removedcallback );If(Cache ["key1"] =Null) Cache. Add ("key1", "value 1 ",Null, Datetime. Now. addseconds (60), timespan. Zero, cacheitempriority. High, onremove );}
2. Clear Cache
Cache. Remove (cachekey );