tornado-core/lib/Storage.js

40 lines
624 B
JavaScript
Raw Permalink Normal View History

2019-07-11 06:38:22 -04:00
class JsStorage {
constructor() {
2019-07-16 13:27:20 -04:00
this.db = {}
2019-07-11 06:38:22 -04:00
}
get(key) {
2019-07-16 13:27:20 -04:00
return this.db[key]
2019-07-11 06:38:22 -04:00
}
get_or_element(key, defaultElement) {
2019-07-16 13:27:20 -04:00
const element = this.db[key]
2019-07-11 06:38:22 -04:00
if (element === undefined) {
2019-07-16 13:27:20 -04:00
return defaultElement
2019-07-11 06:38:22 -04:00
} else {
2019-07-16 13:27:20 -04:00
return element
2019-07-11 06:38:22 -04:00
}
}
put(key, value) {
2019-07-11 10:04:36 -04:00
if (key === undefined || value === undefined) {
2019-07-16 13:27:20 -04:00
throw Error('key or value is undefined')
2019-07-11 10:04:36 -04:00
}
2019-07-16 13:27:20 -04:00
this.db[key] = value
2019-07-11 06:38:22 -04:00
}
del(key) {
2019-07-16 13:27:20 -04:00
delete this.db[key]
2019-07-11 06:38:22 -04:00
}
put_batch(key_values) {
key_values.forEach(element => {
2019-07-16 13:27:20 -04:00
this.db[element.key] = element.value
})
2019-07-11 06:38:22 -04:00
}
}
2019-07-16 13:27:20 -04:00
module.exports = JsStorage