2021-09-01 11:30:21 +00:00
|
|
|
class Cache {
|
2021-09-01 12:46:41 +00:00
|
|
|
constructor () {
|
|
|
|
this.kv = {}
|
2021-09-01 11:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {string} value
|
|
|
|
* @param {boolean?} noOverride
|
|
|
|
*/
|
2021-09-01 12:46:41 +00:00
|
|
|
set (key, value, noOverride) {
|
2021-09-01 11:30:21 +00:00
|
|
|
if (noOverride && key in this.kv) {
|
2021-09-01 12:46:41 +00:00
|
|
|
throw new Error('key exists')
|
2021-09-01 11:30:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
this.kv[key] = value
|
2021-09-01 11:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @return {string?}
|
|
|
|
*/
|
2021-09-01 12:46:41 +00:00
|
|
|
get (key) {
|
|
|
|
return this.kv[key] || null
|
2021-09-01 11:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string[]} keys
|
|
|
|
*/
|
2021-09-01 12:46:41 +00:00
|
|
|
del (...keys) {
|
2021-09-01 11:30:21 +00:00
|
|
|
for (const key of keys) {
|
2021-09-01 12:46:41 +00:00
|
|
|
delete this.kv[key]
|
2021-09-01 11:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
let cache = null
|
2021-09-01 11:30:21 +00:00
|
|
|
|
|
|
|
exports.new = function () {
|
2021-09-01 12:46:41 +00:00
|
|
|
if (cache) throw new Error('cache already initiate')
|
|
|
|
cache = new Cache()
|
|
|
|
return cache
|
|
|
|
}
|
2021-09-01 11:30:21 +00:00
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
/**
|
|
|
|
* @return {Cache}
|
|
|
|
*/
|
2021-09-01 11:30:21 +00:00
|
|
|
exports.get = function () {
|
2021-09-01 12:46:41 +00:00
|
|
|
if (!cache) throw new Error('cache not initiate')
|
|
|
|
return cache
|
|
|
|
}
|