Skip to content

Instantly share code, notes, and snippets.

@wankdanker
Last active September 26, 2022 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wankdanker/8db0398c9bc4e6a9753a17922f5952ce to your computer and use it in GitHub Desktop.
Save wankdanker/8db0398c9bc4e6a9753a17922f5952ce to your computer and use it in GitHub Desktop.
happend LUA script for redis
const Redis = require('ioredis')
const redis = new Redis()
redis.defineCommand("happend", {
numberOfKeys: 2,
lua: `
local current = redis.call("hget", KEYS[1], KEYS[2])
if current == nil or current == false then
current = ARGV[1]
else
current = current .. ARGV[1]
end
redis.call("hset", KEYS[1], KEYS[2], current)
return #current
`,
});
(async () => {
let newLength = await redis.happend('key', 'sub-key', 'value')
})()
local current = redis.call("hget", KEYS[1], KEYS[2])
if current == nil or current == false then
current = ARGV[1]
else
current = current .. ARGV[1]
end
redis.call("hset", KEYS[1], KEYS[2], current)
return #current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment