SoFunction
Updated on 2025-03-07

Enterprise Library for .NET Framework 2.0 cache usage example

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.
      */

    ///// &lt;summary&gt;
    /////// Custom cache refresh operation    ///// &lt;/summary&gt;
    //[Serializable]
    //public class CacheItemRefreshAction : ICacheItemRefreshAction
    //{
    //  #region ICacheItemRefreshAction Member    //  /// &lt;summary&gt;
    // //// Custom refresh operation    //  /// &lt;/summary&gt;
    // /// <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 = ();
    }
    /// &lt;summary&gt;
    /// Get CacheManager instance    /// &lt;/summary&gt;
    /// &lt;returns&gt;CacheManager&lt;/returns&gt;
    public static CacheManager Instance()
    {
      return CacheMgr;
    }
    /// &lt;summary&gt;
    /// Add cache    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    public static void Add(string key, object value)
    {
      (key, value);
    }
    /// &lt;summary&gt;
    /// Add cache_sliding expired_hours    /// &lt;/summary&gt;
    /// <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)));
    }
    /// &lt;summary&gt;
    /// Add cache_sliding expired_day    /// &lt;/summary&gt;
    /// <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)));
    }
    /// &lt;summary&gt;
    /// Add cache_slide expiration_ms    /// &lt;/summary&gt;
    /// <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)));
    }
    /// &lt;summary&gt;
    ///Add cache_Slide expiration_minute    /// &lt;/summary&gt;
    /// <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)));
    }
    /// &lt;summary&gt;
    ///Add cache_slide expiration_seconds    /// &lt;/summary&gt;
    /// <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)));
    }
    /// &lt;summary&gt;
    /// Add cache_sliding expired_file dependencies    /// &lt;/summary&gt;
    /// <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);
    }
    /// &lt;summary&gt;
    /// Add cache_sliding expired_hours    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    /// <param name="hour">hour</param>    /// &lt;param name="refreshAction"&gt;ICacheItemRefreshAction&lt;/param&gt;
    public static void AddWithHour(string key, object value, int hour, ICacheItemRefreshAction refreshAction)
    {
      (key, value, , refreshAction, new SlidingTime((hour)));
    }
    /// &lt;summary&gt;
    /// Add cache_sliding expired_day    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    /// <param name="days">day</param>    /// &lt;param name="refreshAction"&gt;ICacheItemRefreshAction&lt;/param&gt;
    public static void AddWithDay(string key, object value, int days, ICacheItemRefreshAction refreshAction)
    {
      (key, value, , refreshAction, new SlidingTime((days)));
    }
    /// &lt;summary&gt;
    /// Add cache_slide expiration_ms    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    /// <param name="millisecond">millisecond</param>    /// &lt;param name="refreshAction"&gt;ICacheItemRefreshAction&lt;/param&gt;
    public static void AddWithMillisecond(string key, object value, int millisecond, ICacheItemRefreshAction refreshAction)
    {
      (key, value, , refreshAction, new SlidingTime((millisecond)));
    }
    /// &lt;summary&gt;
    /// Add cache_slide expiration_minute    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    /// <param name="minutes">min</param>    /// &lt;param name="refreshAction"&gt;ICacheItemRefreshAction&lt;/param&gt;
    public static void AddWithMinutes(string key, object value, int minutes, ICacheItemRefreshAction refreshAction)
    {
      (key, value, , refreshAction, new SlidingTime((minutes)));
    }
    /// &lt;summary&gt;
    /// Add cache_slide expiration_seconds    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    /// <param name="seconds">seconds</param>    /// &lt;param name="refreshAction"&gt;ICacheItemRefreshAction&lt;/param&gt;
    public static void AddWithSeconds(string key, object value, int seconds, ICacheItemRefreshAction refreshAction)
    {
      (key, value, , refreshAction, new SlidingTime((seconds)));
    }
    /// &lt;summary&gt;
    /// Add cache_sliding expired_file dependencies    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <param name="value">value</param>    /// <param name="filePath">File Path</param>    /// &lt;param name="refreshAction"&gt;ICacheItemRefreshAction&lt;/param&gt;
    public static void AddFileDependency(string key, object value, string filePath, ICacheItemRefreshAction refreshAction)
    {
      FileDependency _fileDependency = new FileDependency(filePath);
      (key, value, , refreshAction, _fileDependency);
    }
    /// &lt;summary&gt;
    /// Clear the cache    /// &lt;/summary&gt;
    public static void Flush()
    {
      ();
    }
    /// &lt;summary&gt;
    /// Move out the cache    /// &lt;/summary&gt;
    /// &lt;param name="key"&gt;&lt;/param&gt;
    public static void Remove(string key)
    {
      if ((key))
        (key);
    }
    /// &lt;summary&gt;
    /// Get cache    /// &lt;/summary&gt;
    /// <param name="key">key</param>    /// <returns>value</returns>    public static object GetData(string key)
    {
      if ((key))
        return (key);
      return null;
    }
    /// &lt;summary&gt;
    /// Get cache    /// &lt;/summary&gt;
    /// <typeparam name="T">Generics</typeparam>    /// <param name="key">key</param>    /// <returns>value</returns>    public static T GetData&lt;T&gt;(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.