Commit 9a8344f8 by 蒋勇

d

parent b3b23288
const system = require("../system")
const settings = require("../../config/settings.js");
class CacheBase {
constructor() {
this.redisClient = system.getObject("util.redisClient");
this.desc = this.desc();
this.prefix = this.prefix();
this.cacheCacheKeyPrefix = "sadd_children_appkeys:" + settings.appKey + "_cachekey";
this.isdebug = this.isdebug();
}
isdebug() {
return false;
}
desc() {
throw new Error("子类需要定义desc方法,返回缓存描述");
}
prefix() {
throw new Error("子类需要定义prefix方法,返回本缓存的前缀");
}
async cache(inputkey, val, ex, ...items) {
const cachekey = this.prefix + inputkey;
var cacheValue = await this.redisClient.get(cachekey);
if (!cacheValue || cacheValue == "undefined" || cacheValue == "null" || this.isdebug) {
var objvalstr = await this.buildCacheVal(cachekey, inputkey, val, ex, ...items);
if (!objvalstr) {
return null;
}
if (ex) {
await this.redisClient.setWithEx(cachekey, objvalstr, ex);
} else {
await this.redisClient.set(cachekey, objvalstr);
}
//缓存当前应用所有的缓存key及其描述
this.redisClient.sadd(this.cacheCacheKeyPrefix, [cachekey + "|" + this.desc]);
return JSON.parse(objvalstr);
} else {
return JSON.parse(cacheValue);
}
}
async invalidate(inputkey) {
const cachekey = this.prefix + inputkey;
this.redisClient.delete(cachekey);
return 0;
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
throw new Error("子类中实现构建缓存值的方法,返回字符串");
}
}
module.exports = CacheBase;
const CacheBase = require("../cache.base");
const system = require("../../system");
class ApiAccessControlCache extends CacheBase {
constructor() {
super();
}
desc() {
return "API访问控制缓存";
}
prefix() {
return "api_access_control_:";
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
return val;
}
}
module.exports = ApiAccessControlCache;
const CacheBase=require("../cache.base");
const system=require("../../system");
const settings = require("../../../config/settings");
class ApiAccessKeyCache extends CacheBase{
constructor(){
super();
this.restS=system.getObject("util.restClient");
}
desc(){
return "应用中缓存访问token";
}
prefix(){
return "g_accesskey_";
}
async buildCacheVal(cachekey,inputkey,val,ex,...items){
var acckapp=await this.restS.execPost({appkey:settings.appKey,secret:settings.secret},settings.paasUrl()+"api/auth/accessAuth/getAccessKey");
var s=acckapp.stdout;
if(s){
var tmp=JSON.parse(s);
if(tmp.status==0){
return JSON.stringify(tmp.data);
}
}
return null;
}
}
module.exports=ApiAccessKeyCache;
const CacheBase=require("../cache.base");
const system=require("../../system");
const settings = require("../../../config/settings");
//缓存首次登录的赠送的宝币数量
class ApiAccessKeyCheckCache extends CacheBase{
constructor(){
super();
this.restS=system.getObject("util.restClient");
}
desc(){
return "应用中来访访问token缓存";
}
prefix(){
return "g_accesskeycheck_";
}
async buildCacheVal(cachekey,inputkey,val,ex,...items){
var cacheManager=system.getObject("db.common.cacheManager");
//当来访key缓存不存在时,需要去开放平台检查是否存在来访key缓存
var acckapp=await cacheManager["ApiAccessKeyCache"].cache(settings.appKey,null,ex);//先获取本应用accessKey
var checkresult=await this.restS.execPostWithAK({checkAccessKey:inputkey},settings.paasUrl()+"api/auth/accessAuth/authAccessKey",acckapp.accessKey);
if(checkresult.status==0){
var s=checkresult.data;
return JSON.stringify(s);
}else{
await cacheManager["ApiAccessKeyCache"].invalidate(settings.appKey);
var acckapp=await cacheManager["ApiAccessKeyCache"].cache(settings.appKey,null,ex);//先获取本应用accessKey
var checkresult=await this.restS.execPostWithAK({checkAccessKey:inputkey},settings.paasUrl()+"api/auth/accessAuth/authAccessKey",acckapp.accessKey);
var s=checkresult.data;
return JSON.stringify(s);
}
}
}
module.exports=ApiAccessKeyCheckCache;
const CacheBase = require("../cache.base");
const system = require("../../system");
class MagCache extends CacheBase {
constructor() {
super();
this.prefix = "magCache";
}
desc() {
return "缓存管理";
}
prefix() {
return "magCache:";
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
return val;
}
async getCacheSmembersByKey(key) {
return this.redisClient.smembers(key);
}
async delCacheBySrem(key, value) {
return this.redisClient.srem(key, value)
}
async keys(p) {
return this.redisClient.keys(p);
}
async get(k) {
return this.redisClient.get(k);
}
async del(k) {
return this.redisClient.delete(k);
}
async clearAll() {
console.log("xxxxxxxxxxxxxxxxxxxclearAll............");
return this.redisClient.flushall();
}
}
module.exports = MagCache;
...@@ -6,6 +6,8 @@ var glob = require("glob"); ...@@ -6,6 +6,8 @@ var glob = require("glob");
class DbFactory{ class DbFactory{
constructor(){ constructor(){
const dbConfig=settings.database(); const dbConfig=settings.database();
console.log("ppppppppppppppppppppppppp");
console.log(dbConfig);
this.db=new Sequelize(dbConfig.dbname, this.db=new Sequelize(dbConfig.dbname,
dbConfig.user, dbConfig.user,
dbConfig.password, dbConfig.password,
......
...@@ -8,6 +8,7 @@ class TestTask extends TaskBase{ ...@@ -8,6 +8,7 @@ class TestTask extends TaskBase{
//this.isThrough=true; //this.isThrough=true;
} }
async subDoTask(params){ async subDoTask(params){
console.log(params);
console.log("TestTask1....."); console.log("TestTask1.....");
} }
} }
......
...@@ -6,9 +6,8 @@ var settings = { ...@@ -6,9 +6,8 @@ var settings = {
db: 11, db: 11,
}, },
database: function () { database: function () {
var args = process.argv;
return { return {
dbname: "alop", dbname: "tasks",
user: "write", user: "write",
password: "write", password: "write",
config: { config: {
......
var path = require('path'); var path = require('path');
var ENVINPUT={
DB_HOST:process.env.DB_HOST,
DB_PORT:process.env.DB_PORT,
DB_USER:process.env.DB_USER,
DB_PWD:process.env.DB_PWD,
REDIS_HOST:process.env.REDIS_HOST,
REDIS_PORT:process.env.REDIS_PORT,
REDIS_PWD:process.env.REDIS_PWD,
DB_NAME:process.env.TASK_DB_NAME,
REDIS_DB:process.env.TASK_REDIS_DB,
APP_ENV:process.env.APP_ENV?process.env.APP_ENV:"dev"
};
var settings = { var settings = {
env:"dev", env:ENVINPUT.APP_ENV,
basepath : path.normalize(path.join(__dirname, '../..')), basepath : path.normalize(path.join(__dirname, '../..')),
port : process.env.NODE_PORT || 3001, port : process.env.NODE_PORT || 3001,
redis:function(){ redis:function(){
...@@ -9,24 +21,25 @@ var settings = { ...@@ -9,24 +21,25 @@ var settings = {
return localsettings.redis; return localsettings.redis;
}else { }else {
return { return {
host:"xxxxx", host:ENVINPUT.REDIS_HOST,
port:xxxxx, port:ENVINPUT.REDIS_PORT,
password:"xxxxx", password:ENVINPUT.REDIS_PWD,
db:xx, db:ENVINPUT.REDIS_DB,
}; };
} }
}, },
database:function(){ database:function(){
if(this.env=="dev"){ if(this.env=="dev"){
console.log("===========================");
var localsettings=require("./localsettings"); var localsettings=require("./localsettings");
return localsettings.database(); return localsettings.database();
}else{ }else{
return { return {
dbname : "xxxxx", dbname : ENVINPUT.DB_NAME,
user : "xxxx", user : ENVINPUT.DB_USER,
password : "xxxxx", password : ENVINPUT.DB_PWD,
config : { config : {
host: 'xxxxxx', host: ENVINPUT.DB_HOST,
dialect: 'mysql', dialect: 'mysql',
operatorsAliases: false, operatorsAliases: false,
pool: { pool: {
...@@ -45,4 +58,5 @@ var settings = { ...@@ -45,4 +58,5 @@ var settings = {
} }
} }
}; };
settings.ENVINPUT=ENVINPUT;
module.exports = settings; module.exports = settings;
...@@ -6,9 +6,9 @@ con=dbf.getCon(); ...@@ -6,9 +6,9 @@ con=dbf.getCon();
var taskName = process.env.TASK_NAME; var taskName = process.env.TASK_NAME;
var params= process.env.TASK_PARAM; var params= process.env.TASK_PARAM;
if(taskName){ if(taskName){
var t=system.getObject("task.test."+taskName); var task=system.getObject("task."+taskName);
(async()=>{ (async()=>{
await t.doTask(params); await task.doTask(params);
console.log("process over............"); console.log("process over............");
})(); })();
}else{ }else{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment