Skip to content

Instantly share code, notes, and snippets.

@lukeasrodgers
Last active July 11, 2016 19:05
Show Gist options
  • Save lukeasrodgers/d2278356e683dba858d6a24c12c0fb68 to your computer and use it in GitHub Desktop.
Save lukeasrodgers/d2278356e683dba858d6a24c12c0fb68 to your computer and use it in GitHub Desktop.
scan-style iteration over redis list in ruby, making multiple roundtrips
# useful if k is a very large list, and you don't want to do it all in one request
# note that consistency will not be guaranteed here (i.e. list can change during iteration)
def scan_list(k, range=1000, &block)
i = 0
result = []
while l = HLR.lrange(k, i, i+range)
break if l.size == 0
if block_given?
yield l
else
result.concat(l)
end
i += range + 1
end
result unless block_given?
end
# hackily dump set contents to JSON file
# probably won't work if set contains values with unescaped single quotes
def dump_set_to_json(filename, setname)
File.open(filename, 'w+') do |file|
file.write("[")
HLR.sscan_each(setname) do |item|
file.write(item)
file.write(',')
end
file.pos = file.pos - 1 # jump back to overwrite last ','
file.write("]")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment