简体   繁体   English

使用 Redis Pub/Sub 将数据持久化到 RDBMS 中

[英]Using Redis Pub/Sub to persist data into an RDBMS

I was experimenting with using Redis for caching and got a bit confused with persistence.我正在尝试使用 Redis 进行缓存,但对持久性有点困惑。 I would like to create a service that automatically writes into a PostgreSQL database every time the Redis cache is updated.我想创建一个服务,每次更新 Redis 缓存时都会自动写入 PostgreSQL 数据库。

My initial thought was to use the Pub/Sub feature to listen to events and then trigger the appropriate queries that write into my RDBMS.我最初的想法是使用 Pub/Sub 功能来侦听事件,然后触发写入我的 RDBMS 的适当查询。 Though, after looking some more into the Pub/Sub feature it appears that it is probably not be meant to be used this way.但是,在对 Pub/Sub 功能进行了更多研究之后,它似乎不应该以这种方式使用。

It seems that the standard practice is to keep that logic inside of the REST API that queries Redis.似乎标准做法是将该逻辑保留在查询 Redis 的 REST API 中。 I would however like to have a service that automatically does this in the background.但是,我想要一个在后台自动执行此操作的服务。 Is there a simpler way of doing this that I am missing ?有没有更简单的方法来做到这一点,我想念?

Thank you in advance.先感谢您。

I guess the common way is to Save data to MongoDB and Redis manually.我猜常见的方法是手动将数据保存到 MongoDB 和 Redis。 for example when a user post new message:例如,当用户发布新消息时:

newPost = new Post("Hello world!");

// save to db and get model ID
newPost->save();

// save model to Redis
newPost->saveToCache();

This approach will guarantee that all new Models will store in database even when Redis is down.这种方法将保证即使 Redis 宕机,所有新模型也将存储在数据库中。

An other approach is to save data in Redis first, and add a flag to a Redis List , Then(anytime later) read flags from list and save related data to database.另一种方法是先将数据保存在 Redis 中,然后在Redis 列表中添加一个标志,然后(稍后)从列表中读取标志并将相关数据保存到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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