我正在将ASP.Net Web窗体应用程序转换为MVC 4.这些应用程序显示常见项目,并且可以为大多数需要显示的项目使用一个域模型。应用程序仅显示数据,不会修改数据。
目前,当用户访问页面时,将从Oracle数据库中检索数据。该页面每30秒更新一次更新面板。因此,如果有几个用户打开他们的页面(这很常见),每个用户都会打到数据库。每30秒一次。
数据库数据每3分钟更新一次。
我想要做的是每30秒更新一次域模型,应用程序从模型中获取数据。因此,每次发出页面请求时,都会从模型中获取数据。由于所有应用程序的数据都是从同一个域模型中检索出来的,因此它们都是相同的。这也应该减少数据库被击中的次数,并希望加快页面重新加载(以及Ajax调用)。
所以, - 如果是第一次使用域模型,请从数据库中填充它。 - 如果小于或等于30秒,应用程序将使用域模型 - 如果超过30秒,模型将从数据库中重新填充,应用程序将从模型中获取数据。
是否可以让这些多个应用程序使用一个域模型?并且,如果可以以某种方式缓存域模型,那么如何做呢?
提前致谢。
答案 0 :(得分:2)
您可以通过使用超时轮询服务器来模拟jQuery AJAX中的UpdatePanel
功能,同时还将模型发送到服务器。
基本上,您的网页加载后,您的HttpGet
操作方法会从数据库中填充您的模型,然后将其发送到您的视图。
首先,您必须有一个部分视图作为您的UpdatePanel
,包含在我们可以在jQuery中引用的容器中:
<div id="stateContainer">
@Html.Partial("YourPartial", Model)
</div>
然后你的jQuery会做类似的事情:
setInterval("pollServer()", 30000);
function pollServer() {
$.ajax({
url: '@Url.Action("PollForUpdate")',
type: "POST",
datatype: 'json',
data: $("form").serialize(),
success: function (result) {
$("#stateContainer").html(result);
}
});
}
然后您的操作方法PollForUpdate
看起来像:
public ActionResult PollForUpdate(YourModel Model)
{
ModelState.Clear();
//fill your Model object with your database stuff
return View("YourPartial", Model);
}
答案 1 :(得分:2)
您可以在控制器中缓存数据库调用的结果。
public ActionResult MyController()
{
var cache = HttpContext.Cache;
var model = cache["key"];
if (model == null) {
model = GetData();
cache.Insert(
"key",
model,
null,
DateTime.Now.AddSeconds(30),
System.Web.Caching.Cache.NoSlidingExpiration);
}
return View(model);
}
或者通过向控制器添加OutputCache属性来缓存整个视图。
[OutputCache(Duration = 30, VaryByParam = "none")]
public ActionResult MyController()
{
var model = GetData();
return View(model);
}