Skip to content

Instantly share code, notes, and snippets.

@bcoe
Created April 18, 2014 07:25
Show Gist options
  • Save bcoe/11029265 to your computer and use it in GitHub Desktop.
Save bcoe/11029265 to your computer and use it in GitHub Desktop.
Atomic update of JSON in Redis
# atomic update of a key using LUA.
EVAL "local t1 = cjson.decode(redis.call('get', ARGV[1])); local t2 = cjson.decode(ARGV[2]); for k,v in pairs(t2) do t1[k] = v end; redis.call('set', ARGV[1], cjson.encode(t1))" 0 "foo" '{"bar": 3}'
local id = ARGV[1]
local t1 = cjson.decode(redis.call('get', id)
local t2 = cjson.decode(ARGV[2])
# merge the new hash with the prior hash.
for k,v in pairs(t2) do t1[k] = v end
redis.call('set', id, cjson.encode(t1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment