.NET 缓存设计的使用说明
关于缓存的设计
1、什么情况下用缓存
缓存是提高应用程序性能的最好方法之一。运用缓存可以优化数据查询,避免不必要的网络数据回传,和避免执行不必要的完全相同的数据处理逻辑。在实现缓存的时候我们要确定什么时候装入缓存数据。用异步装入缓存或用批处理方式来避免出现客户端数据延迟。
一般来说在一定时间内请求了相同的业务逻辑而没有变更的话,可以采用缓存来设计。数据请求频繁的的请求不适合采用缓存,如论坛的回复,但是论坛的主题是可以采用缓存设计的。
2、缓存设计的步骤
确定缓存数据结构:即设计中哪些数据用到了缓存,设计这些数据的缓存结构
确定缓存什么数据
确定缓存过期规则和清理
确定如何装入缓存数据
3、示例 community server的缓存类
using system;
using system.collections;
using system.text.regularexpressions;
using system.web;
using system.web.caching;
namespace larry.cache
{
/// <summary>
/// 缓存类 community server的缓存类
/// </summary>
public class basecache
{
/// <summary>
/// cachedependency 说明
/// 如果您向 cache 中添加某个具有依赖项的项,当依赖项更改时,
/// 该项将自动从 cache 中删除。例如,假设您向 cache 中添加某项,
/// 并使其依赖于文件名数组。当该数组中的某个文件更改时,
/// 与该数组关联的项将从缓存中删除。
/// [c#]
/// insert the cache item.
/// cachedependency dep = new cachedependency(filename, dt);
/// cache.insert("key", "value", dep);
/// </summary>
public static readonly int dayfactor = ;
public static readonly int hourfactor = ;
public static readonly int minutefactor = ;
public static readonly double secondfactor = 0.;
private static readonly system.web.caching.cache _cache;
private static int factor = ;
/// <summary>
/// 单件模式
/// </summary>
static basecache()
{
httpcontext context = httpcontext.current;
if (context != null)
{
_cache = context.cache;
}
else
{
_cache = httpruntime.cache;
}
}
/// <summary>
/// 一次性清除所有缓存
/// </summary>
public static void clear()
{
idictionaryenumerator cacheenum = _cache.getenumerator();
arraylist al = new arraylist();
while (cacheenum.movenext()) //逐个清除
{
al.add(cacheenum.key);
}
foreach (string key in al)
{
_cache.remove(key);
}
}
public static void removebypattern(string pattern)
{
idictionaryenumerator cacheenum = _cache.getenumerator();
regex regex = new regex(pattern, regexoptions.ignorecase | regexoptions.singleline | regexoptions.compiled);
while (cacheenum.movenext())
{
if (regex.ismatch(cacheenum.key.tostring()))
_cache.remove(cacheenum.key.tostring());
}
}
/// <summary>
/// 清除特定的缓存
/// </summary>
/// <param name="key"></param>
public static void remove(string key)
{
_cache.remove(key);
}
/// <summary>
/// 缓存object.
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void insert(string key, object obj)
{
insert(key, obj, null, );
}
/// <summary>
/// 缓存obj 并建立依赖项
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
public static void insert(string key, object obj, cachedependency dep)
{
insert(key, obj, dep, minutefactor * );
}
/// <summary>
/// 按秒缓存对象
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="seconds"></param>
public static void insert(string key, object obj, int seconds)
{
insert(key, obj, null, seconds);
}
/// <summary>
/// 按秒缓存对象 并存储优先级
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="seconds"></param>
/// <param name="priority"></param>
public static void insert(string key, object obj, int seconds, cacheitempriority priority)
{
insert(key, obj, null, seconds, priority);
}
/// <summary>
/// 按秒缓存对象 并建立依赖项
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
/// <param name="seconds"></param>
public static void insert(string key, object obj, cachedependency dep, int seconds)
{
insert(key, obj, dep, seconds, cacheitempriority.normal);
}
/// <summary>
/// 按秒缓存对象 并建立具有优先级的依赖项
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
/// <param name="seconds"></param>
/// <param name="priority"></param>
public static void insert(string key, object obj, cachedependency dep, int seconds, cacheitempriority priority)
{
if (obj != null)
{
_cache.insert(key, obj, dep, datetime.now.addseconds(factor * seconds), timespan.zero, priority, null);
}
}
public static void microinsert(string key, object obj, int secondfactor)
{
if (obj != null)
{
_cache.insert(key, obj, null, datetime.now.addseconds(factor * secondfactor), timespan.zero);
}
}
/// <summary>
/// 最大时间缓存
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void max(string key, object obj)
{
max(key, obj, null);
}
/// <summary>
/// 具有依赖项的最大时间缓存
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
public static void max(string key, object obj, cachedependency dep)
{
if (obj != null)
{
_cache.insert(key, obj, dep, datetime.maxvalue, timespan.zero, cacheitempriority.abovenormal, null);
}
}
/// <summary>
/// insert an item into the cache for the maximum allowed time
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void permanent(string key, object obj)
{
permanent(key, obj, null);
}
public static void permanent(string key, object obj, cachedependency dep)
{
if (obj != null)
{
_cache.insert(key, obj, dep, datetime.maxvalue, timespan.zero, cacheitempriority.notremovable, null);
}
}
public static object get(string key)
{
return _cache[key];
}
/// <summary>
/// return int of seconds * secondfactor
/// </summary>
public static int secondfactorcalculate(int seconds)
{
// insert method below takes integer seconds, so we have to round any fractional values
return convert.toint(math.round((double)seconds * secondfactor));
}
}
}
其实这个类就是一个单件模式的设计 和缓存的公共操作方法,其中cachedependency表示建立缓存依赖项,cacheitempriority表示缓存的优先级。s使用如下
public static cardshop.model.systems getconfig()
{
const string cachekey = "webconfig";
cardshop.model.systems samplecachetable = larry.cache.basecache.get(cachekey) as cardshop.model.systems;
if (samplecachetable == null)
{
oprationcheck.message("第一次加载使用缓存");
samplecachetable = model;
larry.cache.basecache.insert(cachekey, samplecachetable, 24 * larry.cache.basecache.minutefactor);
}
else
{
oprationcheck.message("已经加载了缓存不需要再加载");
}
return samplecachetable;
}
下一篇: java 线性表接口的实例详解