使用 memcached 作为聊天消息的数据库缓冲区
我正在尝试使用 PHP 和 CodeIgniter 构建一个聊天应用程序。
为此,我使用 memcached 实现缓存“缓冲区”,以将最新的聊天消息保存在内存中,从而减少数据库的负载。我想要做的是:
- 当消息到达时,我使用当前分钟(YYYY-MM-DD-HH-MM)作为键将其保存在 memcached 中。不涉及数据库 I/O。这个想法是同一分钟的所有消息都在同一密钥下收集。
- 用户收到的新聊天消息也是从 memcached 获取的(目前我使用的是长轮询,但出于明显的性能原因,这将转移到 Node.js 下的 WebSockets)。同样,不涉及数据库 I/O。
- 自动服务器脚本 (cronjob) 将每 5 分钟运行一次,收集过去 5 分钟的 memcached 数据并将消息插入数据库。
- memcached 对象设置为在 6 分钟后失效,因此我们永远不需要在内存中保留超过 6 分钟的消息数据,
这样每 5 分钟总共有一次数据库写入操作,零数据库读取操作。
这听起来可行吗?有没有更好的(甚至是内置的?)方法来使用 memcached 来实现此目的?
更新:我现在已经进行了一些尝试,并且我有了一个快捷方式的想法(阅读:hack)。我可以在 Node.js 服务器脚本中临时“缓冲”消息,直到准备好存储它们为止。 Node.js 服务器中的 Javascript 对象/消息数组基本上是一种内存缓存。
所以:每 N 条消息/秒,我可以使用任何我想要的方法将缓冲的消息(JS 数组的内容)传递到我的数据库,因为它不会经常被调用。
然而,我担心这可能会削弱 Node.js 服务器进程,因为它可能不喜欢携带那个 200 KB 的数组。
对这个策略有什么想法吗?是不是彻底疯了?
I am playing around with building a chat application using PHP and CodeIgniter.
For this, I am implementing a cache 'buffer' with memcached to hold the most recent chat messages in memory, reducing load on the database. What I want to do is this:
- When a message arrives, I save it in memcached using the current minute (YYYY-MM-DD-HH-MM) as the key. No database I/O involved. The idea being that all messages from the same minute are collected under the same key.
- Users receive new chat messages also fetched from memcached (for now I'm using long-polling, but this will move to WebSockets under Node.js for obvious performance reasons). Again, no database I/O involved.
- An automated server script (cronjob) will run once every 5 minutes, collecting the memcached data from the last 5 minutes and inserting the messages into the database.
- The memcached objects are set to go stale after 6 minutes, so we never need to keep more than 6 minutes worth of message data in memory
This for a total of one database write operation per 5 minutes and zero database read operations.
Does this sound feasible? Is there a better (maybe even built-in?) way to use memcached for this purpose?
Update: I have been experimenting a little now, and I have an idea for a shortcut (read: hack). I can 'buffer' the messages temporarily in the Node.js server script until I'm ready to store them. A Javascript object/array of messages in the Node.js server is basically a memory cache - kind of.
So: Every N messages/seconds, I can pass the buffered messages (the contents of the JS array) to my database, using whatever method I want, since it won't be called very often.
However, I'm worried this might cripple the Node.js server process, since it probably won't enjoy carrying around that 200 KB array.
Any thoughts on this strategy? Is it completely crazy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您研究过 HTML5 套接字连接吗?使用套接字服务器,您不需要存储任何内容。服务器接收来自一个订阅者的消息,并立即将其发送回正确的订阅者。我自己没有使用 HTML5 完成此操作,但我知道该功能现在已经存在。我在使用 Flash 之前已经这样做过,它也支持套接字连接。
Have you looked into HTML5 socket connections? With a socket server, you do not need to store anything. The server receives a message from one subscriber, and immediately sends it back out to the correct subscribers. I have not done this myself using HTML5, but I know the functionality now exists. I have done this before using Flash which also supports socket conenctions.
为什么不使用 INSERT DELAYED ?它为您提供几乎与您想要实现的功能相同的功能,而无需内存缓存。
不管怎样,你的解决方案看起来也不错。
Why don't use
INSERT DELAYED
? It offers you almost the same functionality you are trying to achieve without the need of memcached.Anyway your solution looks good, too.