سؤال

OutputCache attribute on action that returns Json is not working - when I hit the action URL in browser many times, each time I got the breakpoint activated in VS2012 (it looks like the OutputCache attribute is ignored). Here is my code:

public class ApiController : GenericControllerBase
{

    [OutputCache(Duration = 300, VaryByParam = "type;showEmpty;sort;platform")]
    public JsonResult GetCategories(string type, bool? showEmpty, string sort, string platform)
    {
        ///... creating categoryResults object
        return Json(new ApiResult() { Result = categoryResults }, JsonRequestBehavior.AllowGet);
    }

}   

GenericControllerBase inherits from Controller. In other Controllers inheriting from GenericControllerBase OutputCache works as expected, but they return View() instead of Json. As an experiment I added VaryByCustom parameter and checked that GetVaryByCustomString method in global.asax file was not hit either, so the caching functionality is completely ignored. I use MVC3 and autofac for service injection (but other controllers using autofac injection work properly with OutputCache).

What could be the problem? What could block OutputCache functionality? Is it possible that it has something to do with caching the whole response? All other actions using OutputCache in my projects are partial actions embeded with @Html.Action(...) in views.

What is the best way to cache whole Json response on GET action in MVC3?

Update

After some tests it turned out that OutputCache is ignored for actions returning complete pages (not only json). Cache works only for childactions in my project. What could be the cause?

هل كانت مفيدة؟

المحلول

Eventually I used a workaround for MVC3 ignoring OutputCache attribute. I used custom action filter for caching:

public class ManualActionCacheAttribute : ActionFilterAttribute
{
    public ManualActionCacheAttribute()
    {
    }

    public int Duration { get; set; }

    private string cachedKey = null;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string key = filterContext.HttpContext.Request.Url.PathAndQuery;
        this.cachedKey = "CustomResultCache-" + key;
        if (filterContext.HttpContext.Cache[this.cachedKey] != null)
        {
            filterContext.Result = (ActionResult)filterContext.HttpContext.Cache[this.cachedKey];
        }
        else
        {
            base.OnActionExecuting(filterContext);
        }
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Cache.Add(this.cachedKey, filterContext.Result, null, DateTime.Now.AddSeconds(Duration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
        base.OnActionExecuted(filterContext);
    }
}

Above attribute caches the request based on exact URL path and query for given time, and it works as expected.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top