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?