Score:0

How to reset cache on a specific page by a given URL?

jp flag

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?

unusedspoon avatar
aq flag
Using cache tags and contexts. Here's the docs https://www.drupal.org/docs/8/api/cache-api/cache-api and this is how to invalidate caches with a certain tag https://www.drupal.org/docs/drupal-apis/cache-api/cache-tags#s-invalidating
Jaypan avatar
de flag
Yes, there isn't a directly equivalent way in D9, as the way of handing caching was much improved between D7 and D8+.
sonfd avatar
in flag
why do you want to do this?
Ivan Ginovski avatar
jp flag
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.
Score:2
jp flag

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);
      }
    }
  }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.