欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Enterprise Library for .NET Framework 2.0缓存使用实例

程序员文章站 2024-02-13 10:39:04
enterprise library for .net framework 2.0 是微软发布的企业库,它支持.net framework 2.0。并且由一系列的企业应用程...

enterprise library for .net framework 2.0 是微软发布的企业库,它支持.net framework 2.0。并且由一系列的企业应用程序块来构成。本文即以实例展示了enterprise library for .net framework 2.0缓存的使用方法,供大家参考。

关键代码如下:

using microsoft.practices.enterpriselibrary.caching;
using microsoft.practices.enterpriselibrary.caching.expirations;
using system;

namespace etlutilhelpv2
{
  /// <summary>
  /// enterprise library for .net framework 2.0 缓存工具类
  /// </summary>
  public class etlcachetoolv2
  {
    /*
     *在caching application block中,主要提供以下四种保存缓存数据的途径,
     *分别是:内存存储(默认)、独立存储(isolated storage)、
     *数据库存储(database cache storage)和自定义存储(custom cache storage)。
     *in-memory:保存在内存中。  
     *isolated storage cache store:系统将缓存的信息保存在独立文件中(c:\users\<<user name>>\appdata\local\isolatedstorage)。
     *data cache storage:将缓存数据保存在数据库中。(需要运行createcachingdatabase.sql脚本)
     *custom cache storage:自己扩展的处理器。我们可以将数据保存在注册表中或文本文件中。
     *
     * 缓存等级,在企业库的缓存模块中已经提供了4个缓存等级:low,normal,high和notremovable,在超出最大缓存数量后会自动根据缓存等级来移除对象。
     * 过期方式,企业库默认提供4种过期方式
     * absolutetime:绝对是时间过期,传递一个时间对象指定到时过期
     * slidingtime:缓存在最后一次访问之后多少时间后过期,默认为2分钟,有2个构造函数可以指定一个过期时间或指定一个过期时间和一个最后使用时
     * extendedformattime :指定过期格式,以特定的格式来过期,通过extendedformat.cs类来包装过期方式,具体可参照extendedformat.cs,源代码中已经给出了很多方式
     * filedependency:依赖于文件过期,当所依赖的文件被修改则过期,这个我觉得很有用,因为在许多网站,如论坛、新闻系统等都需要大量的配置,可以将配置文件信息进行缓存,将依赖项设为配置文件,这样当用户更改了配置文件后通过icacheitemrefreshaction.refresh可以自动重新缓存。
     */

    ///// <summary>
    ///// 自定义缓存刷新操作
    ///// </summary>
    //[serializable]
    //public class cacheitemrefreshaction : icacheitemrefreshaction
    //{
    //  #region icacheitemrefreshaction 成员
    //  /// <summary>
    //  /// 自定义刷新操作
    //  /// </summary>
    //  /// <param name="removedkey">移除的键</param>
    //  /// <param name="expiredvalue">过期的值</param>
    //  /// <param name="removalreason">移除理由</param>
    //  void icacheitemrefreshaction.refresh(string removedkey, object expiredvalue, cacheitemremovedreason removalreason)
    //  {
    //    if (removalreason == cacheitemremovedreason.expired)
    //    {
    //      cachemanager cache = cachefactory.getcachemanager();
    //      cache.add(removedkey, expiredvalue);
    //    }
    //  }
    //  #endregion
    //}

