Enterprise Library for .NET Framework 2.0 is an enterprise library released by Microsoft that supports .NET Framework 2.0. And consists of a series of enterprise application blocks. This article uses examples to show how to use Enterprise Library for .NET Framework 2.0 cache for your reference.
The key code is as follows:
using ; using ; using System; namespace ETLUtilHelpV2 { /// <summary> /// Enterprise Library for .NET Framework 2.0 Cache Tool Class /// </summary> public class ETLCacheToolV2 { /* *In Caching Application Block, the following four ways to save cached data are provided. * are: memory storage (default), independent storage, *DataBase Cache Storage and Custom Cache Storage. *In-Memory: Saved in memory. *Isolated Storage Cache Store: The system saves the cached information in a separate file (C:\Users\<<user name>>\AppData\Local\IsolatedStorage). *Data Cache Storage: Save cached data in the database. (Requires running scripts) *Custom Cache Storage: its own extended processor. We can save data in a registry or in a text file. * * Cache level. Four cache levels have been provided in the caching module of the enterprise library: Low, Normal, High and NotRemovable. After exceeding the maximum cache number, the object will be automatically removed according to the cache level. * Expiration method, the enterprise library provides 4 expiration methods by default * AbsoluteTime: It is definitely time expiration, pass a time object to specify that the time expires * SlidingTime: How many times the cache expires after the last access, the default is 2 minutes. There are 2 constructors that can specify an expiration time or an expiration time and a last-use time * ExtendedFormatTime: Specify the expired format, expire in a specific format, and wrap the expiration method through classes. For details, you can refer to it. Many methods have been given in the source code. * FileDependency: Dependency depends on the expiration of the file, and when the dependent file is modified, it expires. I think this is very useful because many websites, such as forums, news systems, etc. require a lot of configuration. The configuration file information can be cached and the dependencies can be set as a configuration file. In this way, when the user changes the configuration file, it can be automatically re-cacheed. */ ///// <summary> /////// Custom cache refresh operation ///// </summary> //[Serializable] //public class CacheItemRefreshAction : ICacheItemRefreshAction //{ // #region ICacheItemRefreshAction Member // /// <summary> // //// Custom refresh operation // /// </summary> // /// <param name="removedKey">Removed key</param> // /// <param name="expiredValue">Expired Value</param> // /// <param name="removalReason">Remedy for removal</param> // void (string removedKey, object expiredValue, CacheItemRemovedReason removalReason) // { // if (removalReason == ) // { // CacheManager cache = (); // (removedKey, expiredValue); // } // } // #endregion //} static CacheManager CacheMgr = null; static ETLCacheToolV2() { CacheMgr = (); } /// <summary> /// Get CacheManager instance /// </summary> /// <returns>CacheManager</returns> public static CacheManager Instance() { return CacheMgr; } /// <summary> /// Add cache /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> public static void Add(string key, object value) { (key, value); } /// <summary> /// Add cache_sliding expired_hours /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="hour">hour</param> public static void AddWithHour(string key, object value, int hour) { (key, value, , null, new SlidingTime((hour))); } /// <summary> /// Add cache_sliding expired_day /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="days">day</param> public static void AddWithDay(string key, object value, int days) { (key, value, , null, new SlidingTime((days))); } /// <summary> /// Add cache_slide expiration_ms /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="millisecond">millisecond</param> public static void AddWithMillisecond(string key, object value, int millisecond) { (key, value, , null, new SlidingTime((millisecond))); } /// <summary> ///Add cache_Slide expiration_minute /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="minutes">min</param> public static void AddWithMinutes(string key, object value, int minutes) { (key, value, , null, new SlidingTime((minutes))); } /// <summary> ///Add cache_slide expiration_seconds /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="seconds">seconds</param> public static void AddWithSeconds(string key, object value, int seconds) { (key, value, , null, new SlidingTime((seconds))); } /// <summary> /// Add cache_sliding expired_file dependencies /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="filePath">File Path</param> public static void AddFileDependency(string key, object value, string filePath) { FileDependency _fileDependency = new FileDependency(filePath); (key, value, , null, _fileDependency); } /// <summary> /// Add cache_sliding expired_hours /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="hour">hour</param> /// <param name="refreshAction">ICacheItemRefreshAction</param> public static void AddWithHour(string key, object value, int hour, ICacheItemRefreshAction refreshAction) { (key, value, , refreshAction, new SlidingTime((hour))); } /// <summary> /// Add cache_sliding expired_day /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="days">day</param> /// <param name="refreshAction">ICacheItemRefreshAction</param> public static void AddWithDay(string key, object value, int days, ICacheItemRefreshAction refreshAction) { (key, value, , refreshAction, new SlidingTime((days))); } /// <summary> /// Add cache_slide expiration_ms /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="millisecond">millisecond</param> /// <param name="refreshAction">ICacheItemRefreshAction</param> public static void AddWithMillisecond(string key, object value, int millisecond, ICacheItemRefreshAction refreshAction) { (key, value, , refreshAction, new SlidingTime((millisecond))); } /// <summary> /// Add cache_slide expiration_minute /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="minutes">min</param> /// <param name="refreshAction">ICacheItemRefreshAction</param> public static void AddWithMinutes(string key, object value, int minutes, ICacheItemRefreshAction refreshAction) { (key, value, , refreshAction, new SlidingTime((minutes))); } /// <summary> /// Add cache_slide expiration_seconds /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="seconds">seconds</param> /// <param name="refreshAction">ICacheItemRefreshAction</param> public static void AddWithSeconds(string key, object value, int seconds, ICacheItemRefreshAction refreshAction) { (key, value, , refreshAction, new SlidingTime((seconds))); } /// <summary> /// Add cache_sliding expired_file dependencies /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="filePath">File Path</param> /// <param name="refreshAction">ICacheItemRefreshAction</param> public static void AddFileDependency(string key, object value, string filePath, ICacheItemRefreshAction refreshAction) { FileDependency _fileDependency = new FileDependency(filePath); (key, value, , refreshAction, _fileDependency); } /// <summary> /// Clear the cache /// </summary> public static void Flush() { (); } /// <summary> /// Move out the cache /// </summary> /// <param name="key"></param> public static void Remove(string key) { if ((key)) (key); } /// <summary> /// Get cache /// </summary> /// <param name="key">key</param> /// <returns>value</returns> public static object GetData(string key) { if ((key)) return (key); return null; } /// <summary> /// Get cache /// </summary> /// <typeparam name="T">Generics</typeparam> /// <param name="key">key</param> /// <returns>value</returns> public static T GetData<T>(string key) { if ((key)) return (T)(key); return default(T); } } }
Readers can test the above code in their own projects, which is believed to be helpful to everyone's C# programming.