I am trying to implement MongoDB / Memory, the unified output cache provider, for 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 entry in web configuration:
<caching> <outputCache defaultProvider="CustomOutputCacheProvider"> <providers> <add name="CustomOutputCacheProvider" type="MyApp.Base.Mvc.CustomOutputCacheProvider" /> </providers> </outputCache> </caching>
And use in HomeController:
[OutputCache(Duration = 15)] public ActionResult Index() { return Content("Home Page"); }
My problem is that when I check the log file for the requested keys, I see not only a request to the home controller, but also all other ways:
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 realized that I should not set CustomOutputCacheProvider as defaultProvider in Web.Config, I could not figure out how to specify the cache provider that I want to use for a specific controller action.
Using Asp.Net web pages, you can execute it using <%@ OutputCache Duration="60" VaryByParam="None" providerName="DiskCache" %> at the top of the page, but for MVC the only solution I could find , is an override of the HttpApplication.GetOutputCacheProviderName Method in Global.asax.
Is there a more elegant way to do this using the [OutputCache] attribute?