ASP.NETMVC5JSONResult缓存

ASP.NETMVC5JSONResult缓存,json,ajax,asp.net-mvc,caching,asp.net-mvc-5,Json,Ajax,Asp.net Mvc,Caching,Asp.net Mvc 5,有人能告诉我如何在MVC5应用程序中实现JsonResult操作的缓存吗? 我想使用[OutputCache()]属性来缓存一些被称为ajax的动作。其中一些操作返回ActionResult和html-内容,一些JsonResult和{Id,Title}对的序列化列表,我将使用这些对构建下拉列表 我的目标是减少数据库查询(在构建视图模型时)和服务器请求(在使用ajax调用时)的数量 因此,我的代码如下所示: [OutputCache(Duration=60*60*24)] public asyn

有人能告诉我如何在MVC5应用程序中实现
JsonResult
操作的缓存吗? 我想使用
[OutputCache()]
属性来缓存一些被称为ajax的动作。其中一些操作返回
ActionResult
html
-内容,一些
JsonResult
{Id,Title}
对的序列化列表,我将使用这些对构建下拉列表

我的目标是减少数据库查询(在构建视图模型时)和服务器请求(在使用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);
}
使用类似的
ajax
调用请求内容,如

$.get(url, null, function(data){
    //do something with data
});

那么,如何缓存json内容呢?什么是正确的方法,为什么默认方法不起作用?

< P>如果你想避免数据库查询,你应该考虑缓存服务器端的数据。您可以使用类来实现这一点

快速样品

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;
        }
    }
}
公共类MyLookupDataCache
{
const string categoryCacheKey=“CATEGORYLIST”;
公共列表GetCategories()
{
var cache=MemoryCache.Default;
var items=cache.Get(categoryCacheKey);
如果(项!=null)
{
CacheItemPolicy policy=新的CacheItemPolicy();
policy.AbsoluteExpiration=DateTime.Now.AddDays(7);//7天
//现在从数据库查询
List newItems=repository.GetCategories();
Set(categoryCacheKey、newItems、policy);
返回新项目;
}
其他的
{
退货(清单)项目;
}
}
}

您可以更改方法签名以返回所需的类型。为简单起见,我使用的是
List

其他浏览器是否尊重缓存头?对于AJAX请求,FF似乎忽略了这一点。您也可以使用ObjectCache(
HttpContext.Current.Cache
)来存储查询结果。@Jasen,实际上,当我在浏览器中直接打开链接时(不使用
ajax
),结果还是一样的。在Chrome中,情况也很相似。你试过这个吗@Edward在这个问题上,服务器端缓存存在问题。在我的例子中,服务器端缓存工作得很好。客户端有一点:浏览器询问服务器并获取缓存的服务器结果,但它不使用自己的缓存
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;
        }
    }
}