Using #cache key on a render array generates 2 entries per role, how to change that? On a page controller I have a method that displays the content via a render array. It also caches the rendered output using the #cache key:

  public function viewContent(NodeInterface $node) {
    $content = array(
     '#entity' =&gt; $node,
     '#sorted' =&gt; TRUE,
     '#pre_render' =&gt; [[$this, 'buildList']],
     // Cache the render array until the lock history/node is changed.
     '#cache' =&gt; [
      'keys' =&gt; ['my_module', 'node', $node-&gt;id()],
      'tags' =&gt; ['my_module:node:' . $node-&gt;id()],
     ],
   );
    return $content;
  }


But I noticed that drupal is generating 2 cache entries per user role in the cache render table as you can see in the screenshot:


Is it possible to generate only 1 set of entries for all users and manually pick the cid using the builtin render #cache properties?
Yes, by default cached render arrays are varied by the hashed user permissions. See this container parameter:

/sites/default/services.yml

parameters:
  renderer.config:
    # Renderer required cache contexts:
    #
    # The Renderer will automatically associate these cache contexts with every
    # render array, hence varying every render array by these cache contexts.
    #
    # @default ['languages:language_interface', 'theme', 'user.permissions']
    required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions']


Though this should be changed only in rare cases, because caching content with the wrong user permissions can cause security issues.

If you don't want the variations of the render cache, another approach might be to cache the query results directly:

use Drupal\Core\Cache\Cache;

  if ($cache = \Drupal::cache()-&gt;get($cid)) {
    $data = $cache-&gt;data;
  }
  else {
    $data = my_module_query();
    \Drupal::cache()-&gt;set($cid, $data, Cache::PERMANENT, $tags);
  }


Then you can manually pick a cid. With the cached query results the rendering of the list shouldn't take too much cpu time and so probably you don't need render caching anymore.