    static cachemanager cachemgr = null;
    static etlcachetoolv2()
    {
      cachemgr = cachefactory.getcachemanager();
    }
    /// <summary>
    /// 获取cachemanager实例
    /// </summary>
    /// <returns>cachemanager</returns>
    public static cachemanager instance()
    {
      return cachemgr;
    }
    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void add(string key, object value)
    {
      cachemgr.add(key, value);
    }
    /// <summary>
    /// 添加缓存_滑动过期_小时
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="hour">小时</param>
    public static void addwithhour(string key, object value, int hour)
    {
      cachemgr.add(key, value, cacheitempriority.normal, null, new slidingtime(timespan.fromhours(hour)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_天
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="days">天</param>
    public static void addwithday(string key, object value, int days)
    {
      cachemgr.add(key, value, cacheitempriority.normal, null, new slidingtime(timespan.fromdays(days)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_毫秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="millisecond">毫秒</param>
    public static void addwithmillisecond(string key, object value, int millisecond)
    {
      cachemgr.add(key, value, cacheitempriority.normal, null, new slidingtime(timespan.frommilliseconds(millisecond)));
    }
    /// <summary>
    ///添加缓存_滑动过期_分钟
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="minutes">分钟</param>
    public static void addwithminutes(string key, object value, int minutes)
    {
      cachemgr.add(key, value, cacheitempriority.normal, null, new slidingtime(timespan.fromminutes(minutes)));
    }
    /// <summary>
    ///添加缓存_滑动过期_秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="seconds">秒</param>
    public static void addwithseconds(string key, object value, int seconds)
    {
      cachemgr.add(key, value, cacheitempriority.normal, null, new slidingtime(timespan.fromseconds(seconds)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_文件依赖
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="filepath">文件路径</param>
    public static void addfiledependency(string key, object value, string filepath)
    {
      filedependency _filedependency = new filedependency(filepath);
      cachemgr.add(key, value, cacheitempriority.normal, null, _filedependency);
    }
    /// <summary>
    /// 添加缓存_滑动过期_小时
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="hour">小时</param>
    /// <param name="refreshaction">icacheitemrefreshaction</param>
    public static void addwithhour(string key, object value, int hour, icacheitemrefreshaction refreshaction)
    {
      cachemgr.add(key, value, cacheitempriority.normal, refreshaction, new slidingtime(timespan.fromhours(hour)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_天
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="days">天</param>
    /// <param name="refreshaction">icacheitemrefreshaction</param>
    public static void addwithday(string key, object value, int days, icacheitemrefreshaction refreshaction)
    {
      cachemgr.add(key, value, cacheitempriority.normal, refreshaction, new slidingtime(timespan.fromdays(days)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_毫秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="millisecond">毫秒</param>
    /// <param name="refreshaction">icacheitemrefreshaction</param>
    public static void addwithmillisecond(string key, object value, int millisecond, icacheitemrefreshaction refreshaction)
    {
      cachemgr.add(key, value, cacheitempriority.normal, refreshaction, new slidingtime(timespan.frommilliseconds(millisecond)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_分钟
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="minutes">分钟</param>
    /// <param name="refreshaction">icacheitemrefreshaction</param>
    public static void addwithminutes(string key, object value, int minutes, icacheitemrefreshaction refreshaction)
    {
      cachemgr.add(key, value, cacheitempriority.normal, refreshaction, new slidingtime(timespan.fromminutes(minutes)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="seconds">秒</param>
    /// <param name="refreshaction">icacheitemrefreshaction</param>
    public static void addwithseconds(string key, object value, int seconds, icacheitemrefreshaction refreshaction)
    {
      cachemgr.add(key, value, cacheitempriority.normal, refreshaction, new slidingtime(timespan.fromseconds(seconds)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_文件依赖
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="filepath">文件路径</param>
    /// <param name="refreshaction">icacheitemrefreshaction</param>
    public static void addfiledependency(string key, object value, string filepath, icacheitemrefreshaction refreshaction)
    {
      filedependency _filedependency = new filedependency(filepath);
      cachemgr.add(key, value, cacheitempriority.normal, refreshaction, _filedependency);
    }
    /// <summary>
    /// 清空缓存
    /// </summary>
    public static void flush()
    {
      cachemgr.flush();
    }
    /// <summary>
    /// 移出缓存
    /// </summary>
    /// <param name="key"></param>
    public static void remove(string key)
    {
      if (cachemgr.contains(key))
        cachemgr.remove(key);
    }
    /// <summary>
    /// 获取缓存
    /// </summary>
    /// <param name="key">键</param>
    /// <returns>值</returns>
    public static object getdata(string key)
    {
      if (cachemgr.contains(key))
        return cachemgr.getdata(key);
      return null;
    }
    /// <summary>
    /// 获取缓存
    /// </summary>
    /// <typeparam name="t">泛型</typeparam>
    /// <param name="key">键</param>
    /// <returns>值</returns>
    public static t getdata<t>(string key)
    {
      if (cachemgr.contains(key))
        return (t)cachemgr.getdata(key);
      return default(t);
    }
  }
}

读者可在自身项目中对上述代码加以测试,相信会对大家的c#程序设计起到一定的帮助作用。