先上代码:
WebCache Class
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Text;
namespace Web
{
/**//// <summary>
/// 緩存處理類
/// </summary>
public class WebCache<T>
{
private T data;
/**//// <summary>
/// 獲取或设置緩存值(對象實例化後如果存在緩存則會為此屬性設值)
/// </summary>
public T Data
{
get { return data; }
set
{
Set(value);
}
}
private bool isExist;
/**//// <summary>
/// 是否存在這個緩存
/// </summary>
public bool IsExist
{
get { return isExist; }
set { isExist = value; }
}
private string key;
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="webCacheKey">緩存Key的主要部分</param>
/// <param name="arg">緩存Key的其它部分,可以為空,與webCacheKey共同決定唯一的數據緩存</param>
public WebCache(WebCacheKey cacheKey, object cacheArg)
{
key = GetKey(cacheKey, cacheArg);
if (HttpRuntime.Cache[key] == null)
isExist = false;
else
{
isExist = true;
data = (T)HttpRuntime.Cache[key];
}
}
/**//// <summary>
/// 設置緩存,如果不存在則新加,如果存在則更新
/// </summary>
/// <param name="value">緩存值</param>
/// <param name="overDate">指定過期絕對時間</param>
public T Set(T value, DateTime overDate)
{
if (Config.App.EnableCaching)
{
if (value != null)
{
data = value;
if (isExist)
HttpRuntime.Cache.Remove(key);
HttpRuntime.Cache.Add(key, value, null, overDate, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
}
}
return value;
}
/**//// <summary>
/// 使用默認緩存時間(Web.config中設置)設置緩存,如果不存在則新加,如果存在則更新
/// </summary>
/// <param name="value">緩存值</param>
public T Set(T value)
{
return Set(value, DateTime.Now.AddSeconds(Config.App.CachingSecond));
}
/**//// <summary>
/// 移除缓存
/// </summary>
public void Remove()
{
if (isExist)
HttpRuntime.Cache.Remove(key);
}
/**//// <summary>
/// 获取缓存关键字
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="cacheArg"></param>
/// <returns></returns>
public static string GetKey(WebCacheKey cacheKey, object cacheArg)
{
string key = "CKEY" + cacheKey.ToString();
if (cacheArg != null && cacheArg.ToString().Length > 0)
key += "_" + cacheArg.ToString();
return key;
}
/**//// <summary>
/// 移除缓存的static方法
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="cacheArg"></param>
public static void Remove(WebCacheKey cacheKey, object cacheArg)
{
HttpRuntime.Cache.Remove(GetKey(cacheKey, cacheArg));
}
}
}
namespace Web
{
public enum WebCacheKey
{
UserInfo
}
}
代码很简单,类WebCache用于获取、设置、删除缓存,WebCacheKey提供了缓存键的统一管理——好处就是可以在多处重用,尽量避免同样一份数据多次缓存的问题。WebCahce类我最初是设计为只包括静态方法的,也没有WebCacheKey枚举,主要是想方便调用,后来使用中发现往往有多次查询同一个缓存项及在判断缓存是否存时代码不太好看的问题,如下面这样:
旧版WebCache使用示例(不是现在发的这个哦)
public static UserInfo GetUserInfoFromCache(long uid)
{
string key = "UserInfo_"+uid;
if(WebCache.Exists(key))
return WebCache.Get(key) as UserInfo;
else
{
UserInfo uInfo = new UserInfo(uid);
if(Config.App.EnbaledCaching)
WebCache.Set(key,uInfo);
return uInfo;
}
}
看上面的代码也还可以,用多了也感觉不太方便,利用现在的WebCache类使用缓存会类似下面这样,两个比较一下吧:
本篇文章发的WebCache类的用法
public static UserInfo GetUserInfoFromCache(long uid)
{
WebCache<UserTotalInfo> c = new WebCache<UserTotalInfo>(WebCacheKey.UserInfo, uid);
if (!c.IsExist)
c.Data = new UserInfo(uid);
return c.Data;
}
虽然是多个了类的实例化操作,但下面的操作就方便多了,也不会出现多次重复查询缓存值的现象,性能上会提高一点
注:上面所有代码中用到的Config.App.EnabledCaching为一个配置属性,就是从web.config中读取的是不是需要缓存数据的标志,在些就不再提供实现代码了(大家都知道^__^)
使用这个类的好处不紧紧方便书写代码,还可以在网站发展壮大添加缓存服务器(如Velocity - Microsoft Distributed Cache)时,直接修改这个类的方法即可,不用修改所有的缓存处理代码
如果错误,欢迎大家指正