【问题标题】:Local Storage using Dexie not staying persistent使用 Dexie 的本地存储不持久
【发布时间】:2020-10-30 22:18:42
【问题描述】:

我正在使用 Dexie 访问闪存卡制造商项目中的 IndexedDB。我可以按预期操作数据库,如果我关闭并重新打开浏览器,它会保留在数据库中,但通常当我在几天不处理项目后打开项目时,我放入数据库中的存储数据是'没有了。

我做了一些研究,发现我需要使数据库持久化,但我不知道该怎么做。

我已经将 Dexie Storage Manager page 中的推荐代码复制到主页 index.js 的 onLoad 函数中。以下是相关代码:

//When the page loads, display the decks in ul on the webpage.
window.addEventListener('load', onLoad);
async function onLoad() {
   
   //Check how much data is available.
   showEstimatedQuota();
 
   //Make sure the storage is persistent.
   isStoragePersisted().then(async isPersisted => {
     if (isPersisted) {
       console.log(":) Storage is successfully persisted.");
     } else {
       console.log(":( Storage is not persisted.");
       console.log("Trying to persist..:");
       if (await persist()) {
         console.log(":) We successfully turned the storage to be persisted.");
       } else {
         console.log(":( Failed to make storage persisted");
       }
     }
   });
}

上面的onLoad函数引用了我保存在dexie-setup.js上的三个函数:

//This function makes the storage persistent.
//(Copied from https://dexie.org/docs/StorageManager)
async function persist() {
  return await navigator.storage && navigator.storage.persist &&
    navigator.storage.persist();
}


//This function checks if the storage is persistent.
//(Copied from https://dexie.org/docs/StorageManager)
 async function isStoragePersisted() {
  return await navigator.storage && navigator.storage.persisted &&
    navigator.storage.persisted();
}


//This function logs to console how much data is available.
//(Copied from https://dexie.org/docs/StorageManager)
async function showEstimatedQuota() {
  if (navigator.storage && navigator.storage.estimate) {
    const estimation = await navigator.storage.estimate();
    console.log(`Quota: ${estimation.quota}`);
    console.log(`Usage: ${estimation.usage}`);
  } else {
    console.error("StorageManager not found");
  }
}

我的控制台日志:

  • dexie-setup.js:56 配额:6358499328

  • dexie-setup.js:57 用法:25370

  • index.js:30 :( 存储没有持久化。

  • index.js:31 试图 坚持..:

  • dexie-setup.js:84 完成检查 dexie。

  • index.js:33 :) 我们成功地将存储转换为持久化。

但是,如果我刷新页面,我会在控制台上记录相同的内容:数据库仍设置为非持久性。

showEstimatedQuota 函数检查数据存储并确认 DataStorage API 正在运行,所以我认为这不是问题所在。 (我只存储带有文本的小对象,所以无论如何我不希望超过存储限制。)

到目前为止,该项目在我的 chromebook 上完全是本地的,我正在 Chrome 浏览器上查看它。

请告诉我如何使我的数据库持久化。我对此很陌生(这是我关于stackoverflow的第一个问题!),所以希望这是一个容易解决的问题!提前致谢。

【问题讨论】:

    标签: javascript local-storage persistent-storage dexie


    【解决方案1】:

    引用 Dexie 的文档:“尽管 IndexedDB 是一个功能齐全的 Web 客户端数据库,但默认情况下它不是持久存储。没有 StorageManager 的 IndexedDB 只是一个可以擦除的“尽力而为”的数据库在设备磁盘空间不足的情况下。浏览器可能会在不通知用户的情况下删除您的数据库,以防它需要为其他网站的数据释放空间,这些数据比您最近使用的数据。"

    因此,您无法使数据库持久化。只需“尽力而为”。 此链接可能会有所帮助:

    1. https://web.dev/persistent-storage/
    2. Chrome.Storage.Local Persistence

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 2017-08-04
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多