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

Invalidate cache by tag does not works #156

Open
nnrudakov opened this issue Jun 26, 2018 · 9 comments
Open

Invalidate cache by tag does not works #156

nnrudakov opened this issue Jun 26, 2018 · 9 comments
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@nnrudakov
Copy link

nnrudakov commented Jun 26, 2018

What steps will reproduce the problem?

Here a console command action:

    public function actionTest(): void
    {
        $cache = Yii::$app->cache;
        $this->stdout('Cache class is `' . \get_class($cache) . '`' . \PHP_EOL, Console::FG_GREY);
        $key1 = 'user_42_profile';
        $key2 = 'user_42_stats';
        $tag = 'user-123';
        $cache->delete($key1);
        $cache->delete($key2);
        $this->stdout('$key1 is ' . ($cache->exists($key1) ? '' : 'not ') . 'exists' . \PHP_EOL, Console::FG_GREY);
        $this->stdout('$key2 is ' . ($cache->exists($key2) ? '' : 'not ') . 'exists' . \PHP_EOL, Console::FG_GREY);
        $this->stdout('Store values...' . \PHP_EOL, Console::FG_GREY);

        $cache->set($key1, '', 0, new TagDependency(['tags' => $tag]));
        $cache->set($key2, '', 0, new TagDependency(['tags' => $tag]));

        $this->stdout('$key1 is ' . ($cache->exists($key1) ? '' : 'not ') . 'exists' . \PHP_EOL, Console::FG_GREY);
        $this->stdout('$key2 is ' . ($cache->exists($key2) ? '' : 'not ') . 'exists' . \PHP_EOL, Console::FG_GREY);
        $this->stdout('Going to delete keys by tag...' . \PHP_EOL, Console::FG_GREY);

        TagDependency::invalidate($cache, $tag);

        $this->stdout('$key1 is ' . ($cache->exists($key1) ? 'still ' : 'not ') . 'exists' . \PHP_EOL, Console::FG_GREY);
        $this->stdout('$key2 is ' . ($cache->exists($key2) ? 'still ' : 'not ') . 'exists' . \PHP_EOL, Console::FG_GREY);
    }

What's expected?

I expected that $key1 and $key2 would not exists in cache after deleting by tag.

What do you get instead?

Cache class is `yii\redis\Cache`
$key1 is not exists
$key2 is not exists
Store values...
$key1 is exists
$key2 is exists
Going to delete keys by tag...
$key1 is still exists
$key2 is still exists

Additional info

Q A
Yii vesion 2.0.15.1
Yii redis vesion 2.0.8
PHP version 7.2
Operating system Ubuntu
@cebe
Copy link
Member

cebe commented Jul 13, 2018

I guess this is because dependencies are evaluated only when you call get() not on exist()

@tendallas
Copy link

Have same problem still. I think that's because of extension just marked records as deleted. Redis worker flush them postponed.

P.S. get() doesn't helps at all

@TheBlueAssasin
Copy link

TheBlueAssasin commented Jan 18, 2019

In my case the reason for invalidate to not work was that the tags weren't saved at all because in the config i had 'serializer' => false. After I switched to the default PHP serializer, everything worked fine. This is because the dependency is not evaluated at all if the serializer is set to false

@samdark
Copy link
Member

samdark commented Jan 18, 2019

@nnrudakov was that the case for you?

@nnrudakov
Copy link
Author

Nope.
Here my config:

'cache' => [
    'class'     => yii\redis\Cache::class,
    'redis'     => 'redisConn',
    'keyPrefix' => 'my_pref'
],

In this case serializer is null, right? According to docs:

Defaults to null, meaning using the default PHP serialize() and unserialize() functions.

So, I have the default PHP serializer. What I'm doing wrong?

@sdangolmpt
Copy link

sdangolmpt commented Jun 5, 2019

any update on this issue? it doesn't work for me either.

@samdark
Copy link
Member

samdark commented Jun 6, 2019

No. Want to work on it?

@djidji01
Copy link

I guess this is because dependencies are evaluated only when you call get() not on exist()

This is the solution.

@B0rner
Copy link

B0rner commented Aug 29, 2023

I'm running into the same problem. Is there any workaround for that?
I have several functions, which needs to clear the cache, like this way:

$cache = \Yii::$app->userCache;
        TagDependency::invalidate($cache, 'ListOfPosts');

But this seams not to work, even 5 years after this issue was opened.
In my application, this problem came up more by accident. I wonder how many Yii2 installations in the world are subtly buggy because the developer didn't notice this bug.

In the last 5 years only a few people have contributed to this issue. Either this feature is rarely used or many don't notice the bug. Finally, this issue is equivalent to an application that generates inconsistent data due to a bug.
This is a show stopper in my eyes. It's good to know about this bug. It is better to create a slower application without a cache feature than a performant application that has wrong data.

//Edit: By the way: I'm using memcached as caching-engine and run into the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
None yet
Development

No branches or pull requests

8 participants