企业库5.0——缓存应用

using System;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace SpecialSale.Sys.SysCache
{
  /// <summary>
  /// 缓存过期类型枚举
  /// </summary>
  public enum EnumCacheExpirationType
  {
    /// <summary>
    /// 绝对过期
    /// </summary>
    AbsoluteTime = 1,
    /// <summary>
    /// 固定过期
    /// </summary>
    SlidingTime = 2,
    /// <summary>
    /// 指定过期格式,以特定的格式来过期
    /// </summary>
    ExtendedFormatTime=3,
    /// <summary>
    /// 文件依赖
    /// </summary>
    FileDependency=4,
  }

  public static class CacheHelper
  {
    //2种建立CacheManager的方式
    //ICacheManager cache = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>();
    private static ICacheManager cache = CacheFactory.GetCacheManager();

    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="type">缓存过期类型</param>
    public static void Add(string key, object value, EnumCacheExpirationType type)
    {
      switch (type)
      {
        case EnumCacheExpirationType.AbsoluteTime:
          cache.Add(key, value, CacheItemPriority.Normal, new SysCacheItemRefreshAction(), new AbsoluteTime(TimeSpan.FromMinutes(15)));
          break;
        case EnumCacheExpirationType.SlidingTime:
          cache.Add(key, value, CacheItemPriority.Normal, new SysCacheItemRefreshAction(), new SlidingTime(TimeSpan.FromMinutes(15)));
          break;
        case EnumCacheExpirationType.ExtendedFormatTime:
          throw new Exception("暂时不支持 ExtendedFormatTime 过期");
          break;
        case EnumCacheExpirationType.FileDependency:
          throw new Exception("暂时不支持 FileDependency 过期");
          break;
      }
    }

    /// <summary>
    /// 获取缓存对象
    /// </summary>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static T GetCache<T>(string key) where T:class
    {
      return cache.GetData(key) as T;
    }

    /// <summary>
    /// 移除缓存对象
    /// </summary>
    /// <param name="key">键</param>
    public static void RemoveCache(string key)
    {
      cache.Remove(key);
    }
  }

  /// <summary>
  /// 自定义缓存刷新操作
  /// </summary>
  [Serializable]
  public class SysCacheItemRefreshAction : 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)
      {
        ICacheManager cache = CacheFactory.GetCacheManager();
        cache.Add(removedKey, expiredValue);
      }
    }
    #endregion
  }
}

 

转载保留:http://blog.csdn.net/xxj_jing/article/details/7905285

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值