Redis Stack Exchange how to delete or get keys by pattern

送分小仙女□ 提交于 2019-11-28 07:30:46

问题


I installed Stack Exchange redis client in C#. I can only delete one key or array of keys but I don't know how to delete keys with prefix. Or another solution can be first get all keys by pattern and then delete them. But I don't know how to get keys by pattern too.


回答1:


Deletion is separate by key, unless you are flushing the entire database.

Key scanning is readily available on the IServer API, and is discussed much more here: https://stackexchange.github.io/StackExchange.Redis/KeysScan

However, it should still usually be avoided in production - that isn't the intended use-case for redis.




回答2:


You can do as the following to batch delete items from redis cache. (StackExchange.Redis.StrongName v1.0.488)

foreach (var ep in _muxer.GetEndPoints())
{
    var server = _muxer.GetServer(ep);
    var keys = server.Keys(database: _redisDatabase, pattern: pattern + "*").ToArray();
    _db.KeyDeleteAsync(keys);
}

_muxer is instance of ConnectionMultiplexer

It does not delete by pattern as you asked but much faster than deleting each key separately.



来源:https://stackoverflow.com/questions/26488830/redis-stack-exchange-how-to-delete-or-get-keys-by-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!