DEV Community

Discussion on: Caching in Node.js using Redis

Collapse
 
samwizzy profile image
Sammie Knob • Edited

How do you make the id unique for each key in the redis key pair structure, i.e lets say you have a collection called "Post", and another "Comment" and they both have a document with same id, is there a way to map this id to their Model like so:
post-5
comment-5
...to avoid conflict with the ids within the redis in-memory database

Collapse
 
franciscomendes10866 profile image
Francisco Mendes

There are several ways around this. But I think the simplest would be to write the key like this: post-${id} and then there would be a middleware to fetch the cached post or the comment, this according to the controller.

I hope I understood your question πŸ™

Collapse
 
samwizzy profile image
Sammie Knob

Okay thanks, you did get my question right.

This actually means that there will be a different middleware to handle this, right?

Thread Thread
 
franciscomendes10866 profile image
Francisco Mendes

Thank god πŸ™ Yes, that would imply having a middleware for the posts and another one for the comments. I think it would be a quick and simple solution. 😊

Thread Thread
 
samwizzy profile image
Sammie Knob

Great! Thank Francisco, i really appreciate.
Once again, awesome pieceπŸ€πŸΌπŸ™‚

Thread Thread
 
franciscomendes10866 profile image
Francisco Mendes

Thank you very much πŸ™