ngx_lua
使用lua-resty-ssdb
lua_package_path "/path/to/lua-resty-ssdb/lib/?.lua;;";
server {
location /test {
content_by_lua '
local ssdb = require "resty.ssdb"
local db = ssdb:new()
db:set_timeout(1000) -- 1 sec
local ok, err = db:connect("127.0.0.1", 8888)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ok, err = db:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", ok)
local res, err = db:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)
db:init_pipeline()
db:set("cat", "Marry")
db:set("horse", "Bob")
db:get("cat")
db:get("horse")
local results, err = db:commit_pipeline()
if not results then
ngx.say("failed to commit the pipelined requests: ", err)
return
end
for i, res in ipairs(results) do
if type(res) == "table" then
if not res[1] then
ngx.say("failed to run command ", i, ": ", res[2])
else
-- process the table value
end
else
-- process the scalar value
end
end
-- put it into the connection pool of size 100,
-- with 0 idle timeout
local ok, err = db:set_keepalive(0, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
-- or just close the connection right away:
-- local ok, err = db:close()
-- if not ok then
-- ngx.say("failed to close: ", err)
-- return
-- end
';
}
}
C++
#include
#include
#include
#include
#include "SSDB.h"
int main(int argc, char **argv){
const char *ip = (argc >= 2)? argv[1] : "127.0.0.1";
int port = (argc >= 3)? atoi(argv[2]) : 8888;
ssdb::Client *client = ssdb::Client::connect(ip, port);
if(client == NULL){
printf("fail to connect to server!\n");
return 0;
}
ssdb::Status s;
s = client->set("k", "hello ssdb!");
if(s.ok()){
printf("k = hello ssdb!\n");
}else{
printf("error!\n");
}
delete client;
return 0;
}