Redis 命令获取所有可用密钥?
是否有一个 Redis 命令可以获取数据库中的所有键?我见过一些 python-redis 库获取它们。但想知道是否可以通过 redis-client 实现。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否有一个 Redis 命令可以获取数据库中的所有键?我见过一些 python-redis 库获取它们。但想知道是否可以通过 redis-client 实现。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(16)
尝试查看
KEYS
命令。KEYS *
将列出存储在 redis 中的所有密钥。编辑:请注意
KEYS
文档页面顶部的警告:更新(V2.8或更高版本):
扫描
是 KEYS 的更好替代方案,因为它不会阻塞服务器,也不会消耗大量资源。更喜欢使用它。Try to look at
KEYS
command.KEYS *
will list all keys stored in redis.EDIT: please note the warning at the top of
KEYS
documentation page:UPDATE (V2.8 or greater):
SCAN
is a superior alternative toKEYS
, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.针对 Redis 2.8 及更高版本进行了更新
正如此问题之前答案的评论中所述,
KEYS
是一个潜在危险的命令,因为您的 Redis 服务器将无法执行其他操作它为它服务。KEYS
的另一个风险是它可能会消耗(取决于密钥空间的大小)大量 RAM 来准备响应缓冲区,从而可能耗尽服务器的内存。Redis 2.8 版本引入了 SCAN 系列命令,这些命令更加礼貌并且可用于相同的目的目的。
CLI 还提供了一种很好的使用方式:
Updated for Redis 2.8 and above
As noted in the comments of previous answers to this question,
KEYS
is a potentially dangerous command since your Redis server will be unavailable to do other operations while it serves it. Another risk withKEYS
is that it can consume (dependent on the size of your keyspace) a lot of RAM to prepare the response buffer, thus possibly exhausting your server's memory.Version 2.8 of Redis had introduced the SCAN family of commands that are much more polite and can be used for the same purpose.
The CLI also provides a nice way to work with it:
可能会发生这样的情况:使用 redis-cli 连接到远程 redis 服务器,然后命令:
不显示任何内容,或者更好的是,它显示:
(空列表或集)
如果您绝对确定您使用的 Redis 服务器就是您拥有数据的服务器,那么您的 redis-cli 可能没有连接到正确的 Redis 数据库实例。
正如 Redis 文档中提到的,新连接默认连接到 db 0。
就我而言,
KEYS
命令未检索结果,因为我的数据库为 1。为了选择所需的数据库,请使用 选择。db 由一个整数标识。
我发布此信息是因为以前的答案都没有解决我的问题。
It can happen that using redis-cli, you connect to your remote redis-server, and then the command:
is not showing anything, or better, it shows:
(empty list or set)
If you are absolutely sure that the Redis server you use is the one you have the data, then maybe your redis-cli is not connecting to the Redis correct database instance.
As it is mentioned in the Redis docs, new connections connect as default to the db 0.
In my case
KEYS
command was not retrieving results because my database was 1. In order to select the db you want, use SELECT.The db is identified by an integer.
I post this info because none of the previous answers was solving my issue.
获取 Redis 中的所有密钥
使用 --scan 选项获取所有密钥:
使用 KEYS 命令列出所有密钥:
Get All Keys In Redis
Get all keys using the --scan option:
List all keys using the KEYS command:
--> 从 redis-cli 获取所有键
--> 获取模式列表
这将生成以 'd' 开头的三个字符的键。
这将获得与键中的“t”字符匹配的键
-->通过命令行对键进行计数
-->或者您可以使用
dbsize
-->Get all keys from redis-cli
-->Get list of patterns
This will produce keys which start by 'd' with three characters.
This wil get keys with matches 't' character in key
-->Count keys from command line by
-->Or you can use
dbsize
请查看以下 Redis 备忘单。
要使用 redis-cli 获取 redis 键的子集,我使用以下命令
Take a look at following Redis Cheat Sheet.
To get a subset of redis keys with the redis-cli i use the command
SCAN 不需要客户端像 KEYS 那样将所有密钥加载到内存中。 SCAN 为您提供了一个可以使用的迭代器。我的 redis 中有一条 1B 记录,但我永远无法获得足够的内存来一次返回所有键。
下面是一个 python 代码片段,用于从存储中获取与模式匹配的所有键并删除它们:
SCAN doesn't require the client to load all the keys into memory like KEYS does. SCAN gives you an iterator you can use. I had a 1B records in my redis and I could never get enough memory to return all the keys at once.
Here is a python snippet to get all keys from the store matching a pattern and delete them:
是的,您可以使用此获取所有密钥
Yes, you can get all keys by using this
其中 * 是列出所有键的模式
where * is the pattern to list all keys
按键模式
返回与模式匹配的所有键。
警告:不建议使用此命令,因为在大型数据库上执行该命令时可能会破坏性能,而不是可以使用 扫描 或 SETS。
要使用的 KEYS 命令示例:
KEYS pattern
Returns all keys matching pattern.
Warning : This command is not recommended to use because it may ruin performance when it is executed against large databases instead of KEYS you can use SCAN or SETS.
Example of KEYS command to use :
为了获取 redis 服务器中可用的所有密钥,您应该打开 redis-cli 并输入:
按键*
为了获得更多帮助,请访问此页面:
此链接
In order to get all the keys available in redis server, you should open redis-cli and type:
KEYS *
In order to get more help please visit this page:
This Link
如果你的redis是集群,可以使用这个脚本
If your redis is a cluster,you can use this script
您可以简单地使用 redis-cli 连接到您的 redis 服务器,选择您的数据库并输入 KEYS *,请记住,它将为您提供所选 redis 数据库中存在的所有密钥。
You can simply connect to your redis server using redis-cli, select your database and type KEYS *, please remember it will give you all the keys present in selected redis database.
对于那些需要打字稿助手(使用 ioredis)
并使用以下方式调用它的人: scanKeysFromRedis(store, 'hello');
For the ones that wants a typescript helper (using ioredis)
and call it with: scanKeysFromRedis(store, 'hello');
如果您使用 Laravel 框架,那么您可以简单地使用:
在 Core PHP 中:
If you are using Laravel Framework then you can simply use this:
In Core PHP:
我们应该将 --scan --pattern 与 redis 2.8 及更高版本一起使用。
您可以尝试在 redis-cli 之上使用此包装器。
https://github.com/VijayantSoni/redis-helper
We should be using --scan --pattern with redis 2.8 and later.
You can try using this wrapper on top of redis-cli.
https://github.com/VijayantSoni/redis-helper