@@ -10,13 +10,30 @@ import { LoginProvider } from "./LoginProvider";
1010import * as cacheManager from "cache-manager" ;
1111// var cacheManager = require('cache-manager');
1212var redisStore = require ( 'cache-manager-ioredis' ) ;
13+ var mongoStore = require ( '@skadefro/cache-manager-mongodb' ) ;
1314
1415export class DBHelper {
1516
1617 public static memoryCache : any ;
18+ public static mongoCache : any ;
1719 public static async init ( ) {
1820 if ( ! NoderedUtil . IsNullUndefinded ( this . memoryCache ) ) return ;
19- if ( Config . cache_store_type == "redis" ) {
21+
22+ this . mongoCache = cacheManager . caching ( {
23+ store : mongoStore ,
24+ uri : Config . mongodb_url ,
25+ options : {
26+ collection : "_cache" ,
27+ compression : false ,
28+ poolSize : 5
29+ }
30+ } ) ;
31+
32+ if ( Config . cache_store_type == "mongodb" ) {
33+ this . memoryCache = this . mongoCache ;
34+ DBHelper . ensureotel ( ) ;
35+ return ;
36+ } else if ( Config . cache_store_type == "redis" ) {
2037 this . memoryCache = cacheManager . caching ( {
2138 store : redisStore ,
2239 host : Config . cache_store_redis_host ,
@@ -32,6 +49,7 @@ export class DBHelper {
3249 redisClient . on ( 'error' , ( error ) => {
3350 console . log ( error ) ;
3451 } ) ;
52+
3553 DBHelper . ensureotel ( ) ;
3654 return ;
3755 }
@@ -45,8 +63,6 @@ export class DBHelper {
4563 }
4664 public static async clearCache ( reason : string ) {
4765 this . init ( ) ;
48- // Auth.ensureotel();
49- // this.memoryCache.reset();
5066 var keys : string [ ] ;
5167 if ( Config . cache_store_type == "redis" ) {
5268 keys = await this . memoryCache . keys ( '*' ) ;
@@ -71,16 +87,6 @@ export class DBHelper {
7187 DBHelper . item_cache = Logger . otel . meter . createValueObserver ( "openflow_item_cache_count" , {
7288 description : 'Total number of cached items'
7389 } , async ( res ) => {
74- // let keys: string[] = Object.keys(this.authorizationCache);
75- // let types = {};
76- // for (let i = keys.length - 1; i >= 0; i--) {
77- // if (!types[this.authorizationCache[keys[i]].type]) types[this.authorizationCache[keys[i]].type] = 0;
78- // types[this.authorizationCache[keys[i]].type]++;
79- // }
80- // keys = Object.keys(types);
81- // for (let i = keys.length - 1; i >= 0; i--) {
82- // res.observe(types[keys[i]], { ...Logger.otel.defaultlabels, type: keys[i] })
83- // }
8490 var keys : any = null ;
8591 try {
8692 if ( Config . cache_store_type == "redis" ) {
@@ -105,7 +111,6 @@ export class DBHelper {
105111 try {
106112 if ( NoderedUtil . IsNullEmpty ( _id ) ) return null ;
107113 let item = await this . memoryCache . wrap ( "users" + _id , ( ) => {
108- // if (jwt === null || jwt == undefined || jwt == "") { jwt = Crypt.rootToken(); }
109114 if ( Config . log_cache ) Logger . instanse . debug ( "Add user to cache : " + _id ) ;
110115 return Config . db . getbyid < User > ( _id , "users" , Crypt . rootToken ( ) , true , span ) ;
111116 } ) ;
@@ -120,36 +125,48 @@ export class DBHelper {
120125 Logger . otel . endSpan ( span ) ;
121126 }
122127 }
123- public static async FindRequestTokenID ( key : string , parent : Span ) : Promise < any > {
128+ public static async FindRequestTokenID ( key : string , parent : Span ) : Promise < TokenRequest > {
124129 this . init ( ) ;
125130 const span : Span = Logger . otel . startSubSpan ( "dbhelper.FindRequestTokenID" , parent ) ;
126131 try {
127132 if ( NoderedUtil . IsNullEmpty ( key ) ) return null ;
128- return await this . memoryCache . get ( "requesttoken" + key ) ;
133+ if ( Config . cache_store_type == "redis" ) {
134+ return await this . memoryCache . get ( "requesttoken" + key ) ;
135+ } else {
136+ return await this . mongoCache . get ( "requesttoken" + key ) ;
137+ }
129138 } catch ( error ) {
130139 span ?. recordException ( error ) ;
131140 throw error ;
132141 } finally {
133142 Logger . otel . endSpan ( span ) ;
134143 }
135144 }
136- public static async AdddRequestTokenID ( key : string , data : any , parent : Span ) : Promise < any > {
145+ public static async AdddRequestTokenID ( key : string , data : any , parent : Span ) : Promise < TokenRequest > {
137146 this . init ( ) ;
138147 const span : Span = Logger . otel . startSubSpan ( "dbhelper.FindRequestTokenID" , parent ) ;
139148 try {
140- return await this . memoryCache . set ( "requesttoken" + key , data ) ;
149+ if ( Config . cache_store_type == "redis" ) {
150+ return await this . memoryCache . set ( "requesttoken" + key , data ) ;
151+ } else {
152+ return await this . mongoCache . set ( "requesttoken" + key , data ) ;
153+ }
141154 } catch ( error ) {
142155 span ?. recordException ( error ) ;
143156 throw error ;
144157 } finally {
145158 Logger . otel . endSpan ( span ) ;
146159 }
147160 }
148- public static async RemoveRequestTokenID ( key : string , parent : Span ) : Promise < any > {
161+ public static async RemoveRequestTokenID ( key : string , parent : Span ) : Promise < TokenRequest > {
149162 this . init ( ) ;
150163 const span : Span = Logger . otel . startSubSpan ( "dbhelper.FindRequestTokenID" , parent ) ;
151164 try {
152- return await this . memoryCache . del ( "requesttoken" + key ) ;
165+ if ( Config . cache_store_type == "redis" ) {
166+ return await this . memoryCache . del ( "requesttoken" + key ) ;
167+ } else {
168+ return await this . mongoCache . del ( "requesttoken" + key ) ;
169+ }
153170 } catch ( error ) {
154171 span ?. recordException ( error ) ;
155172 throw error ;
@@ -598,4 +615,13 @@ export class DBHelper {
598615 return { $set : updatedoc } ;
599616 }
600617 }
618+ }
619+ export class TokenRequest extends Base {
620+ constructor ( code : string ) {
621+ super ( ) ;
622+ this . _type = "tokenrequest" ;
623+ if ( NoderedUtil . IsNullEmpty ( code ) ) this . code = "" ;
624+ }
625+ public code : string ;
626+ public jwt : string ;
601627}
0 commit comments