Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- gets all fields from a hash as a dictionary
- local hgetall = function (key)
- local bulk = redis.call('HGETALL', key)
- local result = {}
- local nextkey
- for i, v in ipairs(bulk) do
- if i % 2 == 1 then
- nextkey = v
- else
- result[nextkey] = v
- end
- end
- return result
- end
- -- gets multiple fields from a hash as a dictionary
- local hmget = function (key, ...)
- if next(arg) == nil then return {} end
- local bulk = redis.call('HMGET', key, unpack(arg))
- local result = {}
- for i, v in ipairs(bulk) do result[ arg[i] ] = v end
- return result
- end
- -- setup a redis hash
- redis.call('HMSET', 'myhash',
- 'field1', 'value1',
- 'field2', 'value2',
- 'field4', 'value4',
- 'field5', 'value5',
- 'field3', 'value3');
- -- put the redis hash into a dictionary table
- local mytable = hgetall('myhash')
- local mytable2 = hmget('myhash', 'field4', 'field5')
- -- print key -> value for mytable
- print('mytable:')
- for k, v in pairs(mytable) do
- print(' ' .. k .. ' -> ' .. v)
- end
- -- print key -> value for mytable
- print('mytable2:')
- for k, v in pairs(mytable2) do
- print(' ' .. k .. ' -> ' .. v)
- end
Add Comment
Please, Sign In to add comment