From 30859a124478577f43ed06adbd0c993bdd1a2fdb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Fri, 13 Nov 2015 13:25:33 +0800 Subject: [PATCH] make cache --- lualib/redis.lua | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lualib/redis.lua b/lualib/redis.lua index 18cd6af0..3fc14a58 100644 --- a/lualib/redis.lua +++ b/lualib/redis.lua @@ -98,28 +98,37 @@ end -- msg could be any type of value -local header_cache = setmetatable({}, { - __mode = "kv", - __index = function(t,k) +local function make_cache(f) + return setmetatable({}, { + __mode = "kv", + __index = f, + }) +end + +local header_cache = make_cache(function(t,k) local s = "\r\n$" .. k .. "\r\n" t[k] = s return s - end}) + end) -local command_cache = setmetatable({}, { - __mode = "kv", - __index = function(t,cmd) +local command_cache = make_cache(function(t,cmd) local s = "\r\n$"..#cmd.."\r\n"..cmd:upper() t[cmd] = s return s - end}) + end) + +local count_cache = make_cache(function(t,k) + local s = "*" .. k + t[k] = s + return s + end) local function compose_message(cmd, msg) local t = type(msg) local lines = {} if t == "table" then - lines[1] = "*" .. (#msg+1) + lines[1] = count_cache[#msg+1] lines[2] = command_cache[cmd] local idx = 3 for _,v in ipairs(msg) do @@ -171,7 +180,7 @@ end local function compose_table(lines, msg) local tinsert = table.insert - tinsert(lines, "*" .. #msg) + tinsert(lines, count_cache[#msg]) for _,v in ipairs(msg) do v = tostring(v) tinsert(lines,header_cache[#v])