Here is the code that flush cache on first hit of a day:
public function build() {
...
...
...
$this->handleCacheTag($build['#cache']['tags']);
}
protected function handleCacheTag(&$tags) {
// Get the current date number.
$current_date_number = date('z');
$last_date_number = \Drupal\Core\Site\Settings\Drupal::getstate()->get('module_name.foo.date_counter', $current_date_number);
// If last date in settings is different from current day, invalidate
// tags.
if ($last_date_number < $current_date_number) {
$invalid_tags = range($last_date_number, $current_date_number);
array_walk($invalid_tags, function(&$invalid_tag){
$invalid_tag = 'foo:date.' . $invalid_tag;
});
Cache::invalidateTags($invalid_tags);
}
Settings\Drupal::setstate()->set('module_name.foo.date_counter', $current_date_number);
$tags[] = 'foo:date.' . $current_date_number;
}