This is not a valid use case for drupal_flush_all_caches(). This function is for changed or new code. For content you have to use cache tags.
In the rare case, where it is not possible to add correct cache tags to all render arrays, you can invalidate the tag rendered
, which is added by default, even if no cache tags are specified:
\Drupal\Core\Cache\Cache::invalidateTags(['rendered']);
BTW entity operations are handled in transactions, so concurrent page request might not see the changes until they are committed to the database. In this case a cache clear (in any form) doesn't help.
Edit: Adding an example for the latest comment.
A nice solution would be a queue worker. It runs in the background, without that you have to wait for the admin panel to respond after a node save.
Example:
In the hook add a queue item with the entity ID:
Media::postSave()
\Drupal::queue('media_entity_thumbnail')->createItem(['id' => $translation->id()]);
Which is adding a queue item for this queue worker plugin:
/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php
<?php
namespace Drupal\media\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Process a queue of media items to fetch their thumbnails.
*
* @QueueWorker(
* id = "media_entity_thumbnail",
* title = @Translation("Thumbnail downloader"),
* cron = {"time" = 60}
* )
*/
class ThumbnailDownloader extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new class instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function processItem($data) {
/** @var \Drupal\media\Entity\Media $media */
if ($media = $this->entityTypeManager->getStorage('media')->load($data['id'])) {
$media->updateQueuedThumbnail();
$media->save();
}
}
}
By default cron runs only every 3 hours. If you need the static HTML in a shorter time then trigger the cron task (which also runs the queues) from outside the website. See https://www.drupal.org/docs/user_guide/en/security-cron.html