Drupal 8 block cacheing I have written a block, displayed on the right sidebar of my drupal 8 site. It reads the database, and shows today's reports. But the block's output is aggressively cached, so that it never shows the up-to-date details. The block's code is being called, and returns the correct data, but this is ignored. (Pages created by controller classes do similar work, but are not cached. What's the difference?). 

After reading many help pages, I'm frustrated; I can't get the block to show up-to-date details. The solution seems to involve setting '#cache' on the render array returned, but I can't see how. 

Help please!
It all depends on what you want to do exactly.  I have a block that is dependent on the user thats logged in, and it may also be changed from time to time based on some other code being run.

In this example  

'#cache'=&gt;[
        'contexts'=&gt; [
            'user',
            'route',
        ],
        'tags' =&gt; [
          'Block:smn',
        ],
      ],


It gives it context for which to cache and the tag allows me to invalidate the cache for just this block if I run some other code that will affect what is rendered

in this example if some entity is deleted i use hook_entity_delete

function foo_entity_delete($entity){
    $cache = \Drupal::service('cache_tags.invalidator')-&gt;invalidateTags(array('Block:smn'));

}


and my block will be removed from cache and the next time I visit the page it will be rendered with new information.

for more info on tags, and contexts you can cache with:
https://api.drupal.org/api/drupal/core!core.api.php/group/cache/8.2.x