Skip to content

Instantly share code, notes, and snippets.

@fritzy
Created November 13, 2013 23:12
Show Gist options
  • Save fritzy/7458225 to your computer and use it in GitHub Desktop.
Save fritzy/7458225 to your computer and use it in GitHub Desktop.
Update a key and lookup for a specific root level JSON attribute.
--EVAL "this script" 2 key lookup jsonvalue attribute
local key, lookup = unpack(KEYS);
local jsonvalue, attribute = unpack(ARGV);
local value = cjson.decode(jsonvalue);
local oldvalue = redis.call('GET', key);
--if this key was set before, remove the old lookup
if type(oldvalue) == 'string' then
oldvalue = cjson.decode(oldvalue);
redis.call('HDEL', lookup, oldvalue[attribute]);
end
redis.call('HSET', lookup, value[attribute], key);
return redis.call('SET', key, jsonvalue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment