lora-project/server-api/libs/storeObject.js

75 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-06-02 10:07:25 +00:00
const StoreObj = (() => {
2017-06-06 12:40:51 +00:00
let memStore = {}
let maxAge = 86400000
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
/**
* @param {string} uuid
* @param {object} obj
*/
const set = (uuid, obj) => {
memStore[uuid] = {
obj,
age: Date.now() + maxAge
2017-06-02 10:07:25 +00:00
}
2017-06-06 12:40:51 +00:00
}
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
/**
* @param {string} uuid
*/
const get = (uuid) => {
if (uuid in memStore) {
let s = memStore[uuid]
if (Date.now() < s.age) {
s.age = Date.now() + maxAge
return s.obj
} else {
delete memStore[uuid]
}
2017-06-02 10:07:25 +00:00
}
2017-06-06 12:40:51 +00:00
return null
}
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
/**
* @param {string} uuid
*/
const chkKey = (uuid) => {
if (uuid in memStore) return true
return false
}
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
/**
* @param {string} uuid
*/
const del = (uuid) => {
if (uuid in memStore) delete memStore[uuid]
}
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
const clear = () => {
let t = Date.now()
for (var i in memStore) {
let s = memStore[i]
if (s.age < t) delete memStore[i]
2017-06-02 10:07:25 +00:00
}
2017-06-06 12:40:51 +00:00
}
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
/**
* @param {string} uuid if not input return all
*/
const show = (uuid) => {
if (uuid && uuid in memStore) return memStore[uuid]
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
return memStore
}
2017-06-02 10:07:25 +00:00
2017-06-06 12:40:51 +00:00
return {
set,
get,
chkKey,
clear,
del,
show
}
2017-06-02 10:07:25 +00:00
})()
2017-06-06 12:40:51 +00:00
module.exports = StoreObj