构建“消息读取”的解决方案Redis 中的类型队列系统?

发布于 2024-12-09 01:37:06 字数 291 浏览 0 评论 0原文

我不完全确定 Redis 是解决此类问题的最佳工具,但我认为这是可能的。很多时候,您想向所有用户发送一条消息。几乎就像一个系统范围的新闻条目。您要确保用户不会多次看到此消息,他们应该只在登录时看到一次,之后在发布新条目之前他们永远不会看到该新闻部分。

我认为在 MySQL 中保留一个表,为用户每次读取消息时设置一个布尔列,这是一种低效的方法。您无法真正归档这样的表,因为如果这样做,您将不知道用户已阅读哪些消息以及未阅读哪些消息。

如果这可以使用 Redis pub/sub 完全在内存中完成,那就太好了。消息读取类型系统有哪些策略?

I'm not entirely sure Redis is the best tool for something like this, however I think it is possible. Many times there is a message you want to send to all of your users. Almost like a system wide news entry. You want to make sure that the user does not see this message more than once, they should only see it once on login, and after that they should never see that news section until a new entry has been published.

I think keep a table in MySQL that sets a boolean column for every time a user has read a message as an inefficient way of doing this. You can't really archive a table like that, because if you do, you won't know what messages a user has read and which they haven't.

It would be great if this can be done completely in memory using Redis pub/sub. What are some strategies for a messages read type system?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-12-16 01:37:07

当然,您可以利用 redis 的 pub/sub 功能,也可以使用 comet.io 来实现 - 服务器将消息推送到客户端。如果这就是您想要做的,我可以详细说明一下,但我不完全确定系统是否必须对于您的用例来说那么复杂。使用 pubsub/comet 从前端到后端实现这一切并不是一件容易的任务,尽管可行并且有时需要/问题的正确解决方案。

一个可能也适合您的更简单的方法怎么样:

创建一个带有 creation_date 日期时间字段的 news 表。将 last_seen_news_date 字段添加到您的 user 表中,为所有用户将其设置为过去的某个日期。

当用户加载页面时,按如下方式查询新闻项:

select * from news where creation_date > last_seen_news_date_of_user

然后使用当前日期时间或最新新闻文章的日期时间更新 last_seen_news_date

Of course you could leverage redis pub/sub capabilities and maybe use socket.io for comet – the server pushes messages down to the client. If thats what you want to do I can elaborate a bit more about it, but I´m not entirely sure if the system has to be that complicated for your usecase. Implementig it all from frontend to backend with pubsub/comet is not an easy task, although doable and sometimes required/the right solution for the problem.

How about a simpler approach that might work out for you, too:

Create a news table with a creation_date datetime field. Add a last_seen_news_date field to your user table, set it for all users to some date in the past.

When a user loads the page, query the news items like this:

select * from news where creation_date > last_seen_news_date_of_user

Then update the last_seen_news_date with the current datetime or maybe the datetime of the most recent news article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文