mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
make cache
This commit is contained in:
@@ -98,28 +98,37 @@ end
|
|||||||
|
|
||||||
-- msg could be any type of value
|
-- msg could be any type of value
|
||||||
|
|
||||||
local header_cache = setmetatable({}, {
|
local function make_cache(f)
|
||||||
|
return setmetatable({}, {
|
||||||
__mode = "kv",
|
__mode = "kv",
|
||||||
__index = function(t,k)
|
__index = f,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
local header_cache = make_cache(function(t,k)
|
||||||
local s = "\r\n$" .. k .. "\r\n"
|
local s = "\r\n$" .. k .. "\r\n"
|
||||||
t[k] = s
|
t[k] = s
|
||||||
return s
|
return s
|
||||||
end})
|
end)
|
||||||
|
|
||||||
local command_cache = setmetatable({}, {
|
local command_cache = make_cache(function(t,cmd)
|
||||||
__mode = "kv",
|
|
||||||
__index = function(t,cmd)
|
|
||||||
local s = "\r\n$"..#cmd.."\r\n"..cmd:upper()
|
local s = "\r\n$"..#cmd.."\r\n"..cmd:upper()
|
||||||
t[cmd] = s
|
t[cmd] = s
|
||||||
return 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 function compose_message(cmd, msg)
|
||||||
local t = type(msg)
|
local t = type(msg)
|
||||||
local lines = {}
|
local lines = {}
|
||||||
|
|
||||||
if t == "table" then
|
if t == "table" then
|
||||||
lines[1] = "*" .. (#msg+1)
|
lines[1] = count_cache[#msg+1]
|
||||||
lines[2] = command_cache[cmd]
|
lines[2] = command_cache[cmd]
|
||||||
local idx = 3
|
local idx = 3
|
||||||
for _,v in ipairs(msg) do
|
for _,v in ipairs(msg) do
|
||||||
@@ -171,7 +180,7 @@ end
|
|||||||
|
|
||||||
local function compose_table(lines, msg)
|
local function compose_table(lines, msg)
|
||||||
local tinsert = table.insert
|
local tinsert = table.insert
|
||||||
tinsert(lines, "*" .. #msg)
|
tinsert(lines, count_cache[#msg])
|
||||||
for _,v in ipairs(msg) do
|
for _,v in ipairs(msg) do
|
||||||
v = tostring(v)
|
v = tostring(v)
|
||||||
tinsert(lines,header_cache[#v])
|
tinsert(lines,header_cache[#v])
|
||||||
|
|||||||
Reference in New Issue
Block a user