Skip to content

Instantly share code, notes, and snippets.

@AndreLouisCaron
Created October 16, 2017 12:58
Show Gist options
  • Save AndreLouisCaron/13f2c6ba7618c0a2c3892a083f356300 to your computer and use it in GitHub Desktop.
Save AndreLouisCaron/13f2c6ba7618c0a2c3892a083f356300 to your computer and use it in GitHub Desktop.
Pick up to N keys that match a pattern on a large Redis instance
import itertools
def scan(c, pattern, n):
"""Find up to N Redis keys that match a pattern.
NOTE: even if this Python code looks innocuous, it involves
A LOT of work on the Redis instance, especially when
the number of matches is low with respect to the total
number of keys. Use with care!
"""
# SCAN can return duplicates.
keys = set()
cursor, results = c.scan(0, pattern)
while cursor != 0 and len(keys) < n:
# Fill up to K remaining slots.
keys = keys.union(itertools.islice(results, n - len(keys)))
cursor, results = c.scan(cursor, pattern)
return keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment