Unread message count in a PHP app

ε祈祈猫儿з 提交于 2020-01-13 06:12:24

问题


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 :

  1. Do a plain SQL COUNT() of unread messages on every page load (instantly notified of changes, but it may impact performance badly ?)
  2. Do the same thing, but cache the result for X minutes (we create an annoying delay)
  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)

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 !


回答1:


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 from memcache 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 for Users table.




回答2:


I'd go for option three, except I'd add memcached as solution 4.




回答3:


Do a plain SQL COUNT() of unread messages on every page load (instantly notified of changes, but it may impact performance badly ?)

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:

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

instead of

SELECT * from table where recipient = ?;



回答4:


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.



来源:https://stackoverflow.com/questions/5856257/unread-message-count-in-a-php-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!