Redis客户端不会在收到消息后设置值

时间:2018-09-29 18:55:55

标签: redis node-redis

初始值集发生在服务器(\ complex \ server \ index.js):

app.post('/values', async (req, res) => {
  const index = req.body.index;

  if (parseInt(index) > 40) {
    return res.status(422).send('Index too high');
  }

  redisClient.hset('values', index, 'Nothing yet!');
  redisPublisher.publish('insert', index);

  pgClient.query('INSERT INTO values(number) VALUES($1)', [index]);
  res.send({ working: true });
});

在组件(\ complex \ client \ src \ Fib.js)中提交值时

handleSubmit = async (event) => {
    event.preventDefault();

    await axios.post('/api/values', {
      index: this.state.index
    });

    this.setState({ index: '' });
  };

worker为Redis客户端设置值:

sub.on('message', (channel, message) => {
  redisClient.hset('values', message, fib(parseInt(message)));
});
sub.subscribe('insert');

但是,当为每个提交的索引列出Fib.js组件内的所有值时,该组件会收到“什么都没有!”。

为什么它不接收计算值? 完整的仓库位于https://github.com/ElAnonimo/docker-complex

1 个答案:

答案 0 :(得分:1)

public override string BossHeadTexture => "Modofmods/NPCS/Boss/BossName_Head_Boss"; 是异步的-它需要连接到Redis,发送消息,等待响应等。
因此,可能发生的是竞争情况,redisClient.hset('values', index, 'Nothing yet!');redisPublisher.publish('insert', index);完成之前就运行了。

我还没有遍历代码,因此您还要确保避免在hset之后调用subscribe()的类似竞争条件。

尝试一下:

publish()