by sztanpet · 15y ago · 0 comments
the possibilities this brings is quite huge tho, so quite excited
So the whole point is to expose Redis in Lua in a way that is the most native related to the internals of Redis, and not the most comfortable/high-level.
local function accesscommand (t, key)
local f = function (...)
return redis(key, ...)
end
t[key] = f
return key
end
db = setmetatable({}, {__index = accesscommand})
function db.hmset (key, values)
local t = {}
for key, value in pairs(values) do
t[#t + 1] = key
t[#t + 1] = value
end
redis("hmset", unpack(t))
end
Any command without a function defined in the table would create a closure on the fly that calls that command like normal. (It also stores the closure in the table to speed up future calls.) Commands that could be implemented more elegantly using Lua semantics (like HMSET) have overrides defined in the db table directly, which prevents the closure-generating function from being called.