add storeObject

This commit is contained in:
Jay 2017-06-02 18:07:25 +08:00
parent ca9c86b104
commit c69679dfe2
8 changed files with 165 additions and 0 deletions

View File

@ -6,6 +6,7 @@ const fs = require('fs');
const config = require('./config.json');
// custom modules
const mysql = require('./libs/mysql_pool');
const so = require('./libs/storeObject');
// init mysql connection pool
mysql.database = config.db.mysql.dbname;
@ -67,5 +68,6 @@ const api_route = require('./route/api');
router.use("/api", api_route.routes());
router.get('/', async(c, n) => {
console.log(so.show());
c.body = 'Get root';
});

View File

@ -14,5 +14,9 @@
"port": 27017,
"dbname": "lora"
}
},
"memcached": {
"host": "localhost",
"port": 11211
}
}

View File

@ -0,0 +1,48 @@
const Memcached = require('memcached');
class memcachedLib {
constructor() {
this._host = ''
this._port = ''
this._expire = 86400
this._conn = null;
}
connect() {
this._conn = new Memcached(`${this._host}:${this._port}`)
}
/**
* set object to memcached
* @param {string} key
* @param {string} val
* @param {number} expire
*/
setVal(key, val, expire = this._expire) {
return new Promise((resolve, reject) => {
this._conn.set(key, val, err => {
if (err) return reject(err);
return resolve(null);
})
})
}
/**
* get object from memcached
* @param {string} key
*/
getVal(key) {
return new Promise((resolve, reject) => {
this._conn.get(key, (err, data) => {
if (err) return reject(err);
return resolve(data);
})
})
}
set host(str) { this._host = str }
set port(str) { this._port = str }
set expire(str) { this._expire = str }
}
module.exports = new memcachedLib();

View File

@ -0,0 +1,68 @@
const StoreObj = (() => {
let memStore = {};
let maxAge = 86400000;
/**
* @param {string} uuid
* @param {object} obj
*/
const set = (uuid, obj) => {
memStore[uuid] = {
obj,
age: Date.now() + maxAge
}
}
/**
* @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];
}
}
return null;
}
/**
* @param {string} uuid
*/
const chkKey = (uuid) => {
if (uuid in memStore) return true;
return false;
}
/**
* @param {string} uuid
*/
const del = (uuid) => {
if (uuid in memStore) delete memStore[uuid];
}
const clear = () => {
let t = Date.now();
for (var i in memStore) {
let s = memStore[i];
if (s.age < t) delete memStore[i];
}
}
/**
* @param {string} uuid if not input return all
*/
const show = (uuid) => {
if (uuid && uuid in memStore) return memStore[uuid];
return memStore;
}
return {set, get, chkKey, clear, del, show };
})()
module.exports = StoreObj;

View File

@ -12,6 +12,7 @@
"koa-router": "^7.2.0",
"koa-session": "^5.0.0",
"koa-static": "^3.0.0",
"memcached": "^2.2.2",
"mysql": "^2.13.0"
}
}

View File

@ -4,6 +4,7 @@ const koaBody = require('koa-body');
// import tools
const mysql = require('../../libs/mysql_pool.js');
const so = require('../../libs/storeObject');
const config = require('../../config.json');
// routes

View File

@ -3,6 +3,7 @@ const router = new Router();
// import tools
const crypto = require('../../libs/crypto.js');
const so = require('../../libs/storeObject');
router
.post('/login', async(c, n) => {
@ -25,6 +26,14 @@ router
} catch (err) {
return c.body = 'DB Query Error';
}
let u = c.body.record[0];
let uuid = null;
while (true) {
uuid = crypto.random(10);
if (!so.chkKey(uuid)) { break; }
}
so.set(uuid, u);
})
module.exports = router;

View File

@ -38,6 +38,10 @@ co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
connection-parse@0.0.x:
version "0.0.7"
resolved "https://registry.yarnpkg.com/connection-parse/-/connection-parse-0.0.7.tgz#18e7318aab06a699267372b10c5226d25a1c9a69"
content-disposition@~0.5.0:
version "0.5.2"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.2.tgz#0cf68bb9ddf5f2be7961c3a85178cb85dba78cb4"
@ -103,6 +107,13 @@ fresh@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.0.tgz#f474ca5e6a9246d6fd8e0953cfa9b9c805afa78e"
hashring@3.2.x:
version "3.2.0"
resolved "https://registry.yarnpkg.com/hashring/-/hashring-3.2.0.tgz#fda4efde8aa22cdb97fb1d2a65e88401e1c144ce"
dependencies:
connection-parse "0.0.x"
simple-lru-cache "0.0.x"
http-assert@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/http-assert/-/http-assert-1.3.0.tgz#a31a5cf88c873ecbb5796907d4d6f132e8c01e4a"
@ -147,6 +158,12 @@ isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
jackpot@>=0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/jackpot/-/jackpot-0.0.6.tgz#3cff064285cbf66f4eab2593c90bce816a821849"
dependencies:
retry "0.6.0"
kcors@2:
version "2.2.1"
resolved "https://registry.yarnpkg.com/kcors/-/kcors-2.2.1.tgz#7160a94f2eae633436d2cef8eadd0ce232386779"
@ -252,6 +269,13 @@ media-typer@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
memcached@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/memcached/-/memcached-2.2.2.tgz#68f86ccfd84bcf93cc25ed46d6d7fc0c7521c9d5"
dependencies:
hashring "3.2.x"
jackpot ">=0.0.6"
methods@^1.0.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
@ -364,6 +388,10 @@ resolve-path@^1.3.1:
http-errors "~1.5.0"
path-is-absolute "1.0.1"
retry@0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.6.0.tgz#1c010713279a6fd1e8def28af0c3ff1871caa537"
setprototypeof@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.0.2.tgz#81a552141ec104b88e89ce383103ad5c66564d08"
@ -372,6 +400,10 @@ setprototypeof@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.0.3.tgz#66567e37043eeb4f04d91bd658c0cbefb55b8e04"
simple-lru-cache@0.0.x:
version "0.0.2"
resolved "https://registry.yarnpkg.com/simple-lru-cache/-/simple-lru-cache-0.0.2.tgz#d59cc3a193c1a5d0320f84ee732f6e4713e511dd"
sqlstring@2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.2.0.tgz#c3135c4ea8abcd7e7ee741a4966a891d86a4f191"