1

I try to cache a call to an action method in ASP.NET MVC3 RC1.

The caching works, but the variation by parameter doesnt seem to pick up. Is there something I can do to make the 3 calls to HotOffers return different results depending on productID?

The output right now is

Hot offers 4

Hot offers 4

Hot offers 4

I want the output to be

Hot offers 4

Hot offers 6

Hot offers 8

Action

[OutputCache(Duration = 100, VaryByParam = "productId")]
public PartialViewResult HotOffers(int productId)
{
    ProductModel model = new ProductModel { ProductID = productId };
    model.Name = "Meatball";

    return PartialView(model);
}

Page (Index.cshtml)

@{
    View.Title = "Home Page";
}
<p>
<div>
    @Html.Action("HotOffers", new { productid=4})
</div>
<div>
 @Html.Action("HotOffers", new { productid=6})
</div>
 <div>
 @Html.Action("HotOffers", new { productid = 8 })
</div>
</p>

Partial (HotOffers.cshtml)

Hot offers
@Model.ProductID
Mathias F
  • 14,815
  • 19
  • 82
  • 151
  • 1
    This is due to an incorrect design of the feature in MVC 3 RC. A fix has already been checked into the product and should be available at the next release. – Levi Nov 26 '10 at 20:45

1 Answers1

1

The Caching system used by asp.net exists outside of MVC and therefore only applies to Urls and then the VaryByParam only applies to QueryString parameters.

I posted some code OutputCache behavior in ASP.NET MVC 3 and that should get you going with caching actions based on parameters. That particular example I added an "ignore" parameter that will actually ignore one of the route fields but you can just remove that and you should be ok.

I guess I can post it here sans ignore

public class ActionOutputCacheAttribute : ActionFilterAttribute {
    public ActionOutputCacheAttribute(int cacheDuration) {
        this.cacheDuration = cacheDuration;
    }

    private int cacheDuration;
    private string cacheKey;

    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        string url = filterContext.HttpContext.Request.Url.PathAndQuery;
        this.cacheKey = ComputeCacheKey(filterContext);

        if (filterContext.HttpContext.Cache[this.cacheKey] != null) {
            //Setting the result prevents the action itself to be executed
            filterContext.Result =
            (ActionResult)filterContext.HttpContext.Cache[this.cacheKey];
        }

        base.OnActionExecuting(filterContext);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext) {
        //Add the ActionResult to cache 
        filterContext.HttpContext.Cache.Add(this.cacheKey, filterContext.Result,null, DateTime.Now.AddSeconds(cacheDuration),
          System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

        //Add a value in order to know the last time it was cached.
        filterContext.Controller.ViewData["CachedStamp"] = DateTime.Now;

        base.OnActionExecuted(filterContext);
    }

    private string ComputeCacheKey(ActionExecutingContext filterContext) {
        var keyBuilder = new StringBuilder();
        keyBuilder.Append(filterContext.ActionDescriptor.ControllerDescriptor.ControllerName);
        keyBuilder.Append(filterContext.ActionDescriptor.ActionName);

        foreach (var pair in filterContext.RouteData.Values) {
            if(pair.Value != null)
                keyBuilder.AppendFormat("rd{0}_{1}_", pair.Key.GetHashCode(), pair.Value.GetHashCode());
        }
        return keyBuilder.ToString();
    }
}

The above code was a modification of http://blog.stevensanderson.com/2008/10/15/partial-output-caching-in-aspnet-mvc/

Community
  • 1
  • 1
Buildstarted
  • 25,661
  • 9
  • 79
  • 93