Skip to content

Instantly share code, notes, and snippets.

@mrkvm
Created October 22, 2012 04:47
Show Gist options
  • Save mrkvm/3929686 to your computer and use it in GitHub Desktop.
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.
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