Last active
August 13, 2021 17:26
-
-
Save clayadavis/16256301ed734c9a86ea137085fa50ae to your computer and use it in GitHub Desktop.
Redis list iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lrange_iter(r, key, N_max=None, step=10**5): | |
if N_max is None: | |
starts = itertools.count(step=step) | |
else: | |
starts = range(0, N_max, step) | |
for start in starts: | |
end = start + step - 1 | |
if N_max is not None: | |
end = min(end, N_max - 1) | |
vals = r.lrange(key, start, end) | |
if vals: | |
yield from vals | |
else: | |
break | |
def destructive_lrange_iter(r, key, N_max=None, step=10**5): | |
def ends(): | |
if N_max is None: | |
yield from itertools.repeat(step) | |
else: | |
for start in range(0, N_max, step): | |
yield min(step, N_max - start) | |
def multi_lpop(key, N): | |
vals = r.lrange(key, 0, N-1) | |
if vals: | |
r.ltrim(key, len(vals), -1) | |
return vals | |
for N in ends(): | |
vals = multi_lpop(key, N) | |
if vals: | |
yield from vals | |
else: | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment