如何实现“触发"对于 redis 数据存储? [英] How to implement "trigger" for redis datastore?
问题描述
我有一个程序,它将轮询 redis 数据存储中的某个键,并在该值满足某个条件时执行某些操作.
I have a program, which will poll on a certain key from the redis datastore, and do something when the value satisfies a certain condition.
但是,我觉得定期轮询redis效率很低,我想知道redis是否有触发"机制,当值发生变化并满足条件时,会调用触发器.触发器可能是一个RPC函数,也可能是一个HTTP msg,或者其他什么,这样我就不再需要轮询它了,就像poll和interrupt的区别一样.
However, I think periodically polling on redis is quite inefficient, I'm wondering if there is a "trigger" mechanism for redis, when the value changes and satisfies the condition, the trigger will be called. The trigger might be a RPC function, or an HTTP msg, or something else, so that I don't need to poll on it any more, just like the difference between poll and interrupt.
这可能吗?
推荐答案
您可以使用 Pub/Sub Redis 的特性.鉴于您所描述的情况,这正是您所需要的.
You can use the Pub/Sub feature of Redis. It's exactly what you need given your circumstances as you described.
本质上,您SUBSCRIBE
到频道",应用程序的另一部分写入(PUBLISH
)更改到该频道的值.您的订阅者(消费者、想要了解更改的客户)将几乎实时收到通知.
Essentially, you SUBSCRIBE
to a "channel", and the other part of your application writes (PUBLISH
) the value being changed to that channel. Your subscriber (consumer, the client that wants to know about the change) will get notified in virtually realtime.
这篇关于如何实现“触发"对于 redis 数据存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!