缓存处理
时间:2010-11-16 来源:漠北的天空
1。 Bll层;(MsgType为表名)
public class MsgType
{
protected AggregateCacheDependency dependency = new AggregateCacheDependency();
public MsgType() : base("MsgType")
{
dependency.Add(new SqlCacheDependency(ConfigurationManager.AppSettings ["CacheDatabaseName"].ToString(), "MsgType"));
}
public AggregateCacheDependency GetDependency()
{
return dependency;
}
}
2。 调用Bll的页面层;
public IList<MsgTypeEntity> GetMsgType()
{
string key = "MsgType";
IList<MsgTypeEntity> data = (IList<MsgTypeEntity>)HttpRuntime.Cache[key];
if (data == null)
{//缓存中不存在,则从数据库中取到缓存中。。。
MsgType bll = new MsgType();
data = bll.GetEntities<MsgTypeEntity>("");
AggregateCacheDependency cd = bll.GetDependency();
System.Web.HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
return data;
}