redis api back compatible

This commit is contained in:
云风
2013-06-21 16:09:52 +08:00
parent 277b6957cb
commit d8204357a8
4 changed files with 29 additions and 10 deletions

View File

@@ -19,8 +19,15 @@ local meta = {
} }
function redis.connect(dbname) function redis.connect(dbname)
local fd = socket.open(name[dbname]) local db_conf = name[dbname]
return setmetatable( { __handle = fd, __mode = false }, meta ) 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 end
function command:disconnect() function command:disconnect()
@@ -108,7 +115,8 @@ setmetatable(command, { __index = function(t,k)
socket.write(fd, compose_message { cmd, ... }) socket.write(fd, compose_message { cmd, ... })
local ok, ret = read_response(fd) local ok, ret = read_response(fd)
socket.unlock(fd) socket.unlock(fd)
return ok, ret assert(ok, ret)
return ret
end end
end end
t[k] = f t[k] = f
@@ -127,6 +135,13 @@ function command:exists(key)
return exists return exists
end 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) function command:batch(mode)
if mode == "end" then if mode == "end" then
local fd = self.__handle local fd = self.__handle

View File

@@ -157,8 +157,6 @@ function socket.readline(fd, sep)
end end
function socket.write(fd, msg, sz) function socket.write(fd, msg, sz)
assert(coroutine.running() == READTHREAD[fd], "call socket.lock first")
skynet.send(".socket", "client", fd, msg, sz) skynet.send(".socket", "client", fd, msg, sz)
end end

View File

@@ -1,2 +1,2 @@
main = "127.0.0.1:6379" main = { host = "127.0.0.1" , port = 6379 }

View File

@@ -1,15 +1,21 @@
local skynet = require "skynet" local skynet = require "skynet"
local socket = require "socket" local socket = require "socket"
skynet.start(function() local function console_main_loop()
local stdin = socket.stdin() local stdin = socket.stdin()
socket.lock(stdin) socket.lock(stdin)
while true do while true do
local cmdline = socket.readline(stdin, "\n") local cmdline = socket.readline(stdin, "\n")
local handle = skynet.newservice(cmdline) if cmdline ~= "" then
if handle == nil then local handle = skynet.newservice(cmdline)
print("Launch error:",cmdline) if handle == nil then
print("Launch error:",cmdline)
end
end end
end end
socket.unlock(stdin) socket.unlock(stdin)
end
skynet.start(function()
skynet.fork(console_main_loop)
end) end)