From 7285c50bfcf49e0423943e414b3c33dc35496356 Mon Sep 17 00:00:00 2001 From: holys Date: Mon, 25 Aug 2014 17:06:14 +0800 Subject: [PATCH] add tx support for lua(openresty) --- client/openresty/ledis.lua | 7 ++- client/openresty/tx_test.lua | 96 ++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 client/openresty/tx_test.lua diff --git a/client/openresty/ledis.lua b/client/openresty/ledis.lua index aa2335a..3bb0d02 100644 --- a/client/openresty/ledis.lua +++ b/client/openresty/ledis.lua @@ -137,7 +137,12 @@ local commands = { --[[server]] "ping", "echo", - "select" + "select", + + -- [[transaction]] + "begin", + "commit", + "rollback" } diff --git a/client/openresty/tx_test.lua b/client/openresty/tx_test.lua new file mode 100644 index 0000000..069ddf9 --- /dev/null +++ b/client/openresty/tx_test.lua @@ -0,0 +1,96 @@ +local ledis = require "ledis" +local lds = ledis:new() + +lds:set_timeout(1000) + + + + +-- connect +local ok, err = lds:connect("127.0.0.1", "6380") +if not ok then + ngx.say("failed to connect:", err) + return +end + +lds:del("tx") + +-- transaction + +ok, err = lds:set("tx", "a") +if not ok then + ngx.say("failed to execute set in tx: ", err) + return +end + +ngx.say("SET should be OK <=>", ok) + +res, err = lds:get("tx") +if not res then + ngx.say("failed to execute get in tx: ", err) + return +end + +ngx.say("GET should be a <=>", res) + + + +ok, err = lds:begin() +if not ok then + ngx.say("failed to run begin: ", err) + return +end + +ngx.say("BEGIN should be OK <=>", ok) + +ok, err = lds:set("tx", "b") +if not ok then + ngx.say("failed to execute set in tx: ", err) + return +end + +ngx.say("SET should be OK <=>", ok) + + +res, err = lds:get("tx") +if not res then + ngx.say("failed to execute get in tx: ", err) + return +end + +ngx.say("GET should be b <=>", res) + +ok, err = lds:rollback() +if not ok then + ngx.say("failed to rollback", err) + return +end +ngx.say("ROLLBACK should be OK <=>", ok) + +res, err = lds:get("tx") +if not res then + ngx.say("failed to execute get in tx: ", err) + return +end + +ngx.say("GET should be a <=>", res) + + +lds:begin() +lds:set("tx", "c") +lds:commit() +res, err = lds:get("tx") +if not res then + ngx.say("failed to execute get in tx: ", err) + return +end + +ngx.say("GET should be c <=>", res) + + +local ok, err = lds:close() +if not ok then + ngx.say("failed to close: ", err) + return +end +ngx.say("close success")