关于缓存的设计
1、什么情况下用缓存
缓存是提高应用程序性能的最好方法之一。运用缓存可以优化数据查询,避免不必要的网络数据回传,和避免执行不必要的完全相同的数据处理逻辑。在实现缓存的时候我们要确定什么时候装入缓存数据。用异步装入缓存或用批处理方式来避免出现客户端数据延迟。
一般来说在一定时间内请求了相同的业务逻辑而没有变更的话,可以采用缓存来设计。数据请求频繁的的请求不适合采用缓存,如论坛的回复,但是论坛的主题是可以采用缓存设计的。
2、缓存设计的步骤
确定缓存数据结构:即设计中哪些数据用到了缓存,设计这些数据的缓存结构
确定缓存什么数据
确定缓存过期规则和清理
确定如何装入缓存数据
3、示例 Community Server的缓存类
/// <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使用如下
新闻热点
疑难解答