我在 Redis 中保存了非常小的数据,下面的程序按预期工作,这将允许我下载所有 key 。
redis-cli keys *
有什么方法可以得到keys+values *
吗?
最佳答案
没有命令,但您可以编写脚本来执行此操作。
您需要为每个键执行一个“类型”命令:
> type <key>
并根据响应执行:
- 对于“字符串”:
get <key>
- 对于“哈希”:
hgetall <key>
- 对于“列表”:
lrange <key> 0 -1
- 对于“集合”:
smembers <key>
- 对于“zset”:
zrange <key> 0 -1 withscores
请记住,对于哈希和排序集,您将获得键/分数和值。
一个可能的 sh
实现:
#!/bin/sh -eu
keys=`redis-cli keys '*'`
if [ "$keys" ]; then
echo "$keys" | while IFS= read -r key; do
type=`echo | redis-cli type "$key"`
case "$type" in
string) value=`echo | redis-cli get "$key"`;;
hash) value=`echo | redis-cli hgetall "$key"`;;
set) value=`echo | redis-cli smembers "$key"`;;
list) value=`echo | redis-cli lrange "$key" 0 -1`;;
zset) value=`echo | redis-cli zrange "$key" 0 -1 withscores`;;
esac
echo "> $key ($type):"
echo "$value" | sed -E 's/^/ /'
done
fi
但请注意:
Warning: consider
KEYS
as a command that should only be used in production environments 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.
关于redis - 在命令提示符下获取 Redis 键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8078018/