javascript - 在不填满磁盘的情况下检测 Firefox IndexedDB 或 Web Storage 存储限制?

标签 javascript firefox indexeddb gecko web-storage

我想使用 IndexedDB 处理大量 数据。太多数据无法放入内存。为此,我想使用 Firefox 的 IndexedDB 持久存储,它允许我存储超过 2GB 的数据(Firefox apparently has a limit of 2GB imposed on non-persistent storage)。

但是,我遇到了一个问题。 Firefox 似乎没有对我可以存储在持久存储中的数据量施加限制。事实上,如果我让下面的示例继续运行,它显然会一直运行到磁盘已满!

示例 ( Online )(必须在 Firefox 中运行!):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Firefox IndexedDB Limit Test</title>
    </head>
    <body>
        <script>
(function() {
'use strict';

var IDBReq = indexedDB.open('testdb', {
    version: 1,
    storage: 'persistent'
});

IDBReq.onupgradeneeded = function() {
    this.result.createObjectStore('data');
};

var logmsg;
IDBReq.onsuccess = function() {
    var DB = this.result;
    var size = 0;

    var next = function(i) {
        var data = new Uint8Array(0xFFFF);
        crypto.getRandomValues(data);

        size += data.length;
        logmsg = 'size: ' + size + 'b ' + (size / (1024 * 1024 * 1024)) + 'gb';

        var store = DB.transaction(['data'], 'readwrite').objectStore('data');
        var storeReq = store.add(data, 'data-' + i);
        storeReq.onsuccess = function() {
            next(i + 1);
        };
        storeReq.onerror = function() {
            console.log('storeReq error');
            console.log(this.error);
        };
    };
    next(1);
};
setInterval(function() {
    if (logmsg) {
        console.log(logmsg);
        logmsg = null;
    }
}, 1000);

})();
        </script>
    </body>
</html>

出于显而易见的原因,填满用户的驱动器并不理想。如果用户没有足够的可用磁盘空间,最好根本不要尝试运行它,或者至少在超过 X% 的空间时停止。

奇怪的是,according to MDN , 浏览器似乎应该对存储的数据量施加限制:

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.

但是,我的测试在它填满驱动器时从未抛出任何错误。

现在我的问题是,有什么方法可以确保将大量数据放入 Firefox 的 IndexedDB 持久存储中不会填满用户的驱动器,从而让他们非常非常生气?

最佳答案

注意 MDN 文章中的行:

Temporary data storage does not elicit any user prompts, but there are Storage limits.

它可能更清楚,但这意味着存储限制仅适用于临时存储。因此,全局限制和组限制对持久存储无效。

您可以考虑切换到临时存储。但是如果你需要持久性,那么在 Firefox 实现 navigator.storageQuota 之前你可能会倒霉。或 navigator.storageManager , 以哪个为准。

关于javascript - 在不填满磁盘的情况下检测 Firefox IndexedDB 或 Web Storage 存储限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43725895/

相关文章:

javascript - 如何将indexedDB游标结果绑定(bind)到自动完成输入框

javascript - 在 Jasmine 中使用 $q 测试 AngularJS 服务

javascript - Chrome - 停用 IndexedDB 进行测试

javascript - 将键盘焦点设置为 <div>

javascript - 使用 setInterval() 函数使图像闪烁

css - Internet Explorer 和 Firefox 中的 Twitter 小部件格式

firefox - Firefox 有内置的 Markdown 风格吗?

javascript - JS Function 执行后特定时间自禁用

javascript - 检查 Json 对象是否为空

python-3.x - 如何从网站中提取图片src