简体   繁体   English

PHP 应用程序中的未读消息计数

[英]Unread message count in a PHP app

I am currently developing a simple PHP application where users can send messages to each other.我目前正在开发一个简单的 PHP 应用程序,用户可以在其中相互发送消息。 Messages are stored in a SQL database.消息存储在 SQL 数据库中。 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:虽然这可能是一个容易解决的问题,但我不知道最好的方法是什么,性能方面:

  1. Do a plain SQL COUNT() of unread messages on every page load (instantly notified of changes, but it may impact performance badly?)在每次页面加载时执行一个普通的 SQL COUNT() 未读消息(立即通知更改,但它可能会严重影响性能?)
  2. Do the same thing, but cache the result for X minutes (we create an annoying delay)做同样的事情,但将结果缓存 X 分钟(我们会造成烦人的延迟)
  3. 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)与 2. 相同,但仅在我们读取消息或向我们发送消息时更新(可能会占用大量 RAM / 给磁盘带来压力,因为我们为每个用户创建一个持久条目/文件:我们无法存储它在 $_SESSION 因为我们需要在另一个用户向我们发送消息时更新它)

All my solutions are somewhat server-based, because I'm not very familiar with JS.我所有的解决方案都是基于服务器的,因为我对 JS 不是很熟悉。 But if a better solution exists using JavaScript, It's okay.但是如果使用 JavaScript 存在更好的解决方案,那没关系。

Thank you for your help !谢谢您的帮助 !

I'd suggest 4'th:我建议第4个:

Once new message has been sent to a user, you update counter in memcache .将新消息发送给用户后,您将更新memcache中的计数器。 You create simple ajax application on client side sending a request every X seconds.您在客户端创建简单的 ajax 应用程序,每 X 秒发送一次请求。 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 from memcache extremely fast.在页面刷新时,您不需要查询数据库,因为您从memcache中获取计数非常快。

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).如果我在 DB 中遇到瓶颈,这就是我所做的(在 90% 的情况下,DB 是任何数据库驱动应用程序中最常用的部分)。

That's what we usually do at highloaded web sites: we trying to avoid any COUNTER queries.这就是我们通常在高负载的 web 站点上所做的事情:我们试图避免任何 COUNTER 查询。 If not, we denormalize the database to store counters right in the appropriate table as yet another column eg if you can not use memcache , you would store the unread messages counter as a column for Users table.如果不是,我们将数据库非规范化以将计数器存储在适当的表中作为另一列,例如,如果您不能使用memcache ,则将未读消息计数器存储为Users表的列。

I'd go for option three, except I'd add memcached as solution 4.对于选项三,我会选择 go,但我会添加memcached作为解决方案 4。

Do a plain SQL COUNT() of unread messages on every page load (instantly notified of changes, but it may impact performance badly?)在每次页面加载时执行一个普通的 SQL COUNT() 未读消息(立即通知更改,但它可能会严重影响性能?)

As long as you have a decent table structure, COUNT() is a pretty fast command.只要你有一个像样的表结构, COUNT() 是一个非常快的命令。 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:例如,如果您只需要摘录,我会确保执行以下操作:

SELECT id, author, msgdate, substring(body, 0, 50) from table where recipient = ?

instead of代替

SELECT * from table where recipient = ?;

Imho.恕我直言。 It's best to let the client ping the server and send a json back with the amount of unread messages.最好让客户端 ping 服务器并发送一个 json 与未读消息的数量。 Counting in mysql should be fast so I see no reason not to use it. mysql 的计数应该很快,所以我认为没有理由不使用它。 Just filter the results on the chat session.只需过滤聊天 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.最好的方法是在你的 db 表中存储一个 new_message 并将其默认为 1,并在消息加载后将其设置为 0。

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

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