Reputation: 1198
How can I count number of keys with value matching a pattern in redis-py? I've found methods scan, scan_iter but they search using the pattern on the name of the key.
Example of what I need:
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('key1', 'bar')
r.set('key2', 'bar')
r.set('key3', 'bar')
keys_num = len(list(r.unknown_scan(match='bar')))
print(keys_num)
>>3
I looked into documentation but could not find anything suitable. I thought about pulling all the keys and values and then loop them one by one counting values matching my pattern, but it looks inefficient, there should be a better way.
Upvotes: 3
Views: 2492