search

Home  >  Q&A  >  body text

redis删除多个键出现问题

在看《redis入门指南》这篇文章中,第三章第二节介绍删除键的时候,有这样一段描述:

DEL命令的参数不支持通配符,但我们可以结合Linux的管道和xargs命令自己实现删除所有符合规则的键。比如要删除所有以“user:”开头的键,就可以执行redis-cli
KEYS "user:" | xargs redis-cli
DEL。另外,由于DEL命令支持多个键作为参数,所以还可以执行redis-cli DEL 'redis-cli KEYS
"user:
"'来达到同样的效果,性能更好。

但是我测试的时候,第一个结合管道的命令能起作用,但是第二个貌似不起作用。
刚学习redis,请大神指教。

黄舟黄舟2798 days ago614

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-22 09:02:42

    You read it wrong.

    ` and ' are two different things.

    What they wrote is

    他们写的是
    redis-cli DEL `redis-cli KEYS "user:"`
    
    你写的是
    redis-cli DEL 'redis-cli KEYS "user:"'

    Pay attention to the difference.

    Single quotes directly treat the content within the single quotes as a string,
    redis-cli DEL 'redis-cli KEYS "user:"'redis-cli DEL 'redis-cli KEYS "user:"'
    就是删除redis中名叫redis-cli KEYS "user:" is to delete the key named

    in redis,

    The back quotes are to first execute the content in the back quotes to get the result, that is to say,

    redis-cli DEL `redis-cli KEYS "user:"`
    redis-cli KEYS "user:"The first thing to execute is

    , and the result is taken out (for example:)

    user01
    user02
    user03

    Then execute

    redis-cli DEL user01 \n user02 \n user03

    This problem is not with redis, but with the difference between single quotes and back quotes in the shell. 🎜

    reply
    0
  • Cancelreply