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.
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 :).
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.
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):
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 :).
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:
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.
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.
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
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.
Sebastiaan,
Thought that would be the answer no worries.
Regards
Ismail
Yes, there is. Simply add Cache="xxx" attribute to the umbraco:Macro tag where xxx is the number of seconds to cache ;-)
That is awesome! I assume it works for all macros? Are there also attribute for cache personalized and by page?
@Niels: does this also work for inline macro's ?
<umbraco:Macro runat="server" language="razor">
@if(Model.umbracoNaviHide == "1") {
@("just a test")
}
</umbraco:Macro>
Damiaan: That's the only place it will work.. Cache for 5 minutes:
<umbraco:Macro runat="server" language="razor" Cache="300">
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
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.
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
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.
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
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
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
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.
is working on a reply...