我或我的应用如何知道 Chrome 正在丢弃索引的数据库项目?

pin*_*yid 5 google-chrome local-storage indexeddb

我有一个应用程序最近开始随机丢失 indexeddb 项目。我所说的“失去”是指他们被确认为已获救,但几天后,他们不再存在。

我的假设是 Chrome 正在丢弃 indexeddb 项目,因为磁盘已满。

我的问题是,我的应用程序是否可以侦听任何事件,或者我可以参考任何 Chrome 日志条目以确认情况是否如此。注意。我不是要解决问题,我只是在寻找可以检测到它的方法。

小智 0

每个应用程序都可以通过调用Quota API的queryUsageAndQuota()方法来查询存储了多少数据或者还有多少空间可供应用程序使用。

您可以使用定期的BackgroundSync 来估计分配给临时使用的已用空间和可用空间

// index.html
navigator.serviceWorker.ready.then(registration => {
  registration.periodicSync.register('estimate-storage', {
    // Minimum interval at which the sync may fire.
    minInterval: 24 * 60 * 60 * 1000,
  });
});

// service_worker.js

self.addEventListener('periodicsync', event => {
  if (event.tag == 'estimate-storage') {
    event.waitUntil(estimateAndNotify());
  }
});

To estimate use the method: navigator.storage.estimate()//returns a Promise which resolves with {usage, quota} values in bytes. 
Run Code Online (Sandbox Code Playgroud)

当超过临时存储配额时,为最早使用的源存储的所有数据(包括 AppCache、IndexedDB、WebSQL、文件系统 API)都将被删除。

您可以切换到无限存储或持久存储。