PHP 应用程序中的未读消息计数
我目前正在开发一个简单的 PHP 应用程序,用户可以在其中互相发送消息。消息存储在 SQL 数据库中。我想在每个页面的菜单中添加未读消息的计数,以便用户可以快速查看是否有新消息,而无需定期检查收件箱。
虽然这可能是一个容易解决的问题,但我不知道在性能方面最好的方法是什么:
- 在每个页面加载时对未读消息执行简单的 SQL COUNT()(立即通知)的变化,但它可能会严重影响性能?)
- 做同样的事情,但将结果缓存 X 分钟(我们创建了一个烦人的延迟)
- 与 2. 相同,但仅在我们时更新阅读消息或向我们发送消息时(可能会消耗大量 RAM/给磁盘带来压力,因为我们为每个用户创建一个持久条目/文件:我们无法将其存储在 $_SESSION 中,因为我们当另一个用户向我们发送消息时需要更新它)
我所有的解决方案都有点基于服务器,因为我对 JS 不太熟悉。但如果使用 JavaScript 存在更好的解决方案,那就没问题了。
感谢您的帮助 !
I am currently developing a simple PHP application where users can send messages to each other. Messages are stored in a SQL database. I'd like to put a count of unread messages in the menu on every page, so that a user can see quickly if they have new messages without checking the inbox periodically.
While this may be an easy problem to solve, I don't know what the best method would be, performance-wise :
- Do a plain SQL COUNT() of unread messages on every page load (instantly notified of changes, but it may impact performance badly ?)
- Do the same thing, but cache the result for X minutes (we create an annoying delay)
- Same as 2., but only update when we read a message or when a message is sent to us (can use up a lot of RAM / stress the disk, since we create one persistent entry/file per user : we can't store it in $_SESSION because we need to update it when another user sends a message to us)
All my solutions are somewhat server-based, because I'm not very familiar with JS. But if a better solution exists using JavaScript, It's okay.
Thank you for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议第四种:
将新消息发送给用户后,您可以更新
memcache
中的计数器。您在客户端创建简单的 ajax 应用程序,每 X 秒发送一次请求。在服务器端,您只需检查是否有未读消息。在页面刷新时,您不需要查询数据库,因为您可以非常快地从memcache
获取计数。如果我在数据库中遇到瓶颈(在 90% 的情况下,数据库是任何数据库驱动的应用程序中最薄弱的部分),这就是我所做的。
这就是我们通常在高负载网站上所做的事情:我们试图避免任何 COUNTER 查询。如果没有,我们对数据库进行非规范化,将计数器作为另一列存储在适当的表中,例如,如果您不能使用memcache,则可以将未读消息计数器存储为Users
的列/代码>表。
I'd suggest 4'th:
Once new message has been sent to a user, you update counter in
memcache
. You create simple ajax application on client side sending a request every X seconds. At server side, you just check is there unread messages. At page refresh, you don't need to query the database since you get count frommemcache
extremely fast.That's what I'd done if I had bottleneck in DB (in 90% cases, DB is the weekest part of any database-driven application).
That's what we usually do at highloaded web sites: we trying to avoid any COUNTER queries. If not, we denormalize the database to store counters right in the appropriate table as yet another column e.g. if you can not use
memcache
, you would store the unread messages counter as a column forUsers
table.我会选择第三个选项,除了添加 memcached如解决方案4。
I'd go for option three, except I'd add memcached as solution 4.
只要你有一个合适的表结构,COUNT() 就是一个相当快的命令。我不会缓存这个特定的命令。相反,我会计算出其他查询,以确保您在向他们显示列表时只返回所需的数据。例如,如果您需要的只是摘录,我会确保执行以下操作:
而不是
As long as you have a decent table structure, COUNT() is a pretty fast command. I wouldn't cache this particular command. I'd instead work out the other queries to make sure you're only returning the data you need when showing them a listing. For example, if all you need is an excerpt, I'd make sure to do something like this:
instead of
恕我直言。最好让客户端 ping 服务器并发送回包含未读消息数量的 json。在 mysql 中计数应该很快,所以我认为没有理由不使用它。只需过滤聊天会话中的结果即可。
对于数据库部分。最好的方法是在数据库表中存储填充的 new_message 并将其默认为 1,并在消息加载后将其设置为 0。
Imho. It's best to let the client ping the server and send a json back with the amount of unread messages. Counting in mysql should be fast so I see no reason not to use it. Just filter the results on the chat session.
For the database part. The best way would be to store a new_message filled in your db table and default it to 1, and set that one to 0 when the message has been loaded.