1

希望有人可以对此有所了解。我一辈子都找不到问题所在。

问题:

我正在尝试使用 hset 在一个文件中向 Redis 添加一个值,并使用hget另一个文件中获取该值。在测试用例中最终发生的是值已设置但hget返回 null ,就好像那里什么都没有。

试图:

当我在同一个文件中设置后立即尝试获取该值时,会发生真正奇怪的部分。它实际上正常工作!看一下这个...

// FYI: This is co-mocha so the generator function is okay
describe('Route', () => {
  it('should return the value currently set in Redis', function* () {
    const link = 'this-is-a-link';
    yield redis.hset('i:metadata', 'stream', link); // using ioredis
    const response = yield request.send(); // requests to my Koa server
    expect(response.body.stream).to.equal(link); // expected null to equal 'this-is-a-link'
  });
});

你可以看到路线很简单......

// FYI: This is a Koa route reference with a HTTP GET on '/'
exports.get = function* () {
  const stream = yield redis.hget('i:metadata', 'stream'); // using ioredis
  this.body = { stream }; // holds a value of null
};

如果我在我的测试用例中将那个 hget 插入到我的hset下方,那么这个值就会被完美地检索到。有没有人对如何解决这个问题有任何想法?

笔记:

在任何人建议之前,我已经监听了连接的事件,这并不重要,因为我可以直接在hset下方获取值并且它可以工作。这个问题也存在于一个承诺的node_redis安装中。

谢谢!

4

0 回答 0