Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rickard Liljeberg 23 posts 114 karma points
    Aug 30, 2016 @ 15:19
    Rickard Liljeberg
    0

    OutputCache always renders on new url despite VaryByParam = "none"

    So I want to use OutputCache on a page which is popular. I have used route hijacking in umbraco and this is what it looks like:

    [OutputCache(VaryByParam = "none", Location = System.Web.UI.OutputCacheLocation.Server, Duration = 600)]
    public ActionResult EventPicker(EventPickerViewModel model, string targetGroupTag, bool bot = false)
    

    It works great.. except one thing... it always generates a new cache if I change the url.

            RouteTable.Routes.MapUmbracoRoute(
            "EventPickerRoute",
            "utbildningar/{targetGroupTag}",
            new
            {
                controller = "eventPicker",
                action = "EventPicker",
            },
            new EventPickerRouteHandler());
    

    I have this route (which also works great) but as you can see I can add targetGroupTag in the url and this is my problem.

    If I type utbildningar/tag1 or utbildningar/tag2 I want the same cache, but no matter what I do this does not happen.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 30, 2016 @ 15:45
    Nicholas Westby
    0

    I imagine VaryByParam applies to query string parameters. In other words, it would serve the same cache for utbildningar?tag=tag1 and utbildningar?tag=tag2, but it would serve different caches for utbildningar/tag1 and utbildningar/tag2.

    If you really want to treat the /tag1 and /tag2 versions the same, you could do VaryByCustom and create special criteria in your GetVaryByCustomString method in your custom UmbracoApplication implementation. See here: https://msdn.microsoft.com/en-us/library/5ecf4420.aspx

    By the way, you might also have to capitalize the "none" in VaryByParam = "none" so that it says VaryByParam = "None" (I'm not sure if it's case-sensitive).

  • Rickard Liljeberg 23 posts 114 karma points
    Aug 30, 2016 @ 19:02
    Rickard Liljeberg
    0

    I should have said, but

    1. I have tried capitalizing the none to None with no success.

    2. I have also tried the following: VaryByParam = "None", VaryByCustom = "eventpicker"

    With this in global.asax

        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom.InvariantEquals("eventpicker"))
            {
                return "Version=sameValueAlways";
            }
    
            return base.GetVaryByCustomString(context, custom);
        }
    

    I have then put a breakpoint on the return statement with Version= and it's hit and works.

    However, it's only run after it has already generated a new cache for a new url like utbildningar/tag2. I know this because my call actually takes like 6 seconds. And only after about 6 seconds does it break on that return-statement (if I change the url from utbildningar/tag1 to utbildningar/tag2 for instance). If I just reload the same url it uses the cache.

    But this VaryByCustom makes no difference because it's only called after the cache has been generated due to new url.

    It seems to be the VaryByCustom is useful only to create more caches, not fewer

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Sep 03, 2016 @ 07:05
    Nicholas Westby
    0

    What kind of controller are you implementing (e.g., Controller, RenderMvcController, SurfaceController)?

    I came across this article, which says Umbraco can interfere with output caching: http://www.wiliam.com.au/wiliam-blog/output-caching-can-improve-the-performance-of-your-website

    I'm not sure that applies in your case, but it's probably worth a read just in case.

    Also, I think Umbraco has a function, Html.CachedPartial, that would allow you to cache a partial view. Maybe that would work for your needs. Here's some info about that: https://our.umbraco.org/documentation/reference/templating/mvc/partial-views#caching

    By the way, you may want to return something other than "Version=sameValueAlways". Not sure if that naming has special behaviors. Perhaps try returning a simple word, like "Hello".

    Another thing to keep in mind is that by default, it should take two hits to a page before ASP.NET (or IIS, I forget which) will decide to actually add it to the cache. I think you can configure the number of requests to a page will trigger the caching (e.g., in case you want to change it to 1 visit).

Please Sign in or register to post replies

Write your reply to:

Draft