Hard-coded blocks and cache

Permalink
Here's a question I asked in Totally Random stream on February 10th:

----------
If I hard-code blocks to template, will they get cached just like "normal" blocks added to areas?
----------


There wasn't an definite answer available at the moment, so maybe we can continue discussion here. The caching question is not really critical one, but I just wanted to ask it considering site performance.

 
andrew replied on at Permalink Best Answer Reply
andrew
Looking into this it looks like "yes" and "no." It depends on the method. If you do this:

$supf = Block::getByName('Header Superfish');
$supf->display();


or

$supf = Stack::getByName('Header Superfish');
$supf->display();


It should cache, because you're dealing with real block objects you've defined in a stack or a scrapbook (pre 5.5) somewhere. If you hard-code the block like this:

$bt = BlockType::getByHandle('autonav');
$bt->controller->displayPages = 'top';
$bt->controller->orderBy = 'display_asc';
$bt->render('templates/header_menu');


There will be no caching, because there has to actually be a block object stored in the database somewhere for caching to work.
aghouseh replied on at Permalink Reply
aghouseh
This is actually very interesting. IconicSchema brought up a great point on IRC regarding using a hard-coded autonav block in a site with a large sitemap where you are doing page data gathering (for images/attributes/whatnot) and how this would severely hamper the speed of every page on that site as a result.

Great question, Ale; this is going to actually cause me to change some methodology with site-building to speed things up a bit. It sounds like hard-coding a stack is probably the best way to go.
Ale replied on at Permalink Reply
Thanks for taking the time to explain the methods.

Previously I've used the Block::getByName() quite much, but usually set up some navigations directly to the template (the getByHandle()... method). For now on, I think I'll set all of them up to Scrapbooks/Stacks for better performance.
melat0nin replied on at Permalink Reply
melat0nin
Hi Andrew,

Sorry to necro a year-old thread but is this still the case with the new caching mechanisms in 5.6.1?
webpresso replied on at Permalink Reply
webpresso
Hello Andrew

Is this still the case that hardcoded blocks are not cached after implementing the new cache mechanism in 5.6.1?

Thanks for a short statement on this.
best regards, Roland
VPenkov replied on at Permalink Reply
VPenkov
Blocks now seem to be cached but I would also like an update on that.