如果能在静态页中通过javascript加载一些动态数据,对这些数据做些缓存,相信会对页面访问速度有很大提高。
我这里试了两种方式,不知道大家还有没有更好的方式,欢迎指教。
方式一:数据放入服务器显示文本控件,更改控件文本输出方式
我这里用的
<asp:Literal runat="server" ID="ltlContent"></asp:Literal>
拼接显示内容foreach (DataRow dr in ds.Tables[0].Rows)
{
string leaveWordContent = (string)dr["QuestionContent"];
cacheContent += string.Format("document.write(\"<li><a target='_blank' href='../LeaveWord/ShowLeaveWord.aspx?ID={0}' title='{1}' >{2}</a></li> \");", (int)dr["ID"], leaveWordContent, (leaveWordContent.Length > 13 ? leaveWordContent.Substring(0, 13) : leaveWordContent));
}
string leaveWordContent = (string)dr["QuestionContent"];
cacheContent += string.Format("document.write(\"<li><a target='_blank' href='../LeaveWord/ShowLeaveWord.aspx?ID={0}' title='{1}' >{2}</a></li> \");", (int)dr["ID"], leaveWordContent, (leaveWordContent.Length > 13 ? leaveWordContent.Substring(0, 13) : leaveWordContent));
}
缓存处理
//保存到缓存
this.Context.Cache.Insert("LeaveWordListBlock", cacheContent, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero);
对于这个缓存时间我一直把握不到设置多少比较合适,对于一个信息显示网站来说。
最后更改控件输出方式
View Code
方式二:Ajax请求json数据。
请求处理数据
$(function () {
var jqxhr = $.ajax({
url: "../AjaxControls/getHotLeaveWord.ashx",
data: { ListSize: 8 },
type: "post",
dataType: "json"
})
.success(function (data) {
var showHtml = "";
if (data.result == "ok") {
var array = data.listContent;
for (var i = 0; i < array.length; i++) {
var showStr = array[i].Content.length > 13 ? array[i].Content.substr(0, 13) : array[i].Content;
showHtml += "<li><a target='_blank' href='../LeaveWord/ShowLeaveWord.aspx?ID=" + array[i].ID + "' title='" + array[i].Content + "' >" + showStr + "</a></li>";
}
}
else {
showHtml = "<li>暂时没有满足条件的数据</li>";
}
$('#ulList').html(showHtml);
})
.error(function () { $('#ulList').html("<li>请求数据错误</li>"); })
var jqxhr = $.ajax({
url: "../AjaxControls/getHotLeaveWord.ashx",
data: { ListSize: 8 },
type: "post",
dataType: "json"
})
.success(function (data) {
var showHtml = "";
if (data.result == "ok") {
var array = data.listContent;
for (var i = 0; i < array.length; i++) {
var showStr = array[i].Content.length > 13 ? array[i].Content.substr(0, 13) : array[i].Content;
showHtml += "<li><a target='_blank' href='../LeaveWord/ShowLeaveWord.aspx?ID=" + array[i].ID + "' title='" + array[i].Content + "' >" + showStr + "</a></li>";
}
}
else {
showHtml = "<li>暂时没有满足条件的数据</li>";
}
$('#ulList').html(showHtml);
})
.error(function () { $('#ulList').html("<li>请求数据错误</li>"); })
});
相应请求 一般处理文件中getHotLeaveWord.ashx
List<LeaveWordContent> listContent = new List<LeaveWordContent>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
LeaveWordContent leaveWordContent = new LeaveWordContent();
leaveWordContent.ID = (int)ds.Tables[0].Rows[i]["ID"];
leaveWordContent.Content = (string)ds.Tables[0].Rows[i]["QuestionContent"];
listContent.Add(leaveWordContent);
}
HotLeaveWord hotLeaveWord = new HotLeaveWord();
hotLeaveWord.result = "ok";
hotLeaveWord.listContent = listContent;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
LeaveWordContent leaveWordContent = new LeaveWordContent();
leaveWordContent.ID = (int)ds.Tables[0].Rows[i]["ID"];
leaveWordContent.Content = (string)ds.Tables[0].Rows[i]["QuestionContent"];
listContent.Add(leaveWordContent);
}
HotLeaveWord hotLeaveWord = new HotLeaveWord();
hotLeaveWord.result = "ok";
hotLeaveWord.listContent = listContent;
cacheContent = Newtonsoft.Json.JavaScriptConvert.SerializeObject(hotLeaveWord);
记得
//缓存 略....
context.Response.ContentType = "text/json";
context.Response.Write(cacheContent);
context.Response.Write(cacheContent);