Jump to content

Using $files->render() in a loop is expensive, cache it!


elabx
 Share

Recommended Posts

Example:

foreach($list_of_cards as $card){
    // cache cleared then selector matches
	$files->render("inc/some-include", $vars , ["cache" => "template=some_template", "name" => "template_cache_{$card->id}"])
}

Available since version 3.0.130

  • Like 7
Link to comment
Share on other sites

You're right @monollonom, it doesn't seem to cache per render per call which would be the most useful case!

Looking at the usage in TemplateFile class, it seems the options passed to render() get passed on to $cache->renderFile(), where we can pass on the name paremeter to specify a unique cache name. Updated the example!

5 hours ago, monollonom said:

Thanks for sharing, I should look into this as I also use this a lot.

I had a very simple case where I was delegating the rendering of  list items to another file and I was very surprised how slow everything turned, ProfilerPro was very usefull figuring this out. It wasn't even a big list, just like 40-50 items. I kept debugging what was inside the include file until finally figuring out it was the render() call that killed performance.

 

  • Like 2
Link to comment
Share on other sites

Interesting! Unfortunately it doesn’t seem like this can be used with $page->render(), because it’s built outside of TemplateFile::render(), but I’ve only taken a quick look at the code on my phone, so I may be wrong… I have a lot of places where I wrap stuff like this in cache()->get().

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...