Selecting custom output cache provider for specific controller actions

后端 未结 3 533
渐次进展
渐次进展 2021-02-06 05:50

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

public class CustomOutputCachePro         


        
相关标签:
3条回答
  • 2021-02-06 06:21

    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

    0 讨论(0)
  • 2021-02-06 06:23

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

    0 讨论(0)
  • 2021-02-06 06:42

    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";
    }
    
    0 讨论(0)
提交回复
热议问题