与PHP和jQuery进行实时聊天.在哪里存储信息? mysql还是文件? [英] Live chat with PHP and jQuery. Where to store information? Mysql or file?

查看:101
本文介绍了与PHP和jQuery进行实时聊天.在哪里存储信息? mysql还是文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有1对1实时聊天.两种解决方案:

There are 1 on 1 live chat. Two solutions:

1)我将每条消息存储到数据库中,并在jQuery的帮助下每秒检查数据库中是否有新消息.当然我也可以使用缓存.如果有,我们给出该消息.

1) I store every message into database and with jQuery's help I check if there is a new message in database every second. Of course I use cache either. If there is, we give that message.

2)我将每条消息存储在一个html文件中,并通过jQuery将消息每秒钟重复显示一次.

2) I store every message in one html file and every second through jQuery that file is shown over and over again.

有什么更好的?还是有第三种选择?通常,对于这种项目,mysql还是文件更好?

What is better? Or there is third option? And in general, what is better, mysql or file for this kinda project?

非常感谢您.

P.S.最重要的问题是:哪种方法更有效,哪种方法将消耗更少的资源!

现在,对于许多聊天(例如2,500个聊天,这意味着5,000个用户)来说,使用长时间轮询并检查何时每隔几秒钟通过javascript编辑文件是一件很糟糕的事情吗?我使用类似聊天的方法: http://css-tricks.com/jquery-php-chat/它会杀死我的主机吗?

And is it, nowadays, very bad for many chats (let's say 2,500 chats, that means 5,000 users) to use long polling and check when file was edited every second through javascript? I use very similiar methods like this chat: http://css-tricks.com/jquery-php-chat/ Will it kill my hosting?

推荐答案

每个人都给出了广泛的意见,但我认为没有人能真正做到这一点.

Everyone has given a wide range of opinions but I don't think anyone has really hit the nail on the head.

当涉及到存储数据时,数据量,要访问的速率以及其他几个因素都决定了什么是最好的存储平台.

When it comes down to storing data, the amount of data, the rate it is to be accessed, and several other factors all determine what's the best storage platform.

有人建议使用memcached.现在,尽管这是一个有效的答案(您可以使用它),但仅基于内存缓存将数据存储在服务器内存中这一事实,我认为这不是一个好主意.

Some people have suggested using memcached. Now although this is a valid answer (you can use it), I don't think that this is a good idea, solely based on the fact that memcached stores data within your server's memory.

您的内存不是用于数据存储的,而是用于实际的应用程序,操作系统,共享库等的.

Your memory is not for data storage, it's for use of the actual applications, operating system, shared libraries, etc.

将数据存储在内存中会导致当前正在运行的其他应用程序出现很多问题.如果您在RAM中存储了太多数据,则应用程序将无法完成分配给它们的操作.

Storing data within the memory can cause a lot of issues with other applications currently running. If you store too much data in your RAM your applications would not be able to complete operations assigned to them.

尽管这比基于磁盘的存储平台(例如MySQL)要快,但它并不那么可靠.

Although this is faster then a disk based storage platform such as MySQL, it's not as reliable.

我个人将MySQL用作您的存储引擎服务器端.这样可以减少您遇到的问题,并使数据易于管理.

I would personally use MySQL as your storage engine server-side. This would reduce the amount of problems you would come across and also makes the data very manageable.

为了加快对客户的响应,我将考虑在服务器上运行 node .

To speed up the responses to your clients I would look at running node on your server.

这是因为它是事件驱动且无阻塞的.

This is because it's event driven and non-blocking.

那是什么意思?

好吧,当客户端A请求一些存储在硬盘驱动器上的数据时,传统上PHP可能会对C ++说,向我获取存储在硬盘驱动器此扇区上的该数据块. C ++会说没问题",尽管它会继续获取PHP的信息,然后等待数据被读取并返回,然后再继续操作,但阻止同时.

Well, when Client A requests some data that is stored on the hard drive, traditionally PHP might say to the C++, fetch me this chunk of data stored on this sector of the hard drive. C++ would say 'ok no problem', and while it goes of to get the information PHP would sit and wait for the data to be read and returned before it continues it's operations, blocking all other client's in the meantime.

与节点不同,它略有不同. Node会对内核说,向我获取这些信息,完成后给我打电话",然后它继续接收来自其他可能不需要磁盘访问权限的客户端的请求.

With node, it's slightly different. Node will say to the kernel, 'fetch me this chunk of information and when your done, give me call', and then it continues to take requests from other clients that may not need disk access.

突然之间,因为我们已经为内核分配了回调,所以不必等待:),快乐的日子.

So suddenly because we have assigned a callback to the kernel, we do not have to wait :), happy days.

看一下这张图片:

这确实可能是您寻找的答案,请参见以下内容,以获取有关如何使节点成为您的正确选择的更多描述性和详细信息:

This really could be the answer your looking for, please see the following for a more descriptive and detailed information regarding how node could be the right choice for you:

这篇关于与PHP和jQuery进行实时聊天.在哪里存储信息? mysql还是文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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