代码之家  ›  专栏  ›  技术社区  ›  Peter

如何使用时间戳在2小时或更短时间后删除indexedDB德克西.js而不是日期搜索

  •  0
  • Peter  · 技术社区  · 7 年前

    Dexie.js 在我的网站上缓存ajax响应,但我想删除一些旧的缓存后5分钟或更多取决于使用。 所以mu的问题是如何使用时间而不是日期删除缓存的数据。

    function getTodayDate(){
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; 
        var yyyy = today.getFullYear();
        if(dd<10) {dd = '0'+dd} 
        if(mm<10) {mm = '0'+mm} 
        return  mm + '-' + dd + '-' + yyyy;
    }
    
    var time = new Date().getTime();
    db.product.put({nameId: "abc163", text: "hello world", cacheUrl: "example.com", timestamp: time, date: getTodayDate()})
    .catch(function(error) {
         console.log ("Ooops: " + error);
    });
    

    下面是我如何使用日期搜索删除

    db.product.where("date").below(getTodayDate()).delete()
    .then(function (deleteCount) {
        console.log( "Deleted " + deleteCount + " objects");
    });
    

    但我要删除的时间可能是1小时后,60小时或更少,我不知道是否有可能,但我认为它可以做到。

    function checkTime(){
        var delete_after = 300;
        //How do i check it
    }
    
    db.product.where("timestamp").below(checkTime()).delete()
    .then(function (deleteCount) {
        console.log( "Deleted " + deleteCount + " objects");
    });
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   masoumeh Tom    7 年前

    我这样做是为了保持记录的新鲜,我不想要任何超过四周的东西,所以我用了这个:

    db.records.where('timestamp').below(Date.now() - 2419200000).delete()
    

    看起来很简单-你想做更复杂的事吗?

    推荐文章