ter*_*ran 8 ajax asp.net-mvc json caching asp.net-mvc-5
谁能解释我如何JsonResult在MVC 5应用程序中实现缓存操作?我想ajax使用[OutputCache()] 属性来缓存一些被调用的动作.其中一些操作返回ActionResult与html-content,一些JsonResult具有系列化名单{Id, Title}对其中我将用来构造下拉列表.
我的目标是减少数据库查询(构建ViewModel时)和服务器请求(当使用ajax调用时).
所以,我的代码看起来像下面的代码片段:
[OutputCache(Duration=60*60*24)]
public async Task<ActionResult> SearchCaseOrgDialog(){
//extract data return html page
return View();
}
[OutputCache(Duration=60*60*24)]
public async Task<JsonResult> AjaxOrgDepartments(){
//query database, serialize data, return json
var result = await ctx.OrgDepartments
.Select(d => new {
Id = d.Id,
Title = d.Title }
)
.ToListAsync();
return Json(result, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
当我查看FireFox工具面板时,我会看到下一张图片 - 内容Html:

这里Firefox使用ajax-requested页面的客户端缓存版本.
它不缓存内容,似乎从服务器(服务器端缓存)传输数据.
在这两种情况下,响应头看起来都一样:
Cache-Control:"public, max-age=86400, s-maxage=0"
Run Code Online (Sandbox Code Playgroud)
使用类似的ajax-calls 请求内容
$.get(url, null, function(data){
//do something with data
});
Run Code Online (Sandbox Code Playgroud)
那么,我如何缓存json内容?什么是正确的方法,以及为什么默认方法不起作用?
如果你想避免数据库查询,你应该考虑在服务器端缓存数据。您可以使用MemoryCache类来做到这一点。
快速样品
public class MyLookupDataCache
{
const string categoryCacheKey = "CATEGORYLIST";
public List<string> GetCategories()
{
var cache = MemoryCache.Default;
var items = cache.Get(categoryCacheKey);
if (items != null)
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now.AddDays(7); //7 days
//Now query from db
List<string> newItems = repository.GetCategories();
cache.Set(categoryCacheKey, newItems, policy);
return newItems;
}
else
{
return (List<string>) items;
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以更改方法签名以返回您想要的类型。为了简单起见,我使用List<String>
| 归档时间: |
|
| 查看次数: |
3134 次 |
| 最近记录: |