栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Enterprise Library for .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
{
  /// 
  /// Enterprise Library for .NET framework 2.0 缓存工具类
  /// 
  public class ETLCacheToolV2
  {
    

    ///// 
    ///// 自定义缓存刷新操作
    ///// 
    //[Serializable]
    //public class CacheItemRefreshAction : ICacheItemRefreshAction
    //{
    //  #region ICacheItemRefreshAction 成员
    //  /// 
    //  /// 自定义刷新操作
    //  /// 
    //  /// 移除的键
    //  /// 过期的值
    //  /// 移除理由
    //  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();
    }
    /// 
    /// 获取CacheManager实例
    /// 
    /// CacheManager
    public static CacheManager Instance()
    {
      return CacheMgr;
    }
    /// 
    /// 添加缓存
    /// 
    /// 键
    /// 值
    public static void Add(string key, object value)
    {
      CacheMgr.Add(key, value);
    }
    /// 
    /// 添加缓存_滑动过期_小时
    /// 
    /// 键
    /// 值
    /// 小时
    public static void AddWithHour(string key, object value, int hour)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromHours(hour)));
    }
    /// 
    /// 添加缓存_滑动过期_天
    /// 
    /// 键
    /// 值
    /// 天
    public static void AddWithDay(string key, object value, int days)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromDays(days)));
    }
    /// 
    /// 添加缓存_滑动过期_毫秒
    /// 
    /// 键
    /// 值
    /// 毫秒
    public static void AddWithMillisecond(string key, object value, int millisecond)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMilliseconds(millisecond)));
    }
    /// 
    ///添加缓存_滑动过期_分钟
    /// 
    /// 键
    /// 值
    /// 分钟
    public static void AddWithMinutes(string key, object value, int minutes)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(minutes)));
    }
    /// 
    ///添加缓存_滑动过期_秒
    /// 
    /// 键
    /// 值
    /// 秒
    public static void AddWithSeconds(string key, object value, int seconds)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(seconds)));
    }
    /// 
    /// 添加缓存_滑动过期_文件依赖
    /// 
    /// 键
    /// 值
    /// 文件路径
    public static void AddFileDependency(string key, object value, string filePath)
    {
      FileDependency _fileDependency = new FileDependency(filePath);
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, _fileDependency);
    }
    /// 
    /// 添加缓存_滑动过期_小时
    /// 
    /// 键
    /// 值
    /// 小时
    /// ICacheItemRefreshAction
    public static void AddWithHour(string key, object value, int hour, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromHours(hour)));
    }
    /// 
    /// 添加缓存_滑动过期_天
    /// 
    /// 键
    /// 值
    /// 天
    /// ICacheItemRefreshAction
    public static void AddWithDay(string key, object value, int days, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromDays(days)));
    }
    /// 
    /// 添加缓存_滑动过期_毫秒
    /// 
    /// 键
    /// 值
    /// 毫秒
    /// ICacheItemRefreshAction
    public static void AddWithMillisecond(string key, object value, int millisecond, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromMilliseconds(millisecond)));
    }
    /// 
    /// 添加缓存_滑动过期_分钟
    /// 
    /// 键
    /// 值
    /// 分钟
    /// ICacheItemRefreshAction
    public static void AddWithMinutes(string key, object value, int minutes, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromMinutes(minutes)));
    }
    /// 
    /// 添加缓存_滑动过期_秒
    /// 
    /// 键
    /// 值
    /// 秒
    /// ICacheItemRefreshAction
    public static void AddWithSeconds(string key, object value, int seconds, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromSeconds(seconds)));
    }
    /// 
    /// 添加缓存_滑动过期_文件依赖
    /// 
    /// 键
    /// 值
    /// 文件路径
    /// ICacheItemRefreshAction
    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);
    }
    /// 
    /// 清空缓存
    /// 
    public static void Flush()
    {
      CacheMgr.Flush();
    }
    /// 
    /// 移出缓存
    /// 
    /// 
    public static void Remove(string key)
    {
      if (CacheMgr.Contains(key))
 CacheMgr.Remove(key);
    }
    /// 
    /// 获取缓存
    /// 
    /// 键
    /// 
    public static object GetData(string key)
    {
      if (CacheMgr.Contains(key))
 return CacheMgr.GetData(key);
      return null;
    }
    /// 
    /// 获取缓存
    /// 
    /// 泛型
    /// 键
    /// 
    public static T GetData(string key)
    {
      if (CacheMgr.Contains(key))
 return (T)CacheMgr.GetData(key);
      return default(T);
    }
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号