yiisoft / yii2-redis

Yii 2 Redis extension.
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
452 stars 183 forks source link

Invalidate cache by tag does not works #156

Open nnrudakov opened 6 years ago

nnrudakov commented 6 years ago

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 commented 6 years ago

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

tendallas commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@nnrudakov was that the case for you?

nnrudakov commented 5 years ago

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 commented 5 years ago

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

samdark commented 5 years ago

No. Want to work on it?

djidji01 commented 5 years ago

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

This is the solution.

B0rner commented 1 year ago

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.