Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Feb 28, 2011 @ 11:12
    Ismail Mayat
    0

    inline razor macro caching

    Guys,

    If you are using razor macro inline in a template is there a way to add caching timeout on it? eg property on the macro tag?

    Regards

    Ismail

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 28, 2011 @ 11:34
    Sebastiaan Janssen
    0

    There's nothing like that for other inline macro's right? You're going to have to put it in a seperate razor file then as I've not seen anything built for this yet in the Codeplex commits.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Feb 28, 2011 @ 11:38
    Ismail Mayat
    0

    Sebastiaan,

    Thought that would be the answer no worries.

    Regards

    Ismail

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Feb 28, 2011 @ 11:52
    Niels Hartvig
    6

    Yes, there is. Simply add Cache="xxx" attribute to the umbraco:Macro tag where xxx is the number of seconds to cache ;-)

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 28, 2011 @ 12:12
    Sebastiaan Janssen
    0

    That is awesome! I assume it works for all macros? Are there also attribute for cache personalized and by page?

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Mar 01, 2011 @ 17:05
    Damiaan
    0

    @Niels: does this also work for inline macro's ?

      <umbraco:Macro runat="server" language="razor">
        @if(Model.umbracoNaviHide == "1") {
          @("just a test")
        }
      </umbraco:Macro

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 02, 2011 @ 07:59
    Sebastiaan Janssen
    0

    Damiaan: That's the only place it will work.. Cache for 5 minutes:

    <umbraco:Macro runat="server" language="razor" Cache="300">
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 24, 2011 @ 16:25
    Jeroen Breuer
    0

    Is there any benefit to cache a Razor macro? I just watched this video: http://umbraco.com/help-and-support/video-tutorials/introduction-to-umbraco/developer-introduction/macro-caching and it says that only code behind logic is cached. Wouldn't this mean nothing is cached in Razor? Also this topic got me a bit scared: http://our.umbraco.org/forum/using/ui-questions/16979-Usercontrol-Cache-EXTREMELY-Dangerous!. If I have a form in Razor could the same happen?

    Is there some best practices place where I can find when I should and when I shouldn't cache macro's?

    Something else is that you can also point to Razor files without using macro's right? Instead of an alias you point to the file location. Would that also be faster since I sometimes think using macro's for Razor is a bit of an overhead. 

    Still trying to figure out what's the best way to use Razor with macro's :).

    Jeroen

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    May 24, 2011 @ 17:08
    Sebastiaan Janssen
    0

    See the Umbraco TV episode about caching

    I use no more macro's for Razor, they're not needed and yes it saves a tiny amount of overhead. Also see this Razor Empire tweet and the Pastebin link in it.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 24, 2011 @ 17:46
    Jeroen Breuer
    0

    Thanks I already watched that episode. That's why I posted this question ;-). I think I'll remove some macro's and will use filelocation instead.

    Jeroen

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    May 24, 2011 @ 18:23
    Sebastiaan Janssen
    0

    D'oh I should learn to read!

    The only thing I really know about caching is that I've never used it! :-) Do you really, really need caching or is it just something you are doing out of precaution?

    I would have a look at your slowest Razor scripts (?umbDebugShowTrace=true in your querystring) and see if you could improve the code. If the code is optimal, only then I would think about using caching.

    I do also wonder what exactly is cached for Razor scripts, it would make the most sense if the html that is being generated is eventually cached, but I can't say really.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 25, 2011 @ 10:12
    Jeroen Breuer
    0

    Ok I did a little bit of researching and it's true that the page loads faster if the macro uses filelocation instead of an actuall macro in the developer section.

    I've debugged my page and here are the results of a macro which is created in the developer section (which has caching disabled):

    First load
    umbracoMacro    MacroEngine script added (BuildingTypeMapLocations.cshtml)        0,575123392070605    0,000018
    umbracoMacro    Loading IMacroEngine script                                                         0,575134342004769    0,000011
    umbracoMacro    Loading IMacroEngine script [done]                                               1,23939498876263    0,664261

    Second load (refresh the page)
    umbracoMacro    MacroEngine script added (BuildingTypeMapLocations.cshtml)        0,0297769772159245    0,000020
    umbracoMacro    Loading IMacroEngine script                                                         0,0297923755608419    0,000015
    umbracoMacro    Loading IMacroEngine script [done]                                               0,0332686374723172    0,003476

    The first time the macro hasn't been loaded which means it probably needs to do a call to the database to get the macro from the developer section. This will probably be cached and that's why the page is faster the second time loading. I don't think this is related to the caching you can use in the developer section macro since I disabled that.

    Now if I don't use a macro from the developer section, but a macro which uses filelocation and I debug the page the results I just showed aren't even there which means the time you see above is always skipped! This means the page is a lot faster on the first load and also faster on the second load.

    I did also test what happens if cache is enabled on a macro from the developer section. If I disable "Cache By Page" and cache it for 60 seconds the rendered html is also cached. I had a Razor menu with an active state and after it got cached the active state stayed on the previous menu item because that rendered html got cached. So I guess for the menu which has an active state you should use "Cache By Page" if you want to use caching on a macro from the developer section. I'm not going to do that since I'll be using filelocation and remove the macro from the developer section.

    Now all I need to test is what happens if I use the filelocation attribute and the cache attribute on a macro which isn't in the developer section. At least I know how I can speed up my website again :).

    Jeroen

  • Thomas Dolberg 74 posts 95 karma points
    Nov 10, 2011 @ 00:44
    Thomas Dolberg
    0

    Hi Jeroen,

     

    I just stumbled on your post and I also get some nice performance increases (though the control is not skipped as yours is). Rendering went from 0,030114s (macro created in the developer section) to 0,000153s (macro loaded from filelocation). Now I would like to do the same for my .ascx-controls but I get the following exception:

    umbracoMacroError loading MacroEngine script (file: , Type: ''

    No MacroEngine found for extension 'ascx'

      at umbraco.cms.businesslogic.macro.MacroEngineFactory.GetByExtension(String extension)

      at umbraco.cms.businesslogic.macro.MacroEngineFactory.GetByFilename(String filename)

      at umbraco.macro.loadMacroDLR(MacroModel macro)

      at umbraco.macro.renderMacro(MacroModel model, Hashtable pageElements, Int32 pageId)

     

    Do you know if it is possible to load ascx-controls as macros using the filelocation in some other way?

     

    thanks

    Thomas

  • Thomas Dolberg 74 posts 95 karma points
    Nov 10, 2011 @ 01:20
    Thomas Dolberg
    0

    Hi Jeroen,

     

    I just have an additional question. Have you found out whether it is possible to cache per page using the filelocation approach? Setting Cache="xx" does work, but it then caches the content across pages.

     

    thanks

    Thomas

  • andrew 13 posts 36 karma points
    Feb 23, 2012 @ 23:24
    andrew
    0

    Any idea if the Macro tag will take additional parameters to allow by page or by user caching?  That would be ideal -- otherwise, I guess you have to call it as a regular macro instead of calling the cshtml file directly.

Please Sign in or register to post replies

Write your reply to:

Draft