Which HTML5 offline storage solution should I use

2019-05-07 12:10发布

问题:

I'm writing a Chrome app in which users can create and write notes(plain text), I'd like to the notes to be saved offline.

I've learned that there are several storage solutions from html5: localStorage, Web SQL DB, Application Cache. I'd like to know your advice on how to choose from them, considering the size they offer, the speed, and the convenience of use.

回答1:

localStorage is by far the simplest solution and it's the one you should use for this task. It allows you to store strings, so as long as your data can be represented as strings - it will work (usual practice is to convert non-string data to json before saving, then parse is back).

WebDB is your normal database. If you need to run complicated queries on your data across multiple tables, then you can use it. For storing notes it would be an overkill.

Application cache is something completely different, I don't see how you can apply it here. It is for caching html pages and resources.



回答2:

Local Storage

This is basically a key/value database, you could use this instead of cookies to store settings or other usecases you might use components like memcache on the server for. I would personally use this for data that needs to be cached.

Application Cache

I assume you mean caching by the means of a manifest file. This should complement your other uses of caching. This allows you to cache actual resources on the users device, such as images, javascript files and html files so they can work in your application when they are disconnected from the internet

Web SQL DB

This is a database in which you want to store user data, you might want to use it for data you would store in a sql database on the server too.



回答3:

According to this: https://developer.chrome.com/apps/offline_apps#saving-locally

Packaged apps cannot use Web SQL Database or localStorage. The WebSQL specification has been deprecated for awhile now, and localStorage handles data synchronously (which means it can be slow). The Storage API handles data asynchronously.

IndexedDB is the recommended storage mechanism.