mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-28 20:21:10 -04:00
init crater
This commit is contained in:
34
resources/assets/js/components/base/base-table/expiring-storage.js
Executable file
34
resources/assets/js/components/base/base-table/expiring-storage.js
Executable file
@ -0,0 +1,34 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user