Redis Python - how to delete all keys according to a specific pattern In python, without python iterating

后端 未结 9 656
暗喜
暗喜 2020-12-25 10:42

I\'m writing a django management command to handle some of our redis caching. Basically, I need to choose all keys, that confirm to a certain pattern (for example: \"prefix:

9条回答
  •  半阙折子戏
    2020-12-25 11:09

    According to my test, it will costs too much time if I use scan_iter solution (as Alex Toderita wrote).

    Therefore, I prefer to use:

    from redis.connection import ResponseError
    
    try:
        redis_obj.eval('''return redis.call('del', unpack(redis.call('keys', ARGV[1])))''', 0, 'prefix:*')
    except ResponseError:
        pass
    

    The prefix:* is the pattern.


    refers to: https://stackoverflow.com/a/16974060

提交回复
热议问题