2
我在CentOS的安装Redis的,我有这样的Redis的多个按键,Redis的命令行删除多个键
Product:<id>:<url>
如何删除所有Product:*:*
与CLI?
Redis版本:3.2.4 [最新推测]
谢谢!
我在CentOS的安装Redis的,我有这样的Redis的多个按键,Redis的命令行删除多个键
Product:<id>:<url>
如何删除所有Product:*:*
与CLI?
Redis版本:3.2.4 [最新推测]
谢谢!
没有内置的命令。您必须使用SCAN
命令获取与该模式匹配的所有密钥,然后使用DEL
命令删除这些密钥。
// scan from cursor 0 to get the next cursor and keys
SCAN 0 match Product:*:*
// next_cursor, Product:x1:y1, Product:x2:y2, ...
DEL Product:x1:y1 Product:x2:y2 ...
// scan from the next cursor until it return 0
SCAN next_cursor match Product:*:*
另一种解决方案是使用HASH
保存该模式键:
// set key value
HSET Products Product:<id>:<url> value
// remove a single key
HDEL Products Product:<id>:<url>
// remove all keys
DEL Products
使用redis-cli
工具,你可以做到以下几点:
redis-cli --scan --pattern 'Product:*:*' | xargs redis-cli DEL
你能给我范例扫描然后删除? –
@JohnFG我更新了答案。有关'SCAN'的更多详细信息,请查看文档。 –