PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to update Radis for dynamic values?

 September 05, 2022     caching, node-redis, redis     No comments   

Issue

I am working with some coaching using Redis in Nodejs. here is my code implimentation.

redis.get(key)
if(!key) {
redis.set(key, {"SomeValue": "SomeValue", "SomeAnohterValue":"SomeAnohterValue"}
}
return redis.get(key)

Till here everything works well.

But let's assume a situation where I need to get the value from a function call and set it to Redis and then I keep getting the same value from Redis whenever I want, in this case, I don't need to call the function again and again for getting the value.

But for an instance, the values have been changed or some more values have been added to my actual API call, now I need to call that function again to update the values again inside the Redis corresponding to that same key.

But I don't know how can I do this.

Any help would be appreciated.

Thank you in advanced.


Solution

First thing is that your initial code has a bug. You should use the set if not exist functionality that redis provides natively instead of doing check and set calls

What you are describing is called cache invalidation and is one of the hardest parts in software development

You need to do a 'notify' in some way when the value changes so that the fetchers know that it is time to grab the most up to date value.

One simple way would be to have a dirty boolean variable that is set to true when the value is updated and when fetching you check that variable. If dirty then get from redis and set to false else return the vue from prior



Answered By - Asad Awadia
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing