mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
simple redis
This commit is contained in:
34
main.lua
34
main.lua
@@ -1,16 +1,22 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
|
||||||
print("Server start")
|
skynet.dispatch()
|
||||||
local launcher = skynet.launch("snlua","launcher.lua")
|
|
||||||
print("launcher", launcher)
|
skynet.start(function()
|
||||||
local console = skynet.launch("snlua","console.lua")
|
print("Server start")
|
||||||
print("console",console)
|
local launcher = skynet.launch("snlua","launcher.lua")
|
||||||
local watchdog = skynet.launch("snlua","watchdog.lua")
|
print("launcher", launcher)
|
||||||
print("watchdog",watchdog)
|
local console = skynet.launch("snlua","console.lua")
|
||||||
local gate = skynet.launch("gate","8888 4 0")
|
print("console",console)
|
||||||
print("gate",gate)
|
local watchdog = skynet.launch("snlua","watchdog.lua")
|
||||||
local db = skynet.launch("snlua","simpledb.lua")
|
print("watchdog",watchdog)
|
||||||
print("simpledb",db)
|
local gate = skynet.launch("gate","8888 4 0")
|
||||||
local connection = skynet.launch("connection","256")
|
print("gate",gate)
|
||||||
print(connection)
|
local db = skynet.launch("snlua","simpledb.lua")
|
||||||
skynet.exit()
|
print("simpledb",db)
|
||||||
|
local connection = skynet.launch("connection","256")
|
||||||
|
print("connection",connection)
|
||||||
|
local redis = skynet.call(".launcher", "broker .redis snlua redis-cli.lua 127.0.0.1:6379")
|
||||||
|
print("redis",redis)
|
||||||
|
skynet.exit()
|
||||||
|
end)
|
||||||
@@ -9,14 +9,27 @@ local fd
|
|||||||
local write_fd
|
local write_fd
|
||||||
local readline_fd
|
local readline_fd
|
||||||
local read_fd
|
local read_fd
|
||||||
|
local close_fd
|
||||||
|
|
||||||
local function init_fd(fdstr)
|
local function init_fd(fdstr)
|
||||||
fd = fdstr
|
fd = fdstr
|
||||||
write_fd = "WRITE "..fd.." "
|
write_fd = "WRITE "..fd.." "
|
||||||
readline_fd = "READLINE ".. fd .." \r\n"
|
readline_fd = "READLINE ".. fd .." \r\n"
|
||||||
read_fd = "READ " .. fd .. " "
|
read_fd = "READ " .. fd .. " "
|
||||||
|
close_fd = "CLOSE "..fd
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function init()
|
||||||
|
fd = skynet.call(".connection", "CONNECT " .. redis_server)
|
||||||
|
if fd == nil then
|
||||||
|
print("Connect to redis server error : ", redis_server)
|
||||||
|
skynet.exit()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
init_fd(fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local function compose_message(msg)
|
local function compose_message(msg)
|
||||||
local lines = { "*" .. #msg }
|
local lines = { "*" .. #msg }
|
||||||
for _,v in ipairs(msg) do
|
for _,v in ipairs(msg) do
|
||||||
@@ -44,7 +57,7 @@ redcmd[42] = function(data) -- '*'
|
|||||||
local data = skynet.call(".connection", read_fd .. bytes)
|
local data = skynet.call(".connection", read_fd .. bytes)
|
||||||
table.insert(result, string.sub(data,1,-3))
|
table.insert(result, string.sub(data,1,-3))
|
||||||
end
|
end
|
||||||
skynet.ret(skynet.pack(unpack(bulk)))
|
skynet.ret(skynet.pack(true,bulk))
|
||||||
end
|
end
|
||||||
|
|
||||||
redcmd[36] = function(data) -- '$'
|
redcmd[36] = function(data) -- '$'
|
||||||
@@ -54,7 +67,7 @@ redcmd[36] = function(data) -- '$'
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local firstline = skynet.call(".connection", read_fd .. (bytes + 2))
|
local firstline = skynet.call(".connection", read_fd .. (bytes + 2))
|
||||||
skynet.ret(skynet.pack(string.sub(firstline,1,-3)))
|
skynet.ret(skynet.pack(true,string.sub(firstline,1,-3)))
|
||||||
end
|
end
|
||||||
|
|
||||||
redcmd[43] = function(data) -- '+'
|
redcmd[43] = function(data) -- '+'
|
||||||
@@ -71,21 +84,30 @@ end
|
|||||||
|
|
||||||
skynet.dispatch(function(msg, sz, session, address)
|
skynet.dispatch(function(msg, sz, session, address)
|
||||||
local message = { skynet.unpack(msg,sz) }
|
local message = { skynet.unpack(msg,sz) }
|
||||||
skynet.send(".connection", write_fd .. compose_message(message))
|
local write_cmd = write_fd .. compose_message(message)
|
||||||
local result = skynet.call(".connection", readline_fd)
|
local result
|
||||||
|
while true do
|
||||||
|
skynet.send(".connection", write_cmd )
|
||||||
|
result = skynet.call(".connection", readline_fd)
|
||||||
|
if result then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
-- reconnect
|
||||||
|
if init() then
|
||||||
|
skynet.ret(skynet.pack(false , "Disconnected"))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
local firstchar = string.byte(result)
|
local firstchar = string.byte(result)
|
||||||
local data = string.sub(result,2)
|
local data = string.sub(result,2)
|
||||||
local f = redcmd[firstchar]
|
local f = redcmd[firstchar]
|
||||||
assert(f)
|
if f == nil then
|
||||||
f(data)
|
skynet.ret(skynet.pack(false , "Invalid result"))
|
||||||
|
skynet.send(".connection", close_fd)
|
||||||
|
init()
|
||||||
|
else
|
||||||
|
f(data)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(init)
|
||||||
fd = skynet.call(".connection", "CONNECT " .. redis_server)
|
|
||||||
if fd == nil then
|
|
||||||
print("Connect to redis server error : ", redis_server)
|
|
||||||
skynet.exit()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
init_fd(fd)
|
|
||||||
end)
|
|
||||||
|
|||||||
47
redis.lua
Normal file
47
redis.lua
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local string = string
|
||||||
|
local table = table
|
||||||
|
local unpack = unpack
|
||||||
|
|
||||||
|
local redis = {}
|
||||||
|
|
||||||
|
local command = {}
|
||||||
|
|
||||||
|
redis.cmd = command
|
||||||
|
|
||||||
|
setmetatable(command, { __index = function(t,k)
|
||||||
|
local f = function(...)
|
||||||
|
return skynet.call(".redis", skynet.unpack, skynet.pack(k, ...))
|
||||||
|
end
|
||||||
|
t[k] = f
|
||||||
|
return f
|
||||||
|
end})
|
||||||
|
|
||||||
|
function command.EXISTS(key)
|
||||||
|
local result , exists = skynet.call(".redis", skynet.unpack, skynet.pack("EXISTS", key))
|
||||||
|
exists = exists ~= 0
|
||||||
|
return result, exists
|
||||||
|
end
|
||||||
|
|
||||||
|
local function split(cmd)
|
||||||
|
local cmds = {}
|
||||||
|
for v in string.gmatch(cmd,"[^ ]+") do
|
||||||
|
table.insert(cmds,v)
|
||||||
|
end
|
||||||
|
return unpack(cmds)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function send_command(cmd, ...)
|
||||||
|
return command[cmd](...)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function redis.send(cmd, more, ...)
|
||||||
|
if more == nil then
|
||||||
|
send_command(split(cmd))
|
||||||
|
else
|
||||||
|
send_command(cmd, more, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return redis
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
local redis = require "redis"
|
||||||
|
|
||||||
skynet.dispatch()
|
skynet.dispatch()
|
||||||
|
|
||||||
local command ="*2\r\n$3\r\nGET\r\n$1\r\nA\r\n"
|
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
local cli = skynet.call(".launcher","broker redis snlua redis-cli.lua 127.0.0.1:7379")
|
print(redis.cmd.EXISTS("A"))
|
||||||
print("redis-cli:", cli)
|
print(redis.cmd.GET("A"))
|
||||||
assert(cli)
|
print(redis.cmd.SET("A","hello world"))
|
||||||
print(skynet.call(cli, skynet.unpack, skynet.pack("GET","A")))
|
|
||||||
skynet.exit()
|
skynet.exit()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user