Skip to content

Instantly share code, notes, and snippets.

@amichaelgrant
Created March 14, 2016 22:56
Show Gist options
  • Save amichaelgrant/44c8faa2e46055c10aaa to your computer and use it in GitHub Desktop.
Save amichaelgrant/44c8faa2e46055c10aaa to your computer and use it in GitHub Desktop.
Running Lua Script in Redis from Nodejs Client
-- setnex.lua
local key = ARGV[1]
local expiry = ARGV[2]
local value = ARGV[3]
local reply = redis.call("SETNX", key, value)
if 1 == reply then
redis.call("EXPIRE", key, expiry)
end
return reply
You can call it from node_redis like this:
client.eval(setnex_lua, 0, "mykey", 10, "myvalue", function (err, res) {
console.dir(err);
console.dir(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment