int intBlogid = 2;
try
{
intBlogid = int.Parse(Request["blogid"]);
}
catch
{ }
BLL.postword bll = new BLL.postword();
DataTable mytab = Common.mystring.GetCacheTable("myblog", 30);//读取cache
if (mytab == null)
{
mytab = bll.GetList(intBlogid).Tables[0];
Common.mystring.SetCacheTable("myblog", 30, mytab);//设置cache
} rpt.DataSource = mytab;
rpt.DataBind();
以上代码在本机测试,只要在30秒内连续访问,不论blogid的参数是什么,它绑定的内容都一样,我的意思如果blogid的值每次不一样,则要从数据库读取,如果一样则从缓存读取附:读取,设置Cache的代码
public static DataTable GetCacheTable(string strCacheKey,int intTime)
{
DataTable dt = null; Cache TabCache = HttpRuntime.Cache; if (TabCache[strCacheKey] != null)
{
dt = (DataTable)TabCache[strCacheKey];
TabCache.Insert(strCacheKey, dt, null, DateTime.Now.AddSeconds(intTime), Cache.NoSlidingExpiration);
}
return dt;
} public static void SetCacheTable(string strCacheKey, int intTime,DataTable mytab)
{ Cache TabCache = HttpRuntime.Cache; if (TabCache[strCacheKey] == null)
{
TabCache.Insert(strCacheKey, mytab, null, DateTime.Now.AddSeconds(intTime), Cache.NoSlidingExpiration);
}
}
改成 if ((mytab == null) || (((int)mytab.Tables[0].Rows[0][ID]) != intBlogid))
或同步更新缓存中的数据集
参考