Created
October 22, 2012 04:47
-
-
Save mrkvm/3929686 to your computer and use it in GitHub Desktop.
Simple Ruby method to do a multi-set on a bunch of Redis keys together.
This file contains hidden or 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
require 'redis' | |
# Simple Lua script to set all the given keys to the given values. | |
# Note the magic of this is that it's done atomically. Yay atomicity! | |
MultiSetScript = <<EOF | |
for i = 1, #KEYS do | |
redis.call('SET', KEYS[i], ARGV[i]) | |
end | |
EOF | |
def multi_set(keys, vals) | |
unless keys.is_a? Array and vals.is_a? Array | |
raise ArgumentError, "keys and values must be arrays" | |
end | |
unless keys.length == vals.length | |
raise ArgumentError, "number of keys must be same as number of values" | |
end | |
Redis.new.eval MultiSetScript, keys, vals | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment