Skip to content

Instantly share code, notes, and snippets.

@klovadis
Created March 15, 2013 15:03
Show Gist options
  • Save klovadis/5170485 to your computer and use it in GitHub Desktop.
Save klovadis/5170485 to your computer and use it in GitHub Desktop.
A Lua script for Redis that allows you to set hash values based on a dictionary table
-- sets all fields for a hash from a dictionary
local hmset = function (key, dict)
if next(dict) == nil then return nil end
local bulk = {}
for k, v in pairs(dict) do
table.insert(bulk, k)
table.insert(bulk, v)
end
return redis.call('HMSET', key, unpack(bulk))
end
-- example will set foo and baz in myhash
local mytable = { foo='bar', baz='asdf' }
hmset('myhash', mytable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment