有效地将大约 20kb 的文本重新保存到 mysql
我有一个 contenteditable div,用户可以在其中输入最多大约 20kb 的文本。当他们打字时,我想继续将该 div 自动保存到 mysql 表中,以便他们以后可以在手机、平板电脑等上检索该文本。到目前为止,我只是每 20 秒自动保存一次,方法是获取整个文本块并执行以下操作对我的表中保存该文本的行的更新。
我意识到这种方法效率很低,而且一旦我的用户群增长就不再有效。有更好的方法吗?例如,以某种方式在原始文本和更改内容之间进行比较并仅保存它?使用node.js?如果您有任何想法,请告诉我。谢谢。
I have a contenteditable div where users can enter up to about 20kb of text. As they're typing I want to keep autosaving that div to a mysql table so that they can later retrieve that text on their phone, tablet etc. So far I've been simply autosaving every 20 seconds by taking the whole text block and doing an update to the row in my table that holds that text.
I realize that method is really inefficient and won't work once my user base grows. Is there a better way to do this? E.g, somehow take a diff between the original text and what changed and save only that? Use node.js? If you've got some idea, let me know. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
并非所有文本都是20KB,因此无需直接问题。您也可以选择减少自动保存,并且也可以自动执行会话,并且仅写入数据库的频率较低。
您可以计算存储版本(也可以保存在会话中)和键入版本之间的差异。优点是您不必经常使用数据库,但是处理将变得更加困难,因此网络服务器上的负载也会增加。
我可能会选择对存储表进行自动保护(您可以在MySQL中选择存储类型),然后编写一个单独的过程/CRON作业,以定期进行批量更新物理表。
Not all texts will be 20kb, so there doesn't need to be a direct problem. You could also choose to autosave less often, and you can autosave to a session too, and only write to the database less frequently.
You could calculate the differences between the stored version (which can be kept in the session too) and the typed version. The advantage is that you don't have to use the database so often, but processing will become harder, so the load on your webserver will increase too.
I'd maybe choose to do autosaves to a memory table (you can choose the storage type in MySQL), and write a separate process/cron job, that updates the physical table in bulk on regular intervals.
这让我想起了谷歌文档,他们有一个非常好的系统,可以自动将大文本保存到数据库中。现在我不知道谷歌文档的内部组件,但这是你可能想要研究的东西。
谷歌有更多的带宽,所以他们的方法可能不适合你,尽管他们似乎不断地将数据保存到他们的服务器上。
你可以做的是使用javascript并在用户端保存数据,并且仅在用户离开或单击“保存”时将数据加载到数据库中。这样,当他们返回页面时,只要保存文件在那里,他们就可以返回到他们的文件。
this reminds me of google docs, they have a pretty nice system of autosaving large text to the database. Now I don't know about the internal components of google docs, but that is something you might want to look into.
google has much more bandwidth so their method might not work for you, though it seems like they are constantly saving data to their servers.
what you could do is use javascript and save data on the user side, and only load the data into the database when the user leaves or clicks "save". This way, when they come back to the page, as long as the savefile is there, they can get back to their file.
一种方法:
使用ajax每x秒甚至通过x字符的倍数将文本发送回服务器(例如,每10、20、30个char等)。
使用服务器端代码(例如PHP)从此请求(作为快速有效)中完成大部分工作,您可以将文本保存在会话中。
在每个请求上,服务器端代码可以与上一个请求中的会话文本进行比较,然后将使您有可能执行以下操作之一:
a)如果新文本的启动与旧会话文本相同,则只需使用Say Concat来更新数据库中的字段:
然后将会话文本设置为新文本。
b)如果启动文本不同,那么您知道您需要进行直接UP UPDATE语句,以将字段值更改为新文本。
另一种方法:
One way:
Use ajax to send the text back to server every x seconds or even by multiples of x characters (e.g every 10, 20, 30 chars and so on).
Use server side code (eg php) to do most of the work from this request (as its fast and efficient) where you could hold the text in session.
On each request the server side code could compare with the session text from the last request and would then give you the possibility to do one of the following:
a) If start of new text is same as old session text then just update the field in database with the different using say concat:
and then set session text to new text.
b) If start text is different then you know you need to do a straight up update statement to change the field value to the new text.
Another way: