How do I disable Twig and block cache for a specific module? I'm working on a module in Drupal 8 which retrieves time sensitive data from an external REST api. The data is processed and forwarded to a Block. The final data is sent to a Twig template like so:

...

public function build() {
  $build = [];

  $build['ticker_block'] = [
    '#theme' =&gt; 'module_theme',
    '#data' =&gt; $myService-&gt;data(),
  ];

  return $build;
}


This doesn't work on the production environment where cache is enabled and the rendered data shows up as stale. How do I disable cache for my module only, and do I have to do it for both the Twig template and Block plugin?
You can control the cache in the render array with the #cache element.

To disable the cache add:

$build['#cache']['max-age'] = 0;


In this case the block will not be cached for logged in users or anonymous users with a session.

If you want to disable the cache for anonymous users without a session:


  Disable it either by uninstalling the page_cache module completely or
  by triggering the kill switch, see
  How can I prevent a particular page being cached? for example.


The caching of the twig code is not connected to this, the twig code is only cached once, the first time it is used after you cleared the cache.

(edit acc. to comments of Berdir)