Files
crater/resources/assets/js/components/base/base-table/expiring-storage.js
Mohit Panjwani bdf2ba51d6 init crater
2019-11-11 12:16:00 +05:30

35 lines
639 B
JavaScript
Executable File

class ExpiringStorage {
get (key) {
const cached = JSON.parse(
localStorage.getItem(key)
)
if (!cached) {
return null
}
const expires = new Date(cached.expires)
if (expires < new Date()) {
localStorage.removeItem(key)
return null
}
return cached.value
}
has (key) {
return this.get(key) !== null
}
set (key, value, lifeTimeInMinutes) {
const currentTime = new Date().getTime()
const expires = new Date(currentTime + lifeTimeInMinutes * 60000)
localStorage.setItem(key, JSON.stringify({ value, expires }))
}
}
export default new ExpiringStorage()