0

In Drupal 7, I found a way to reset cache on a specific page with:

$url = url('<front>',  array('absolute' => TRUE));
cache_clear_all($url, 'cache_page');

What is the equivalent way in Drupal 9?

4
  • 4
    Using cache tags and contexts. Here's the docs drupal.org/docs/8/api/cache-api/cache-api and this is how to invalidate caches with a certain tag drupal.org/docs/drupal-apis/cache-api/cache-tags#s-invalidating Commented Jan 5, 2023 at 10:53
  • Yes, there isn't a directly equivalent way in D9, as the way of handing caching was much improved between D7 and D8+.
    – Jaypan
    Commented Jan 7, 2023 at 17:00
  • why do you want to do this?
    – sonfd
    Commented Jan 8, 2023 at 1:37
  • I was adding a library to a specific page and it could be updated only through a configuration settings form, which would then reset the cache, so the library is properly updated. Commented Jan 8, 2023 at 18:29

1 Answer 1

2

Solved it like this:

$url = Url::fromUserInput($custom_url);
  if ($url instanceof UrlInterface) {
    if ($url->isRouted()) {
      $nid = $url->getRouteParameters()['node'];
      /** @var \Drupal\node\Entity\Node $node */
      $node = Node::load($nid);
      if ($node instanceof NodeInterface) {
        $tags = $node->getCacheTags();
        \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
      }
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.