As @drdaeman says in the comment, using a Redis Hash offers you the ability to query a single key, where the sub-keys or hash keys is a unique key. The command would look something like the following:
HSET offerId offerJSONString
HGET offerId offerJSONString
So in your express function, you could do something like:
app.get('/insertOffers', function(req, res) {
let offer = {
id: req.query.id,
offerName: req.query.offerName,
offerVendor: req.query.offerVendor
}
client.hset('offers', offer.id, JSON.stringify(offer), yourCallbackFunction);
});
Note, you when you get the data, you will need to JSON.parse it.
client.hget('offers', offerId, function(err, offer){
let offer = JSON.parse(offer);
// do something with offer
})
I think the preference for a single key can be valid, like if you want to later delete all the offers with a single command, without a lookup. Redis is really fast though, so there shouldn't be a heavy performance cost to 2 round trips to the redis server.
HGET/HSET) instead ofGET/SET. However, there is a drawback that hash items can't have independent expire times. And normally there's really no harm in having a lot of keys in Redis, so using something likeclient.set(`offers:{id}`, JSON.stringify({name: offerName, vendor: offerVendor}))should be the way to go.client.set('offer:{id}', JSON.stringify({name:offerName, vendor:offerVendor}));it worked but when I am trying to get offer id wise then it does not work. Whatever id I pass it always return same last saved result. not id wise. can you please suggest how should I implement in such a way when I passclient.get('offer:{id}', function(err, reply) { console.log(reply); })then it should offer id wise. I think it is overriding last data in offers not saving id wise also