当前位置: 编程技术>.net/c#/asp.net
Enterprise Library for .NET Framework 2.0缓存使用实例
来源: 互联网 发布时间:2014-11-01
本文导语: Enterprise Library for .NET Framework 2.0 是微软发布的企业库,它支持.NET Framework 2.0。并且由一系列的企业应用程序块来构成。本文即以实例展示了Enterprise Library for .NET Framework 2.0缓存的使用方法,供大家参考。 关键代码如下: using ...
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
{
/*
*在Caching Application Block中,主要提供以下四种保存缓存数据的途径,
*分别是:内存存储(默认)、独立存储(Isolated Storage)、
*数据库存储(DataBase Cache Storage)和自定义存储(Custom Cache Storage)。
*In-Memory:保存在内存中。
*Isolated Storage Cache Store:系统将缓存的信息保存在独立文件中(C:UsersAppDataLocalIsolatedStorage)。
*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可以自动重新缓存。
*/
/////
///// 自定义缓存刷新操作
/////
//[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.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。