这篇翻译不完整。请帮忙从英语翻译这篇文章。
在客户端已经有很多的 Web 技术用来存储各种各样的数据,浏览器运行的进程要搞清楚需要分配多少空间给 Web 数据存储、当达到存储限制哪些需要删除,并不是一件简单的事情而且不同浏览器间有很大不同。这篇文章尝试解释这些是怎么实现的。
注意: 以下信息对于大部分现代浏览器来说是很准确的,但是已知的浏览器的实现细节是,Opera 和 Chrome 行为是完全一致的,Opera Mini(仍然是 presto-based,在服务端渲染)并不在客户端保存任何数据。
数据存储的不同类型
即使在相同的浏览器、使用相同的存储方法,仍然存在不同的数据存储方法需要我们搞清楚。这部分我们讨论那些在不同浏览器之间的不同之处。
一般来说,数据存储的的类型主要有以下两种:
- 持久化存储:这种数据是希望长久保留的,只有的当用户选择清除才会被删除掉(比如,在 Firefox 中在每个页面的页面信息对话框中都有一个“清除数据(clear storage)” 的按钮)。
- 临时存储:这种数据不用保存很久,当最近一次使用时存储达到限制大小就会被自动清理掉(LRU policy)。
默认情况下,临时存储被用于大部分使用场景(比如,标准网页应用),而持久化数据存储被用于安装的本地应用(比如,安装在 Firefox OS 或者 Firefox 卓面的应用,Chrome 应用)。
Firefox 详细介绍
在 Firefox 中,当你使用 open()
创建 IndexedDB 实例时,你可以通过设置专有选项 storage 来选择使用哪种存储类型,例如:
-
var request = indexedDB.open("myDatabase", { version: 1, storage: "persistent" });
-
var request = indexedDB.open("myDatabase", { version: 1, storage: "temporary" });
当设置持久化存储类型时,浏览器会自动弹出一个对话框提示用户是否愿意让应用开启持久化存储。
相反,临时存储类型不会有任何弹窗,但是会有大小限制 Storage limits.
Firefox 的默认存储
在 Firefox 中还有第三个选择,它叫默认存储类型(Default Storage)。这是当你不设置 storage 选项时的默认设置。默认存储类型在不同的使用场景中,行为不是一直一样的,对于安装的本地 Firefox OS 应用,它是持久化存储,而其他使用场景则是临时数据存储。
数据存到哪里去了?
每种存储类型表示一个独立的仓库,下面是 Firefox 浏览器中不同类型映射到用户的 Firefox Profile 下的目录(其他浏览器可能有所不同)
<profile>/storage
— the main top level directory for storages maintained by the quota manager (see below.)<profile>/storage/permanent
— persistent data storage repository<profile>/storage/temporary
— temporary data storage repository<profile>/storage/default
— default data storage repository
Note: In Firefox, you can find your profile folder by going entering about:support
in the URL bar, and pressing the Show in... button (e.g. Show in Finder on Mac OS X) next to the Profile Folder title.
Note: If you are looking around in your Profile at the data stored, you might see a fourth folder: persistent
. Basically, the persistent
folder was renamed to permanent
a while ago to keep upgrades/migration simpler.
Note: Users shouldn’t add their own directories or files under <profile>/storage
. This will cause storage initialization to fail; for example open()
will fire an error event.
Storage limits
The maximum browser storage space is dynamic — it is based on your hard drive size. The global limit is calculated as 50% of free disk space. In Firefox, an internal browser tool called the Quota Manager keeps track of how much disk space each origin is using up, and deletes data if necessary.
So if your hard drive is 500GB, then the total storage for a browser is 250GB. If this is exceeded, a process called origin eviction comes into play, deleting entire origin's worth of data until the storage amount goes under the limit again. There is no trimming effect put in place, to delete parts of origins — deleting one database of an origin could cause problems with inconsistency.
There's also another limit called group limit — this is defined as 20% of the global limit. Each origin is part of a group (group of origins). There's one group for each eTLD+1 domain.
For example:
mozilla.org
— group1, origin1www.mozilla.org
— group1, origin2joe.blogs.mozilla.org
— group1, origin3firefox.com
— group2, origin4
In this group, mozilla.org
, www.mozilla.org
, and joe.blogs.mozilla.org
can aggregately use a maximum of 20% of the global limit. firefox.com
has a separate maximum of 20%.
The two limits react differently to limits being reached:
- The group limit is also called the "hard limit": it doesn't trigger origin eviction.
- The global limit is a "soft limit" since there's a chance that some space will be freed and the operation can continue.
Note: If the group limit is exceeded, or if origin eviction couldn't free enough space, the browser will throw a QuotaExceededError
.
LRU policy
When the available disk space is filled up, the quota manager will start clearing out data based on a LRU policy — the least recently used origin will be deleted first, then the next one, until the browser is no longer over the limit.
We track the "last access time" for each origin using temporary storage. Once the global limit for temporary storage is reached (more on the limit later), we try to find all currently unused origins (i.e. ones with no tabs/apps open that are keeping open datastores). These are then sorted according to "last access time". The least recently used origins are then deleted until there's enough space to fulfill the request that triggered this origin eviction.
What technologies use browser data storage?
In Firefox, the following technologies make use of browser data storage to store data when required. We term them "quota clients" in this context:
The "last access time" of origins is updated when any of these are activated/deactivated origin eviction will delete data for all these quota clients.
In Chrome/Opera, the Quota Management API handles quota management for AppCache, IndexedDB, WebSQL and File System API.
See also
- Working with quota on mobile browsers, by Eiji Kitamura. A detailed analysis of client-side storage on mobile browsers.
- Quota Management API : Fast Facts, by Eiji Kitamura. A look at the Quota Management API in Chrome/Blink (which should include Opera too.)