简体   繁体   中英

How do I access an object value from a cached JSON API in Redis cache?

In my Rails app, I have gotten to the stage where I stored the nested API JSON objects as the value of my Redis key eg. "A". The whole object is accessed by calling a GET of "A". However, what would I need to do if I want to change of the value of a certain name of the object? Since I stored the whole JSON object as Redis value, would I need to create a new model and replicate the same values within the model and alter through a for instance Postgres? Or is there a way to target a specific value in the JSON object that is stored as value of key "A" in Redis? If so, how would I create the routes to allow a front end developer to update the cache outside of the rails app?

Redis does not natively support modifying JSON documents within its values; there are some experimental plugins for doing so, but most likely, you will need to deserialize the current value ( doc = redis.get('A') ), modify it ( doc['name'] = 'foo' ), and then reserialize and store it back into Redis ( redis.set('A', JSON.encode(doc)) ).

However, if you're interested in working with JSON documents like this at scale, or perhaps querying on your JSON documents' values within your database, I'd strongly suggest you use either a NoSQL JSON document store (eg MongoDB), or take advantage of Postgres' new-ish native JSON support.

Also, your comments about "allowing a front-end dev to update the cache outside of the Rails stack" are a little worrying; if you're exposing write access to your database (be it Redis, Mongo, or Postgres) directly to the front-end, you're potentially exposing yourself to a host of security issues. If you want that ability, and you want to modify JSON documents like you propose, you might find yourself best off by using Firebase -- it's designed for direct client-side access, uses JSON natively, and has a lot of support and tooling available.

Redis does not natively support modifying JSON documents within its values;

said correctly @Robert Nubel, but you do have two viable alternatives for achieving that:

  1. Server-side Lua scripts - Redis' Lua sandbox is shipped with the cjson library. You can use it to extract/update any element from an embedded serialized JSON string, without the network penality. You can find a bunch of scripts that do that at https://github.com/RedisLabsModules/rejson/tree/master/benchmarks/lua

  2. Redis Modules - the above-referred scripts are a (non-essential) part of a newly-developed Redis module that provides a native JSON data type for Redis. The module is still in preview mode, but I believe that it will be made GA and added with features in the near future (disclosure - author here ;)). You can find ReJSON, the JSON data type for Redis at https://github.com/redislabsmodules/rejson and be sure to check the comparative results vs. the Lua approach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM