yiisoft / yii2-redis

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

yii cache/flush-all is dangerous #182

Closed shushenghong closed 4 years ago

shushenghong commented 5 years ago

if use redis as cache, yii cache/flush-all is very dangerous, since it will flushdb, if there are other data inside the same redis db, they all be cleared.

image

samdark commented 5 years ago

@shushenghong is there any safer approach?

shushenghong commented 5 years ago

i suggest use scan command with keyPrefix, and set default keyPrefix like redis session.

loop
    scan cursor match keyPrefix* count 100
    del key1 key2 ...
samdark commented 5 years ago

How would that work with thousands of keys?

shushenghong commented 5 years ago

another suggestion, if we use redis hashes to store cache, one hash store no more than 200 fields e.g., so we can avoid this problem?

samdark commented 5 years ago

That would require changing the way cache is stored significantly. I'm not sure about performance of such solution compared to current one.

flaviovs commented 4 years ago

This is a serious issue, and deserves at least a huge red bold warning in the documentation while not fixed. In our case, we do not use any app data in Redis, but we do use the extension to store cache entries and session and mutexes. Needless to say, wiping out the latter have huge implications.

BTW, a workaround we're using here is to use a Redis "database" only for cache components. That way, flushing caches doesn't affect sessions and mutexes.

rob006 commented 4 years ago

PR: https://github.com/yiisoft/yii2-redis/pull/200