Key code:
usingMicrosoft.Practices.EnterpriseLibrary.Caching;usingMicrosoft.Practices.EnterpriseLibrary.Caching.Expirations;usingSystem;namespaceetlutilhelpv2{// <summary> /// Enterprise Library for. NET Framework 2.0 Cache tool Classes // </summary> Public classETLCacheToolV2 {/** In caching Application block, the following four ways to save cached data are provided,* respectively: Memory storage (default), isolated storage (Isolated Storage),* Database Cache Storage and custom cache (Storage). *in-memory: In memory. *isolated Storage Cache Store: The system saves cached information in a separate file (C:\users\<<user Name>>\appdata\local\isolatedstora GE). *data Cache Storage: Saves cached data in the database. (Need to run Createcachingdatabase.sql script)*custom Cache Storage: self-expanding processor. We can save the data in the registry or in a text file. ** Cache level, 4 cache levels are already available in the cache module of the Enterprise Library: Low,normal,high and notremovable, which automatically removes objects based on the cache level after exceeding the maximum number of caches. * Expiration mode, the Enterprise Library default provides 4 kinds of expiration* Absolutetime: Absolute time expires, passing a time object specified to expire* Slidingtime: The cache expires after the last visit, the default is 2 minutes, 2 constructors can specify an expiration time or specify an expiration time and a last used* Extendedformattime: Specify the expiration format, in a specific format to expire, through the ExtendedFormat.cs class to wrap the expiration method, specifically can refer to ExtendedFormat.cs, the source code has given a lot of ways* Filedependency: dependent on the expiration of the file, when the dependent files are modified to expire, this I think is very useful, because in many sites, such as forums, news systems and so on need a lot of configuration, you can cache the profile information, the dependency is set as a profile, This allows the user to automatically re-cache the configuration file after it has been changed by Icacheitemrefreshaction.refresh. */ /////<summary> /////Custom Cache refresh Operations /////</summary> //[serializable] //public class Cacheitemrefreshaction:icacheitemrefreshaction //{ //#region Icacheitemrefreshaction members ////<summary> /////Custom refresh Operation ////</summary> //////<param Name= "Removedkey" > Removed keys </param> /////<param Name= "Expiredvalue" > Expired value </param> /////<param Name= "Removalreason" > Removal reasons </param> //void Icacheitemrefreshaction.refresh (String Removedkey, Object Expiredvalue, CacheItemRemovedReason Removalreason) // { //if (Removalreason = = cacheitemremovedreason.expired) // { //CacheManager cache = Cachefactory.getcachemanager (); //cache. ADD (Removedkey, expiredvalue); // } // } //#endregion //} StaticCacheManager cachemgr =NULL;StaticETLCacheToolV2 () {cachemgr = Cachefactory.getcachemanager (); }// <summary> /// get CacheManager instance // </summary> // <returns>CacheManager</returns> Public StaticCacheManager Instance () {returnCachemgr; }// <summary> /// Add cache // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> Public Static voidADD (stringKeyObject value) {Cachemgr.add (key,value); }// <summary> /// Add Cache _ Sliding Expiration _ hours // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> /// <param name= "Hour" > Hours </param> Public Static voidAddwithhour (stringKeyObject value,intHour) {Cachemgr.add (key,value, Cacheitempriority.normal,NULL,NewSlidingtime (Timespan.fromhours (hour))); }// <summary> /// Add Cache _ Sliding Expiration _ days // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> // <param name= "Days" > Day </param> Public Static voidAddwithday (stringKeyObject value,intDays) {Cachemgr.add (key,value, Cacheitempriority.normal,NULL,NewSlidingtime (Timespan.fromdays (days))); }// <summary> /// Add Cache _ Sliding expiration _ milliseconds // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> // <param name= "millisecond" > Ms </param> Public Static voidAddwithmillisecond (stringKeyObject value,intMillisecond) {Cachemgr.add (key,value, Cacheitempriority.normal,NULL,NewSlidingtime (Timespan.frommilliseconds (millisecond))); }// <summary> /// Add Cache _ Sliding expiration _ minutes // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> /// <param name= "Minutes" > Min </param> Public Static voidAddwithminutes (stringKeyObject value,intminutes) {Cachemgr.add (key,value, Cacheitempriority.normal,NULL,NewSlidingtime (Timespan.fromminutes (minutes))); }// <summary> /// Add Cache _ Sliding expiration _ seconds // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> // <param name= "seconds" > S </param> Public Static voidAddwithseconds (stringKeyObject value,intseconds) {Cachemgr.add (key,value, Cacheitempriority.normal,NULL,NewSlidingtime (timespan.fromseconds (seconds))); }// <summary> /// Add Cache _ Sliding Expiration _ file Dependency // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> /// <param name= "FilePath" > File path </param> Public Static voidAddFileDependency (stringKeyObject value,stringFilePath) {Filedependency _filedependency =NewFiledependency (FilePath); Cachemgr.add (Key,value, Cacheitempriority.normal,NULL, _filedependency); }// <summary> /// Add Cache _ Sliding Expiration _ hours // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> /// <param name= "Hour" > Hours </param> /// <param name= "refreshaction" >ICacheItemRefreshAction</param> Public Static voidAddwithhour (stringKeyObject value,intHour, icacheitemrefreshaction refreshaction) {Cachemgr.add (key,value, Cacheitempriority.normal, Refreshaction,NewSlidingtime (Timespan.fromhours (hour))); }// <summary> /// Add Cache _ Sliding Expiration _ days // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> // <param name= "Days" > Day </param> /// <param name= "refreshaction" >ICacheItemRefreshAction</param> Public Static voidAddwithday (stringKeyObject value,intDays, Icacheitemrefreshaction refreshaction) {Cachemgr.add (key,value, Cacheitempriority.normal, Refreshaction,NewSlidingtime (Timespan.fromdays (days))); }// <summary> /// Add Cache _ Sliding expiration _ milliseconds // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> // <param name= "millisecond" > Ms </param> /// <param name= "refreshaction" >ICacheItemRefreshAction</param> Public Static voidAddwithmillisecond (stringKeyObject value,intMillisecond, icacheitemrefreshaction refreshaction) {Cachemgr.add (key,value, Cacheitempriority.normal, Refreshaction,NewSlidingtime (Timespan.frommilliseconds (millisecond))); }// <summary> /// Add Cache _ Sliding expiration _ minutes // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> /// <param name= "Minutes" > Min </param> /// <param name= "refreshaction" >ICacheItemRefreshAction</param> Public Static voidAddwithminutes (stringKeyObject value,intMinutes, icacheitemrefreshaction refreshaction) {Cachemgr.add (key,value, Cacheitempriority.normal, Refreshaction,NewSlidingtime (Timespan.fromminutes (minutes))); }// <summary> /// Add Cache _ Sliding expiration _ seconds // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> // <param name= "seconds" > S </param> /// <param name= "refreshaction" >ICacheItemRefreshAction</param> Public Static voidAddwithseconds (stringKeyObject value,intseconds, icacheitemrefreshaction refreshaction) {Cachemgr.add (key,value, Cacheitempriority.normal, Refreshaction,NewSlidingtime (timespan.fromseconds (seconds))); }// <summary> /// Add Cache _ Sliding Expiration _ file Dependency // </summary> // <param name= "key" > Keys </param> // <param name= "value" > Value </param> /// <param name= "FilePath" > File path </param> /// <param name= "refreshaction" >ICacheItemRefreshAction</param> Public Static voidAddFileDependency (stringKeyObject value,stringFilePath, Icacheitemrefreshaction refreshaction) {filedependency _filedependency =NewFiledependency (FilePath); Cachemgr.add (Key,value, Cacheitempriority.normal, Refreshaction, _filedependency); }// <summary> // Empty cache // </summary> Public Static voidFlush () {Cachemgr.flush (); }// <summary> /// move out Cache // </summary> /// <param name= "key" ></param> Public Static voidRemove (stringKey) {if(Cachemgr.contains (key)) Cachemgr.remove (key); }// <summary> /// Get Cache // </summary> // <param name= "key" > Keys </param> /// <returns> value </returns> Public Static ObjectGetData (stringKey) {if(Cachemgr.contains (key))returnCachemgr.getdata (key);return NULL; }// <summary> /// Get Cache // </summary> /// <typeparam name= "T" > Generics </typeparam> // <param name= "key" > Keys </param> /// <returns> value </returns> Public StaticT getdata<t> (stringKey) {if(Cachemgr.contains (key))returnT Cachemgr.getdata (key);return default(T); } }}
Hope to be helpful!