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#程序设计起到一定的帮助作用。



