Macro performance - data context, documents

emma zelewsky asked on April 7, 2016 20:27

I have a macro to check the most recent modification to a custom doctype and adjust the number of cache minutes accordingly. It's used on a dynamic newsletter page, and the idea is to have the page update itself quickly when a new or corrected story is sent from staging to production.

The site is updated once a day so it doesn't make much sense set a low cache time, and the goal with the macro is to avoid the hassle of clearing the cache manually so the newsletter page is up to date before mailing.

Here's the macro-- {% if (DateTime.Now > CMSContext.CurrentSite.DataContext.Documents["/Articles"].AllChildren.ClassNames("custom.article").Columns("DocumentModifiedWhen").OrderBy("DocumentModifiedWhen DESC").FirstItem.DocumentModifiedWhen.AddMinutes(15)) {120} else {1} %}

My question is how to improve the macro's performance. It benchmarks at an average of 0.06733s, and debug shows a red bar for duration. I found that the more specific the document query, the better the performance, but I can't see how to slice the pie any thinner.

Thanks!

Recent Answers


Roman Hutnyk answered on April 7, 2016 21:06 (last edited on April 7, 2016 21:07)

Emma, I'm afraid macro is not right solution. You have to configure cache dependencies and Kentico will take care of everything else. See documentation for more details.

2 votesVote for this answer Mark as a Correct answer

emma zelewsky answered on April 7, 2016 21:41

Thanks, Roman! Setting a cache dependency was my first choice and makes the most sense, but I couldn't get it to work with the custom doctype. That's why I fell back to the admittedly hacky macro. I'll take another look at configuring cache keys.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on April 7, 2016 22:38

Yes, this is what I would do. BTW cache key and cache dependency are two different things, so maybe there is some confusion around this?! It does not really metter if you're using custom doc type or built in, you dependency should look similar to this:

node|corporatesite|/home

2 votesVote for this answer Mark as a Correct answer

emma zelewsky answered on April 8, 2016 18:27

Well, that was a worthwhile lesson learned... I gave the cache configs another shot and was able to get things working.

The difference was entering specific cache keys in the repeater (under System) and running the Output Cache Dependencies webpart on the page. The keys point to the doctype, the doc node and the node where repeater runs.

Previously, I entered cache settings in the repeater or the Output Cache Dependencies webpart -- not both.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.