HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何将到期时间应用于HTML5 indexeddb中的键?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
html5 api中的indexedDB中,我可以使用它来存储键值对.但是,如何确保在添加某个键值后1天后,该键应自动数据库删除.

我正在虑将值包装在具有当前日期时间和到期时间的对象中,当您获得该值时,请检查时差,但这是最好的方法吗?

谢谢

解决方法

是的,Scott Marcus和dgrogan说的是什么.还有一个提示:如果在时间戳上创建索引,则可以在“过期”值范围内迭代游标,并在打开数据库删除它们.

var open = indexedDB.open('demo');
open.onupgradeneeded = function() {
  var db = open.result;
  var store = db.createObjectStore('store');
  var index = store.createIndex('timestamp','timestamp');

  // Populate with some dummy data,with about half from the past:
  for (var id = 0; id < 20; ++id) {
    store.put({
      value: Math.random(),timestamp: new Date(Date.Now() + (Math.random()-0.5) * 10000)
    },id);
  }
};
open.onsuccess = function() {
  var db = open.result;
  var tx = db.transaction('store','readwrite');

  // Anything in the past:
  var range = IDBKeyRange.upperBound(new Date());

  tx.objectStore('store').index('timestamp').opencursor(rangE)
    .onsuccess = function(E) {
      var cursor = e.target.result;
      if (!cursor) return;
      console.log('deleting: ' + cursor.key);
      cursor.delete();
      cursor.conTinue();
    };

  // This transaction will run after the first commits since
  // it has overlapping scope:
  var tx2 = db.transaction('store');
  tx2.objectStore('store').count().onsuccess = function(E) {
    console.log('records remaining: ' + e.target.result);
  };
};

大佬总结

以上是大佬教程为你收集整理的如何将到期时间应用于HTML5 indexeddb中的键?全部内容,希望文章能够帮你解决如何将到期时间应用于HTML5 indexeddb中的键?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。