Tuesday, 3 October 2017

[question] How would you cache mongoose js objects in Redis ?

Let's say we have the following schema: let userSchema =new Schema({ id:{type:String,unique:true}, name:String }) and the schema have some methods:userSchema.methods.sayHi=function(){ console.log('hello world'); } one way to cache the object is simply to stringfiy it like Redis.set(user.id,JSON.stringfiy(user)); and then retrieve it and populate it as: let user=new User(JSON.parse(Redis.get(id))); but i think this is a bad practice since calling save() method will throw an errorAny other way to do this ?

Submitted October 03, 2017 at 02:55PM by 10701220

No comments:

Post a Comment