connector-wordpress/source/front/session-cache.js

28 lines
599 B
JavaScript
Raw Normal View History

import hash from './object-hash-wrapper.js'
const MAX_AGE_IN_MS = 120000
2021-04-13 20:50:56 +02:00
export default class SessionCache {
static add(storage, parameters, data) {
const key = hash(parameters)
const timestamp = Date.now()
const value = {
data,
timestamp,
}
storage.setItem(key, JSON.stringify(value))
}
static get(storage, parameters) {
const key = hash(parameters)
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
)
return value.data
return null
}
}