Skip to content

Invalidate data cache whenever any entry or category is saved #12208

Answered by brandonkelly
MoritzLost asked this question in Q&A
Discussion options

You must be logged in to vote

Here’s how you’d do it in Craft 3:

// Turn on element cache tag collection
Craft::$app->elements->startCollectingCacheTags();

// Execute the query
$query = $this->getQuery();
$result = $this->process($query);

// Stop collecting cache tag collection, and get the resulting TagDependency
$dependency = Craft::$app->elements->stopCollectingCacheTags();

// Cache the results with the dependency
$cache->set($cacheKey, $result, self::CACHE_DURATION, $dependency);

That will still work in Craft 4, but those two Elements service methods have been deprecated in favor of startCollectingCacheInfo() and stopCollectingCacheInfo(). The advantage of the new methods is that you will get the recommended ca…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@MoritzLost
Comment options

@brandonkelly
Comment options

@MoritzLost
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants