How to prevent a block from being cached? After login. I'm adding one sidebar block. Once I clear cache then only updates variable or it's value.

namespace Drupal\test_demo\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides 'Sidebar' Block
 *
 * @Block(
 *   id = "sidebar_block",
 *   admin_label = @Translation("Sidebar Block"),
 *   category = @Translation("Sidebar Block"),
 * )
 */

class SidebarBlock extends BlockBase {
  public function build() {
    return [
      '#markup' =&gt; $this-&gt;t('Sidebar'),
      '#cache' =&gt; [
            'max-age' =&gt; 0,
          ]
      ];
  }
}


In mytheme.theme

function theme_preprocess_block(array &amp;$variables) {
  echo $variables['elements']['#id'];
}


How can I stop that block to being cached. So once I refresh the page. It loads the value immediately.
Got the solution via https://stefvanlooveren.me/blog/disable-block-caching-drupal-8

Add below function to your block

https://stefvanlooveren.me/blog/disable-block-caching-drupal-8

public function getCacheMaxAge() {
    return 0;
}


https://www.drupal.org/docs/8/api/cache-api/cache-max-age