Redis value update

2019-09-06 23:39发布

问题:

Im currently having a redis data set with key representing ids and values as a json . I need to add a new entity in the json for every userid(keys). Is there any existing opensource tool? what is the way i should proceed to update for 1M keys of data.

回答1:

There are a few possibilities:

  1. Here's some pseudo code for doing this with Redis 2.6 Lua scripting.

    for userid in users:
        EVAL 'local obj = cjson.decode(redis.call("GET", KEY[1])); obj.subobj.newjsonkey = ARGV[1]; redis.call("SET", KEY[1], cjson.encode(obj));' 1 userid "new value!"
    
  2. Short of that, you may need to stop the service and do this with GETs and SETs since you probably don't have a locking mechanism in place. If you can enforce a lock, see http://redis.io/commands/setnx

  3. There are a few tools for updating an rdb. https://github.com/sripathikrishnan/redis-rdb-tools https://github.com/nrk/redis-rdb

Note, this answer was adapted to my answer to: Working with nested objects in Redis?



标签: redis