gpt4 book ai didi

node.js - Redis/Node等待key存在

转载 作者:IT王子 更新时间:2023-10-29 06:11:09 25 4
gpt4 key购买 nike

是否可以等待一个键存在于Redis中?让引擎在后台运行以处理繁重的工作。这个想法是让 rest api 向 Redis 发送请求,然后引擎从该列表中读取。然后,其余 api 将监视一个特定的 key ,该 key 最终将包含要发送给客户端的响应。

这可能吗? - 当我进行自己的研究时,我一直在使用 BLPOP,但这是针对列表而不是 Redis 中的特定键。

有没有更好的方法来做到这一点?

最佳答案

您可以使用 Redis Keyspace Notification实现目标的功能。

有了这个特性,当有人创建/设置给定键时,Redis 会向特定 channel 发布消息。为了等待给定的 key 存在,你只需要订阅这个特定的 channel ,并等待通知。

// Enable keyspace notification:
config set notify-keyspace-events g$K
// subscribe the specific channel: __keyspace@<db_num>__:<key_name>
subscribe '__keyspace@0__:key'

当有人设置 key 时,您会收到以下通知:

1) "message"
2) "__keyspace@0__:key"
3) "set"

关于node.js - Redis/Node等待key存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40431374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com