如何高效率查询redis下所有的key? 这个可能是每一个后端程序员在利用redis时常常碰着的场景,常日我们会利用keys或者scan命令去实行查询结果

KEYS patternWarning: consider KEYS as a command that should only be used in productionenvironments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]

redis官网keys命令已经给出警告不应该在生产环境中利用keys,建议我们利用scan命令查询, 但鉴于scan命令是基于游标迭代查询办法,每次查询都依赖上次的结果,以是常日运维环境下我们要想利用scan命令获取符合pattern的所有key,查询还是很费事,那有没有更简便的办法呢? 当前有,本日老码给大家推举的工具便是内部封装了scan命令,出输出符合结果的key外,还额外增加key对应的数据类型,如果追加删除参数也能帮我查询到的同时删除对应key。

NAME: redisKey - 按规则扫描过滤keyUSAGE: redisKey [global options] command [command options] [arguments...]COMMANDS: help, h Shows a list of commands or help for one commandGLOBAL OPTIONS: --host value, -h value (default: "127.0.0.1") --port value, -p value (default: 6379) --db value, -d value (default: 0) --password value --delete (default: false) --help show help

利用参考

phpredis获取key若何高效力的查询redis所有key强烈推举给你这个小对象 React

redisKey \ //这里mac下须要转义 查询所有keyredisKey \ --delete //查询所有key并删除

工具已经开源

https://github.com/ixqbar/redisKey

欢迎各位试用、收藏、点赞、评论!