lua-resty-redis
lua-resty-redis copied to clipboard
Use lazy generation instead of hardcode commands?
trafficstars
In https://github.com/openresty/lua-resty-redis/blob/master/lib/resty/redis.lua#L27 there is a list of redis commands contains more than one hundred entries...
Well, why not use lazy generation instead? For example,
M.__index = function (self, name)
if _M[name] == nil then
_M[name] = function (self, ...)
do_cmd(self, name, ...)
end
end
return _M[name]
end
It is quite shorter.
IMHO, there are pros and cons to replace hardcore command list to lazy generation:
Cons:
- Bad for auto completion
- Require additional comparison
Pros:
- Easier to maintain. No need to keep update with redis's command list. Use hardcore command list has a problem. We may not update
lua-resty-redisas soon as the new version of redis is released. If the latest version of redis adds command X, people may useadd_commandsto work around. However, once we updatelua-resty-redisand add command X, they will generate method X twice. - No need to generate one hundred methods each time we require the package.(In most time, I only use 3~6 redis commands in a lua file, which is just 5% of whole commands!)
Yes, we should do that. We can specifically prepare the 10 most commonly used methods. Patches welcome!