8

I'm trying to implement a MongoDB / Memory combined Output Cache Provider to use with MVC4. Here is my initial implementation:

public class CustomOutputCacheProvider : OutputCacheProvider
{
    public override object Get(string key)
    {
        FileLogger.Log(key);
        return null;
    }

    public override object Add(string key, object entry, DateTime utcExpiry)
    {
        return entry;
    }

    public override void Set(string key, object entry, DateTime utcExpiry)
    {
    }

    public override void Remove(string key)
    {
    }
}

And my web config entry:

<caching>
  <outputCache defaultProvider="CustomOutputCacheProvider">
    <providers>
      <add name="CustomOutputCacheProvider" type="MyApp.Base.Mvc.CustomOutputCacheProvider" />
    </providers>
  </outputCache>
</caching>

And the usage within HomeController:

[OutputCache(Duration = 15)]
public ActionResult Index()
{
    return Content("Home Page");
}

My problem is, when I check the logfile for the keys that are requested, I see not only the request to home controller, but all other paths as well:

a2/  <-- should only log this entry
a2/test
a2/images/test/50115c53/1f37e409/4c7ab27d/50115c531f37e4094c7ab27d.jpg
a2/scripts/jquery-1.7.2.min.js

I've figured that I shouldn't set the CustomOutputCacheProvider as the defaultProvider in Web.Config, what I couldn't figure out is how to specify the cache provider that I want to use for a specific controller action.

With Asp.Net Web Pages you can accomplish it by using <%@ OutputCache Duration="60" VaryByParam="None" providerName="DiskCache" %> at the top of the page, but for MVC the only solution I could find is to override HttpApplication.GetOutputCacheProviderName Method in Global.asax.

Is there a more elegant way to accomplish this by using the [OutputCache] attribute?

M. Mennan Kara
  • 10,072
  • 2
  • 35
  • 39
  • Out of interest what is `a2/test` and how does it fit in with the original request `a2/`? – Chris Moutray Aug 13 '12 at 12:48
  • No idea really, what I request is `http://host/test`. Mvc adds the a2 (see [OutputCacheAttribute.cs](http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/98d041ae352f#src%2fSystem.Web.Mvc%2fOutputCacheAttribute.cs) ) while generating the key – M. Mennan Kara Aug 13 '12 at 12:55
  • 1
    You might want to check this http://stackoverflow.com/a/43405118/51734 – Barbaros Alp Apr 14 '17 at 04:16

3 Answers3

7

Is there a more elegant way to set the OutputCacheProvider using the [OutputCache] attribute?

I think the answer is no, (well not with the current mvc4 release) since there is no relationship between implementing a custom OutputCacheProvider and decorating an action with the OutputCache attribute.

As you discovered by implementing the custom provider and logging in the Get method you see every request made to the web server. If you were to remove the OutputCache attribute from all your actions you will still see every request in out log file. I thought the answer for this ASP.NET MVC hits outputcache for every action was pretty useful to confirm that.

Since it looks like you only want to implement one output-cache provider then I think your only option is to not set the default provider and continue to override the GetOutputCacheProviderName implementation (as you have already mentioned). Perhaps something like this to exclude all Content, Images and Scripts

public override string GetOutputCacheProviderName(HttpContext context)
{
    string absolutePath = context.Request.Url.AbsolutePath;

    if (absolutePath.StartsWith("/Content/", StringComparison.CurrentCultureIgnoreCase)
        || absolutePath.StartsWith("/Scripts/", StringComparison.CurrentCultureIgnoreCase)
        || absolutePath.StartsWith("/Images/", StringComparison.CurrentCultureIgnoreCase))
        return base.GetOutputCacheProviderName(context);

    return "CustomOutputCacheProvider";
}

If you need to implement more than one output-cache provider then I guess you'll have to implement a helper to give you the correct provider name. But here's an example where I've resolved the routing data for you; where as the prev example looked directly at the url.

public override string GetOutputCacheProviderName(HttpContext context)
{       
    RouteCollection rc = new RouteCollection();
    MvcApplication.RegisterRoutes(rc);
    RouteData rd = rc.GetRouteData(new HttpContextWrapper(HttpContext.Current));

    if (rd == null)
        return base.GetOutputCacheProviderName(context);

    var controller = rd.Values["controller"].ToString();
    var action = rd.Values["action"].ToString();

    if (controller.Equals("Content", StringComparison.CurrentCultureIgnoreCase) 
        || controller.Equals("Scripts", StringComparison.CurrentCultureIgnoreCase) 
        || controller.Equals("Images", StringComparison.CurrentCultureIgnoreCase))
        return base.GetOutputCacheProviderName(context);

    if (controller.Equals("Account", StringComparison.CurrentCultureIgnoreCase))
        return "AccountOutputCacheProvider";
    if (controller.Equals("Something", StringComparison.CurrentCultureIgnoreCase))
        return controller + "OutputCacheProvider";

    return "CustomOutputCacheProvider";
}
Community
  • 1
  • 1
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
1

If i where you, i would try to write MyOutputCachAttribute inherited from OutputCachAttribute that will choose provider by its parameter.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
  • I've considered it, but it seems like almost equal job to implement my own caching mechanism. See [OutputCacheAttribute.cs](http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/98d041ae352f#src%2fSystem.Web.Mvc%2fOutputCacheAttribute.cs) – M. Mennan Kara Aug 08 '12 at 15:59
0

Check this article from MSDN Magazine (with source code and examples referencing MongoDB & Azure as distributed cache providers) may well provide some insight http://msdn.microsoft.com/en-us/magazine/gg650661.aspx

EDIT

Can you use the CacheProfile setting to specify the provider as suggested here?

http://www.dotnetcurry.com/ShowArticle.aspx?ID=665

Luke Baughan
  • 4,658
  • 3
  • 31
  • 54