2

I am trying to make rest web service in nodejs and using redis for caching purpose to improve performance. I want to save as a object like offer which has id, offer name and vendor.

I do not want to have different key to have for all these. What is the way to do in a single key.

app.get('/insertOffers', function(req, res) {
    const {id, offerName, offerVendor} = req.query;
    client.set('offers', ) // client is my redis client
});
2
  • If you really want it to be all in a single key, consider using hashes, and do HGET/HSET) instead of GET/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 like client.set(`offers:{id}`, JSON.stringify({name: offerName, vendor: offerVendor})) should be the way to go. Commented May 16, 2017 at 17:50
  • @drdaeman I have saved as you suggested 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 pass client.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 Commented May 16, 2017 at 18:15

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can't we do like client.set('offer:{id}', JSON.stringify({name:offerName, vendor:offerVendor})); and client.get('offer:{id}', function(err, reply) { console.log(reply); })
@Williams Yes of course, this would be the normal approach. However in your original question, you state "I do not want to have different key to have for all these." A hash gives you 1 key per to store all the offers. Effectively they are the same, since you need the 'offer' prefix and a key.
I have this let offer = { id: req.query.id, offerName: req.query.offerName, offerVendor: req.query.offerVendor } client.hset('offer', offer.id, JSON.stringify(offer)); and getting this node_redis: Deprecated: The HSET command contains a "undefined" argument. This is converted to a "undefined" string now and will return an error from v.3.0 on. Please handle this in your code to make sure everything works as you intended it to.
you can add redis.print as the 4th argument, like on the hset example github page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.