how to delete indexedDB after 2 hours or less using timestamp Dexie.js instead of date search

Viewed 2407

Am using Dexie.js on my website to cache ajax response, but i would love to delete some old cache after 5 minites or more depending on the useage. So mu question is how do i delete cached data using time not date.

Saving to cache indexedDB

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);
});

Here is how i delete using date search

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

But i what to delete by time maybe after 1 hour, 60 hours or less, i don't know if is possible but i think it can be done.

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 Answers

I did this for keeping records fresh, I don't want anything older than four weeks, so I just used this:

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

Seems straightforward - are you trying to do something more complicated?

Related