I don't understand the way the handle my cache tag in magento 2. I have implemented DataObject/IdentityInterface.php for my models wishlist and wishlistitem. So when I create a wishlist for example I see my tags in in the dev tool of my browser for example. "advanced_wishlist_46,advanced_wishlist_48,advanced_wishlist_74,advanced_wishlist_75" and I see a new tag when a new wishlist is created when I am on my block to view my wishlists. I use varnish with my magento 2.
MY goal is to invalidate the cache when a wishlist is created or deleted. Without having to reload the browser manually.
I have created a method:
protected function clearWishlistCache(): void
{
$cache_context = $this->cacheContextFactory->create();
$ids = $this->wishListRepository->getAllWishlistIds();
$cache_context->registerEntities(WishList::CACHE_TAG, $ids);
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $cache_context]);
}
I call this method just after having created a new wishlist, and indeed when I create a new wishlist it works the page is refreshed and I see my new wishlist.
But when I use this method after or before deleting a wishlist, this time I don't see the result without manually reloading the page. And I finally notice that regarding the wishlist deletion, If I don't use this method, if I manually reload the page the wishlist list is correctly updated.
So I really don't understand how the cache tag and varnish work. How to use it and in which cases?