1- module . exports = function ( memoryClient , mongoDbClient , cacheLayer , settings , logger ) {
1+ module . exports = function ( memoryClient , mongoDbClient , mongooseClient , cacheLayer , settings , logger ) {
22
33 var pub = { } ,
44 clients = {
55 'memory' : memoryClient ,
66 'mongoDb' : mongoDbClient ,
7+ 'mongoose' : mongooseClient ,
78 } ;
89
910 var getStore = function ( ) {
@@ -13,42 +14,51 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
1314 pub . getByKey = function ( type , key , callback ) {
1415 // logger.log('Getting ' + type + ' ' + key + '...', 1);
1516
16- var cacheResult = cacheLayer . getByKey ( type , key ) ;
17- if ( cacheResult ) return callback ( null , cacheResult ) ;
17+ if ( settings . enableCache ) {
18+ var cacheResult = cacheLayer . getByKey ( type , key ) ;
19+ if ( cacheResult ) return callback ( null , cacheResult ) ;
20+ }
1821
1922 // logger.log(type + ' ' + key + ' not found in cache, getting from store...', 1);
2023
2124 getStore ( ) . getByKey ( type , key , function ( error , storeResult ) {
2225 if ( error ) return callback ( error ) ;
2326
24- if ( storeResult ) cacheLayer . upsertItem ( type , storeResult ) ;
27+ if ( settings . enableCache && storeResult ) cacheLayer . upsertItem ( type , storeResult ) ;
2528 callback ( null , storeResult ) ;
2629 } ) ;
2730 } ;
2831
2932 pub . getById = function ( type , id , callback ) {
30- var cacheResult = cacheLayer . getById ( type , id ) ;
31- if ( cacheResult . length ) return callback ( null , cacheResult ) ;
33+
34+ if ( settings . enableCache ) {
35+ var cacheResult = cacheLayer . getById ( type , id ) ;
36+ if ( cacheResult . length ) return callback ( null , cacheResult ) ;
37+ }
3238
3339 getStore ( ) . getById ( type , id , function ( error , storeResult ) {
3440 if ( error ) return callback ( error ) ;
3541
36- cacheLayer . upsertItem ( type , storeResult ) ;
42+ if ( settings . enableCache ) cacheLayer . upsertItem ( type , storeResult ) ;
3743 callback ( null , storeResult ) ;
3844 } ) ;
3945 } ;
4046
4147 pub . getByArmadaKey = function ( type , armadaKey , callback ) {
4248 // logger.log('Getting all ' + type + 's from armada ' + armadaKey + '...', 1);
4349
44- var cacheResult = cacheLayer . getByArmadaKey ( type , armadaKey ) ;
45- if ( cacheResult . length ) return callback ( null , cacheResult ) ;
50+ if ( settings . enableCache ) {
51+ var cacheResult = cacheLayer . getByArmadaKey ( type , armadaKey ) ;
52+ if ( cacheResult . length ) return callback ( null , cacheResult ) ;
53+ }
4654
4755 getStore ( ) . getByArmadaKey ( type , armadaKey , function ( error , storeResult ) {
4856 if ( error ) return callback ( error ) ;
4957
50- for ( var i in storeResult ) {
51- cacheLayer . upsertItem ( type , storeResult [ i ] ) ;
58+ if ( settings . enableCache ) {
59+ for ( var i in storeResult ) {
60+ cacheLayer . upsertItem ( type , storeResult [ i ] ) ;
61+ }
5262 }
5363
5464 callback ( null , storeResult ) ;
@@ -61,7 +71,7 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
6171 getStore ( ) . addItem ( type , item , function ( error ) {
6272 if ( error ) callback ( error ) ;
6373
64- cacheLayer . upsertItem ( type , item ) ;
74+ if ( settings . enableCache ) cacheLayer . upsertItem ( type , item ) ;
6575 callback ( null , true ) ;
6676 } ) ;
6777 } ;
@@ -72,7 +82,7 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
7282 getStore ( ) . removeByKey ( type , key , function ( error ) {
7383 if ( error ) callback ( error ) ;
7484
75- cacheLayer . removeByKey ( type , key ) ;
85+ if ( settings . enableCache ) cacheLayer . removeByKey ( type , key ) ;
7686 callback ( null , true ) ;
7787 } ) ;
7888 } ;
@@ -83,7 +93,7 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
8393 getStore ( ) . removeByArmadaKey ( type , armadaKey , function ( error ) {
8494 if ( error ) callback ( error ) ;
8595
86- cacheLayer . removeByArmadaKey ( type , armadaKey ) ;
96+ if ( settings . enableCache ) cacheLayer . removeByArmadaKey ( type , armadaKey ) ;
8797 callback ( null , true ) ;
8898 } ) ;
8999 } ;
@@ -94,7 +104,7 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
94104 getStore ( ) . removeBySystemId ( type , systemId , function ( error ) {
95105 if ( error ) callback ( error ) ;
96106
97- cacheLayer . removeBySystemId ( type , systemId ) ;
107+ if ( settings . enableCache ) cacheLayer . removeBySystemId ( type , systemId ) ;
98108 callback ( null , true ) ;
99109 } ) ;
100110 } ;
@@ -105,8 +115,10 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
105115 getStore ( ) . getAll ( type , function ( error , items ) {
106116 if ( error ) callback ( error ) ;
107117
108- for ( var i in items ) {
109- if ( items [ i ] . key ) cacheLayer . upsertItem ( type , items [ i ] ) ;
118+ if ( settings . enableCache ) {
119+ for ( var i in items ) {
120+ if ( items [ i ] . key ) cacheLayer . upsertItem ( type , items [ i ] ) ;
121+ }
110122 }
111123
112124 callback ( null , items ) ;
@@ -119,7 +131,7 @@ module.exports = function (memoryClient, mongoDbClient, cacheLayer, settings, lo
119131 getStore ( ) . updateItem ( type , key , item , function ( error ) {
120132 if ( error ) return callback ( error ) ;
121133
122- cacheLayer . upsertItem ( type , item ) ;
134+ if ( settings . enableCache ) cacheLayer . upsertItem ( type , item ) ;
123135 callback ( null , true ) ;
124136 } ) ;
125137 } ;
0 commit comments