Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Avoid calling getCacheTagsToInvalidateOnUpdate() twice #118

Merged
merged 1 commit into from
Feb 3, 2022

Conversation

squatto
Copy link
Contributor

@squatto squatto commented Jan 21, 2022

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.

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;
}

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;
}
```
@codecov
Copy link

codecov bot commented Feb 3, 2022

Codecov Report

Merging #118 (7bd849c) into master (7e77875) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #118   +/-   ##
=======================================
  Coverage   79.34%   79.34%           
=======================================
  Files           4        4           
  Lines         184      184           
=======================================
  Hits          146      146           
  Misses         38       38           
Impacted Files Coverage Δ
src/FlushQueryCacheObserver.php 52.50% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7e77875...7bd849c. Read the comment docs.

@rennokki rennokki merged commit 8e29612 into renoki-co:master Feb 3, 2022
@rennokki rennokki changed the title Get cache tags once when invalidating [fix] Avoid calling getCacheTagsToInvalidateOnUpdate() twice Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants