Unread message count in a PHP app

£可爱£侵袭症+ 提交于 2019-12-04 17:04:12

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.

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

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 = ?;

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.

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