2

I am working on a chrome extension which has to store data of its user. For that I am using a hosted server which is running a mysql database. But currently any addition or change in data fires a request to the hosted server.

Chrome extension provides chrome.storage.local API which is suitable to store data upto 5mb. I want to take advantage of this storage API to reduce number of requests to my hosted server by using it as a temporary storage.

I am planning to use chrome.storage.onChanged.addListener and chrome.storage.local.getBytesInUse to check if data stored crosses a certain threshold value and then only fire an ajax request to the remote server to save the data. Upon successful response, the old data in chrome.storage will be flushed off.

But there are chances of losing some new data which is created during the process of request/response cycle from the server.

How can I prevent any loss of data? Is there any alternative solution to this optimization problem of reducing number of requests to the remote server from the extension?

Thanks.

4

1 回答 1

1

这不是关于 chrome 扩展的问题。它更多的是关于离线工作和智能同步的持久数据库。这恰好是一个很难正确解决的问题。

最简单的解决方案是使用 chrome.storage.sync。这可以让您免费获得持久性,但需要注意的是存储空间有限。在尝试其他选项之前,您绝对应该看看这是否可行。

否则,我建议您在推出自己的解决方案之前先查看第 3 方选项。您可能听说过渐进式网络应用程序,它可以离线工作,并在互联网可用时同步。

一篇关于渐进式网络应用程序优势的文章

谷歌教程

PouchDB,一个广受好评的网络数据库,可以离线工作并同步到其他数据库

看看那些。这将是非常值得的麻烦。否则,您最终只会在尝试同步工作的黑客之上构建黑客。

...最后一件事...确保将远程数据库的 URL 添加到 清单的权限中。

于 2017-05-07T20:56:24.393 回答