2022-01-09 12:11:24 +01:00
|
|
|
import hash from './object-hash-wrapper.js'
|
2021-04-04 18:03:05 +02:00
|
|
|
|
|
|
|
const MAX_AGE_IN_MS = 120000
|
|
|
|
|
2021-04-13 20:50:56 +02:00
|
|
|
export default class SessionCache {
|
2021-04-05 18:23:37 +02:00
|
|
|
static add(storage, parameters, data) {
|
2021-04-04 18:03:05 +02:00
|
|
|
const key = hash(parameters)
|
|
|
|
const timestamp = Date.now()
|
|
|
|
const value = {
|
|
|
|
data,
|
|
|
|
timestamp,
|
|
|
|
}
|
2021-04-05 18:23:37 +02:00
|
|
|
storage.setItem(key, JSON.stringify(value))
|
2021-04-04 18:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 18:23:37 +02:00
|
|
|
static get(storage, parameters) {
|
2021-04-04 18:03:05 +02:00
|
|
|
const key = hash(parameters)
|
2021-04-05 18:23:37 +02:00
|
|
|
const value = JSON.parse(storage.getItem(key))
|
2021-12-09 19:46:19 +01:00
|
|
|
if (
|
|
|
|
value &&
|
|
|
|
value.timestamp &&
|
|
|
|
value.timestamp > Date.now() - MAX_AGE_IN_MS
|
|
|
|
)
|
2021-04-04 18:03:05 +02:00
|
|
|
return value.data
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|