lua-resty-redis icon indicating copy to clipboard operation
lua-resty-redis copied to clipboard

Use lazy generation instead of hardcode commands?

Open spacewander opened this issue 9 years ago • 1 comments
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-redis as soon as the new version of redis is released. If the latest version of redis adds command X, people may use add_commands to work around. However, once we update lua-resty-redis and 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!)

spacewander avatar May 24 '16 16:05 spacewander

Yes, we should do that. We can specifically prepare the 10 most commonly used methods. Patches welcome!

agentzh avatar May 24 '16 18:05 agentzh