0

I have a module that defines route /x and assigns it the controller ViewNodes::show. This takes no parameters and the show method simply returns a nearly blank render array:

[
'#theme' => 'view_nodes', 
'#cache' => ['tags' => ['node_list'], 'max-age' => Cache::PERMANENT, 'bin' => 'render'], 
'#pre_render' => [[$this, 'buildNodeListView']]
];

The buildNodeListView then takes care of doing all the expensive logic and SQL queries to build the array. This all works fine and the page displays as expected, but devel's WebProfiler shows the SQL queries for building the render array are still being executed even when they should have been cached. This suggests buildNodeListView is always being called, which means #pre_render is always being called, which means the render array is always a miss. Am I mistaken here?

The route shows up under "bin dynamic_page_cache" and is always marked as MISS. And in the HTTP headers, there's always X-DRUPAL-DYNAMIC-CACHE: UNCACHEABLE

Is there something I'm doing wrong, or things I should check to further debug this? My settings.local.php has

//$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

and

 //$settings['cache']['bins']['render'] = 'cache.backend.null';

They're both commented out so the caching should be enabled. The Dynamic Page Cache module is also enabled. And my routes do not have the "no_cache" option.

1

You need cache keys

'#cache' => ['keys' => ['key1', 'key2', ...], ...]

and this is not the dynamic page cache, it is the standard render cache which will take care of this.

  • Thanks, the render array is now being cached as expected! – eo1919 Jul 11 '17 at 16:24

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .