Redis scan count: How to force SCAN to return all keys matching a pattern?

前端 未结 3 1889
有刺的猬
有刺的猬 2020-12-13 14:01

I am trying to find out values stored in a list of keys which match a pattern from redis. I tried using SCAN so that later on i can use MGET to get

3条回答
  •  天命终不由人
    2020-12-13 14:35

    Reminder this is a trivial task if you're using the scan_iter method on the redis python library:

    from redis import StrictRedis
    
    redis = StrictRedis.from_url(REDIS_URI)
    
    keys = []
    for key in redis.scan_iter('foo:bar:*', 1000):
        keys.append(key)
    

    In the end, keys will contain all the keys you would get by applying @khanou 's method.

    This is also more efficient than doing shell scripts, since those spawn a new client on each iteration of the loop.

提交回复
热议问题