【发布时间】:2015-02-28 03:25:26
【问题描述】:
我需要澄清一下缓存的工作方式。
我有一个具体问题:
我在该页面中有一个主页(索引)我有很多静态元素(标题、文本等),我在该页面中还有一个下拉列表,当 DOM 初始化时满载,让我放大一点,这样你就可以捕捉到我的漂移:
加载了我的索引页面,加载页面后,我在 AJAX(使用 Jquery)中发送请求,从 DB(SQL Azure)获取下拉列表的“选项”,我这样做是为了显示首先将页面发送给用户,然后从数据库中获取数据(因此用户不会等待页面加载的那一秒)。
现在当我缓存该页面时,我是否也缓存下拉列表的请求?
你想看一些代码吗?当然。
我正在使用 MVC 架构设计,所以我的代码如下所示:
家庭控制器:
// The Action that display the Index page
public class HomeController : Controller
{
[OutputCache(Duration=60*60)]
public ActionResult Index()
{
return View();
}
// The Action that get the data from the DB.
[OutputCache(Duration=15)]
public JsonResult GetProfiles()
{
Dictionary<string, string> ProfileDictionary = DataQueries.GetUserProfiles();
return Json(ProfileDictionary);
}
部分索引页面:
<div class="gap-bottom" >
<h2><span class="font-Droid">Step 2: </span><span class="font-Crete">Select Profile</span></h2>
<p>Pick a profile....</p>
@Html.DropDownList("filterSelect", new MultiSelectList(new[] { "Choose Profile" }), new { @style = "width: 50%", @class = "form-control" })
</div>
索引页面脚本
$.ajax({
type: 'POST',
url: '/Home/GetProfiles',
success: function (data) {
$.each(data, function (key, value) {
$('#filterSelect').append($("<option></option>").attr("value", value).text(key));
});
},
error: function (ts) {
if (ts.readyState == 0 || ts.status == 0) return;
alert(ts.responseText)
}
});
所以我的问题是:
如果我缓存页面,下拉列表是否也被缓存?或者我需要像我一样在spsfic中缓存Action?
底线,我想将页面缓存很长时间(如小时)和下拉列表 10 秒。
我自己做了一些测试(使用谷歌浏览器页面检查器中的网络选项卡),对于索引页面,我有时会看到“200 OK”,有时会看到“304 未修改”,比例为 1:1 一次 200好的,有时刷新页面时未修改 304。
关于下拉列表,我总是从 /Home/GetProfiles 得到 200 OK,正如我所见,这个动作根本没有缓存。
当我查看标题时(在 Google chrome 的网络选项卡中),我看到了两个请求(localhost - 获取索引页面,Home/GetProfiles - POST 获取配置文件)
Cache-Control:max-age=0
并在响应头中看到:
本地主机:
Cache-Control:public, max-age=3427
获取配置文件:
Cache-Control:public, max-age=20
这就是我想要的(在请求和响应的标题中)?如果它可以只缓存页面 X 秒和使用 AJAX 的请求 Y 秒?
谢谢。
【问题讨论】:
标签: jquery ajax asp.net-mvc caching browser-cache