mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 11:33:09 +00:00
redis api back compatible
This commit is contained in:
@@ -19,8 +19,15 @@ local meta = {
|
||||
}
|
||||
|
||||
function redis.connect(dbname)
|
||||
local fd = socket.open(name[dbname])
|
||||
return setmetatable( { __handle = fd, __mode = false }, meta )
|
||||
local db_conf = name[dbname]
|
||||
local fd = socket.open(db_conf.host, db_conf.port or 6379)
|
||||
assert(fd)
|
||||
local r = setmetatable( { __handle = fd, __mode = false }, meta )
|
||||
if db_conf.db ~= nil then
|
||||
r:select(db_conf.db)
|
||||
end
|
||||
|
||||
return r
|
||||
end
|
||||
|
||||
function command:disconnect()
|
||||
@@ -108,7 +115,8 @@ setmetatable(command, { __index = function(t,k)
|
||||
socket.write(fd, compose_message { cmd, ... })
|
||||
local ok, ret = read_response(fd)
|
||||
socket.unlock(fd)
|
||||
return ok, ret
|
||||
assert(ok, ret)
|
||||
return ret
|
||||
end
|
||||
end
|
||||
t[k] = f
|
||||
@@ -127,6 +135,13 @@ function command:exists(key)
|
||||
return exists
|
||||
end
|
||||
|
||||
function command:sismember(key, value)
|
||||
assert(not batch, "sismember can't used in batch mode")
|
||||
local result, ismember = skynet.call( self.__handle, "lua" , "SISMEMBER", key, value)
|
||||
assert(result, ismember)
|
||||
return ismember ~= 0
|
||||
end
|
||||
|
||||
function command:batch(mode)
|
||||
if mode == "end" then
|
||||
local fd = self.__handle
|
||||
|
||||
@@ -157,8 +157,6 @@ function socket.readline(fd, sep)
|
||||
end
|
||||
|
||||
function socket.write(fd, msg, sz)
|
||||
assert(coroutine.running() == READTHREAD[fd], "call socket.lock first")
|
||||
|
||||
skynet.send(".socket", "client", fd, msg, sz)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
main = "127.0.0.1:6379"
|
||||
main = { host = "127.0.0.1" , port = 6379 }
|
||||
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
local skynet = require "skynet"
|
||||
local socket = require "socket"
|
||||
|
||||
skynet.start(function()
|
||||
local function console_main_loop()
|
||||
local stdin = socket.stdin()
|
||||
socket.lock(stdin)
|
||||
while true do
|
||||
local cmdline = socket.readline(stdin, "\n")
|
||||
local handle = skynet.newservice(cmdline)
|
||||
if handle == nil then
|
||||
print("Launch error:",cmdline)
|
||||
if cmdline ~= "" then
|
||||
local handle = skynet.newservice(cmdline)
|
||||
if handle == nil then
|
||||
print("Launch error:",cmdline)
|
||||
end
|
||||
end
|
||||
end
|
||||
socket.unlock(stdin)
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.fork(console_main_loop)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user