PHP应用中的未读消息计数 [英] Unread message count in a PHP app

查看:553
本文介绍了PHP应用中的未读消息计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个简单的PHP应用程序,用户可以相互发送消息。消息存储在SQL数据库中。我想在每个页面的菜单中放置一些未读消息,以便用户可以快速查看是否有新消息而无需定期检查收件箱。

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. 在每个页面上加载的未读消息的简单SQL COUNT()(立即通知更改,但可能会严重影响性能?)

  2. 执行同样的事情,但是将结果缓存X分钟(我们创建一个令人讨厌的延迟)

  3. 与2.相同,但仅在我们阅读邮件或更新时更新消息发送给我们(可能会占用大量RAM /压力磁盘,因为我们为每个用户创建一个持久性条目/文件:我们无法将其存储在$ _SESSION中,因为我们需要在更新时更新它另一个用户向我们发送消息)

  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)

我所有的解决方案都是基于服务器的,因为我不是很熟悉JS。但如果使用JavaScript存在更好的解决方案,那没关系。

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.

感谢您的帮助!

推荐答案

我建议4日:

一旦新消息发送给用户,你就会在内存缓存。您可以在客户端创建简单的ajax应用程序,每隔X秒发送一次请求。在服务器端,您只需检查是否有未读消息。在页面刷新时,您不需要查询数据库,因为您从 memcache 计算得非常快。

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.

这就是我在DB中遇到瓶颈时所做的事情(在90%的情况下,DB是任何数据库驱动的应用程序的周末部分)。

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).

这就是我们通常的情况在高负载的网站上做:我们试图避免任何COUNTER查询。如果没有,我们将数据库非规范化以将计数器存储在适当的表中,作为另一列,例如如果您不能使用 memcache ,则会将未读消息计数器存储为 Users 表的列。

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.

这篇关于PHP应用中的未读消息计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