Does the #cache property for render arrays cache content from a query when used in a controller? I have a custom page displaying a query result from the database. Before I attempt to cache it myself, I am trying to see if Drupal 8 render arrays support caching in anyway for authenticated users. My use case is a route and a controller on the node entity. Users go to node/id/something that displays a list of entries in a table. I would like to cache this in the controller. 

I am using the following code.

public function displayContent(NodeInterface $node) {
  // query the DB to generate content. 
   $content['#markup'] = $result_of_db_query; 

    $content['#cache'] = [
      'keys' =&gt; ['entity_view', 'node', $node-&gt;id()],
      'tags' =&gt; ['node:' . $node-&gt;id()],
      'max-age' =&gt; Cache::PERMANENT,
    ];
    return $content; 
}


It doesn't work: The database query is executed on every page request.
I am not sure if this is due to the fact this page is an administrative page and users must be authenticated.
Now that the topic is open again I will try to answer the questions:


  Does the #cache property for render arrays cache content from a query
  when used in a controller?


Yes, it does. The cache metadata will be used to cache the render array if you set cache keys and the metadata bubbles up to page level.


  The database query is executed on every page request. I am not sure if
  this is due to the fact this page is an administrative page and users
  must be authenticated.


On page level this is true, as for now it is presumed that admin pages are not cacheable, see Change Dynamic Page Cache's response policy from "deny admin routes" to "deny html admin routes"


  I expect the content to be cached and the query that generate the
  result to not run on every page request.


See the comments from @Berdir and this blog post how to do this by moving the query to a #pre_render callback:

https://dev.acquia.com/blog/drupal-8-performance-render-caching