Skip to content

Commit 7bd849c

Browse files
authored
Get cache tags once when invalidating
I'm using an overridden `getCacheTagsToInvalidateOnUpdate()` to track how many times each tag is invalidated. The way `invalidateCache()` was originally written calls the `getCacheTagsToInvalidateOnUpdate()` method twice, resulting in the tags being counted twice for each invalidation. This change simply assigns the tags to a variable and uses the variable instead of calling the method directly. FWIW, this is what I'm doing to track the tag invalidations. Just a simple array saved to the cache. ```php public function getCacheTagsToInvalidateOnUpdate($relation = null, $pivotedModels = null): array { $tags = $this->getCacheBaseTags(); $invalidations = Cache::get('cache-invalidations', []); foreach ($tags as $tag) { $invalidations[$tag] = ($invalidations[$tag] ?? 0) + 1; } Cache::forever('cache-invalidations', $invalidations); return $tags; } ```
1 parent 7e77875 commit 7bd849c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/FlushQueryCacheObserver.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ protected function invalidateCache(Model $model, $relation = null, $pivotedModel
154154
{
155155
$class = get_class($model);
156156

157-
if (! $model->getCacheTagsToInvalidateOnUpdate($relation, $pivotedModels)) {
157+
$tags = $model->getCacheTagsToInvalidateOnUpdate($relation, $pivotedModels);
158+
159+
if (! $tags) {
158160
throw new Exception('Automatic invalidation for '.$class.' works only if at least one tag to be invalidated is specified.');
159161
}
160162

161-
$class::flushQueryCache(
162-
$model->getCacheTagsToInvalidateOnUpdate($relation, $pivotedModels)
163-
);
163+
$class::flushQueryCache($tags);
164164
}
165165
}

0 commit comments

Comments
 (0)