简体   繁体   中英

Redis: Wrong data in wrong tables

I am trying to solve a problem that has been blocking me for a month

I am bulding the backend of an application using and due to our structure we have to (What I mean by is the one's that we use "select" ie "select 2") 扩展应用程序的后端,由于我们的结构,我们必须 (我的意思是是我们使用“ select”即“ select 2”的表)。 )

We receive a lot of request and push a lot of response in a sec, and no matter how much I tried . Assume we have a that has to be stored inside . ,必须将其存储在And a that has to be stored in . ,必须存储在How matter what I tried (I've checked my code multiple times) I could not stop . The last trick I've tried was actually ;

redisClient.select(4, function(err) {
  if(err)
    console.log("You could not select the table. Function will be aborted");
  else {
    // Proceed with the logic
  }
});

What could be the reason that I cannot simply stop this mess ? One detail that drivers me crazy is that it works really well on local and also online however whenever multiple request reach to server it gets mixed. Any suggestions to prevent this error? (Even though I cannot share the code to NDA I can make sure that logic has been coded correctly)

I'm not sure about your statement about having to "transfer data from one redis table to another". Reading through your example it seems like you could simply have two redis clients that write to different databases (what you called "tables").

It would look similar to this:

var redis = require("redis");
var client1 = redis.createClient(6379, '127.0.0.1');
var client2 = redis.createClient(6379, '127.0.0.1');

client1.select(2, function(err){
    console.log('client 1 is using database 2');
});

client2.select(4, function(err){
    console.log('client 2 is using database 4');
});

Then, wherever your read/write logic is, you just use the appropriate client:

client1.set("someKey", "teacherID", function(err){
    // ...
});

client2.set("someKey", "studentID", function(err){
    // ...
});

You can obviously encapsulate the above into functions with callbacks, nest the operations, use some async library, etc. to make it do whatever you need it to do. If you need to transfer values from database 2 to database 4 you could do a simple client1.get() and client2.set() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM