87,921
社区成员
发帖
与我相关
我的任务
分享
var _date = "2013-7-6"
$.getJSON("news.aspx?jsonp=?", { date: _date, r: Math.random() }, function (data) {
var html = [];
$.each(data, function (i, item) {
html.push('<li>' + item.title + '</li>');
});
$('#List').html(html.join(''));
});
<%@ OutputCache Duration="600" VaryByParam="date"%>
StringBuilder sb = new StringBuilder();
string CacheName = context.Request.QueryString["date"].ToString();//缓存名称
if (context.Cache[CacheName] == null)
{
List<Article> l = Article.GetNews(DateTime.Parse(date.ToString()));//这段就是访问数据库
foreach (Article a in l)
{
sb.Append("{\"id\":\"" + a.ID + "\",");
sb.Append("\"title\":\"" + a.Title + "\"},");
}
if (sb.ToString().EndsWith(","))
{
sb.Remove(sb.Length - 1, 1);
}
context.Cache.Insert(CacheName, sb, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration);//10分钟缓存
}
else
{
sb = (StringBuilder)context.Cache[CacheName];
}
<%@ OutputCache Duration="600" VaryByParam="date;r"%>
页面级缓存中,你VaryByParam属性中仅单独针对date参数做出了缓存定义。所以
news.aspx?date=1&r=0.1353
news.aspx?date=2&r=0.6989
这两个返回的结果会是相同的,VaryByParam属性可同时配置多个参数以做区分,多个参数用分号分隔开
如果上述方法可以解决你的问题,那就再多说一句。既然使用了r: Math.random()做动态的随机参数,你的页面级缓存的设置就已经毫无意义