1
- /*jshint -W072 */
2
-
3
1
var async = require ( 'async' ) ;
4
2
var domain = require ( 'domain' ) ;
5
3
6
4
/**
7
5
* Module that lets you specify a hierarchy of caches.
8
6
*/
9
- var multi_caching = function ( caches ) {
7
+ var multi_caching = function ( caches ) {
10
8
var self = { } ;
11
9
if ( ! Array . isArray ( caches ) ) {
12
10
throw new Error ( 'multi_caching requires an array of caches' ) ;
@@ -21,40 +19,29 @@ var multi_caching = function (caches) {
21
19
}
22
20
23
21
var i = 0 ;
24
- async . forEachSeries ( caches , function ( cache , async_cb ) {
25
- if ( typeof options === 'object' ) {
26
- cache . store . get ( key , options , function ( err , result ) {
27
- if ( err ) {
28
- return cb ( err ) ;
29
- }
30
- if ( result ) {
31
- // break out of async loop.
32
- return cb ( err , result , i ) ;
33
- }
34
-
35
- i += 1 ;
36
- async_cb ( err ) ;
37
- } ) ;
38
- } else {
39
- cache . store . get ( key , function ( err , result ) {
40
- if ( err ) {
41
- return cb ( err ) ;
42
- }
43
- if ( result ) {
44
- // break out of async loop.
45
- return cb ( err , result , i ) ;
46
- }
22
+ async . forEachSeries ( caches , function ( cache , async_cb ) {
23
+ var callback = function ( err , result ) {
24
+ if ( err ) {
25
+ return cb ( err ) ;
26
+ }
27
+ if ( result ) {
28
+ // break out of async loop.
29
+ return cb ( err , result , i ) ;
30
+ }
47
31
48
- i += 1 ;
49
- async_cb ( err ) ;
50
- } ) ;
32
+ i += 1 ;
33
+ async_cb ( err ) ;
34
+ } ;
35
+ if ( typeof options === 'object' ) {
36
+ cache . store . get ( key , options , callback ) ;
37
+ } else {
38
+ cache . store . get ( key , callback ) ;
51
39
}
52
-
53
40
} , cb ) ;
54
41
}
55
42
56
43
function set_in_multiple_caches ( caches , opts , cb ) {
57
- async . forEach ( caches , function ( cache , async_cb ) {
44
+ async . forEach ( caches , function ( cache , async_cb ) {
58
45
if ( typeof opts . options !== 'object' ) {
59
46
cache . store . set ( opts . key , opts . value , opts . ttl , async_cb ) ;
60
47
} else {
@@ -68,8 +55,8 @@ var multi_caching = function (caches) {
68
55
*
69
56
* When a key is found in a lower cache, all higher levels are updated
70
57
*/
71
- self . get_and_pass_up = function ( key , cb ) {
72
- get_from_highest_priority_cache ( key , function ( err , result , index ) {
58
+ self . get_and_pass_up = function ( key , cb ) {
59
+ get_from_highest_priority_cache ( key , function ( err , result , index ) {
73
60
if ( err ) {
74
61
return cb ( err ) ;
75
62
}
@@ -78,7 +65,7 @@ var multi_caching = function (caches) {
78
65
79
66
if ( result !== undefined && index ) {
80
67
var cachesToUpdate = caches . slice ( 0 , index ) ;
81
- async . forEach ( cachesToUpdate , function ( cache , async_cb ) {
68
+ async . forEach ( cachesToUpdate , function ( cache , async_cb ) {
82
69
cache . set ( key , result , result . ttl , async_cb ) ;
83
70
} ) ;
84
71
}
@@ -95,7 +82,7 @@ var multi_caching = function (caches) {
95
82
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority
96
83
* cache, it gets set in all higher-priority caches.
97
84
*/
98
- self . wrap = function ( key , work , options , cb ) {
85
+ self . wrap = function ( key , work , options , cb ) {
99
86
if ( typeof options === 'function' ) {
100
87
cb = options ;
101
88
options = undefined ;
@@ -109,14 +96,14 @@ var multi_caching = function (caches) {
109
96
self . queues [ key ] = [ { cb : cb , domain : process . domain } ] ;
110
97
111
98
function fillCallbacks ( err , data ) {
112
- self . queues [ key ] . forEach ( function ( task ) {
99
+ self . queues [ key ] . forEach ( function ( task ) {
113
100
var taskDomain = task . domain || domain . create ( ) ;
114
101
taskDomain . bind ( task . cb ) ( err , data ) ;
115
102
} ) ;
116
103
delete self . queues [ key ] ;
117
104
}
118
105
119
- get_from_highest_priority_cache ( key , function ( err , result , index ) {
106
+ get_from_highest_priority_cache ( key , function ( err , result , index ) {
120
107
if ( err ) {
121
108
return fillCallbacks ( err ) ;
122
109
} else if ( result ) {
@@ -131,16 +118,16 @@ var multi_caching = function (caches) {
131
118
opts . ttl = options ;
132
119
}
133
120
134
- set_in_multiple_caches ( caches_to_update , opts , function ( err ) {
121
+ set_in_multiple_caches ( caches_to_update , opts , function ( err ) {
135
122
fillCallbacks ( err , result ) ;
136
123
} ) ;
137
124
} else {
138
125
domain
139
126
. create ( )
140
- . on ( 'error' , function ( err ) {
127
+ . on ( 'error' , function ( err ) {
141
128
fillCallbacks ( err ) ;
142
129
} )
143
- . bind ( work ) ( function ( err , data ) {
130
+ . bind ( work ) ( function ( err , data ) {
144
131
if ( err ) {
145
132
fillCallbacks ( err ) ;
146
133
return ;
@@ -154,7 +141,7 @@ var multi_caching = function (caches) {
154
141
if ( typeof options !== 'object' ) {
155
142
opts . ttl = options ;
156
143
}
157
- set_in_multiple_caches ( caches , opts , function ( err ) {
144
+ set_in_multiple_caches ( caches , opts , function ( err ) {
158
145
if ( err ) {
159
146
fillCallbacks ( err ) ;
160
147
} else {
@@ -166,7 +153,7 @@ var multi_caching = function (caches) {
166
153
} ) ;
167
154
} ;
168
155
169
- self . set = function ( key , value , options , cb ) {
156
+ self . set = function ( key , value , options , cb ) {
170
157
var opts = {
171
158
key : key ,
172
159
value : value ,
@@ -178,24 +165,24 @@ var multi_caching = function (caches) {
178
165
set_in_multiple_caches ( caches , opts , cb ) ;
179
166
} ;
180
167
181
- self . get = function ( key , options , cb ) {
168
+ self . get = function ( key , options , cb ) {
182
169
if ( typeof options === 'function' ) {
183
170
cb = options ;
184
171
options = false ;
185
172
}
186
173
get_from_highest_priority_cache ( key , options , cb ) ;
187
174
} ;
188
175
189
- self . del = function ( key , options , cb ) {
176
+ self . del = function ( key , options , cb ) {
190
177
if ( typeof options === 'function' ) {
191
178
cb = options ;
192
179
options = false ;
193
180
}
194
- async . forEach ( caches , function ( cache , async_cb ) {
195
- if ( typeof options === 'object' ) {
196
- cache . store . del ( key , options , async_cb ) ;
197
- } else {
198
- cache . store . del ( key , async_cb ) ;
181
+ async . forEach ( caches , function ( cache , async_cb ) {
182
+ if ( typeof options === 'object' ) {
183
+ cache . store . del ( key , options , async_cb ) ;
184
+ } else {
185
+ cache . store . del ( key , async_cb ) ;
199
186
}
200
187
} , cb ) ;
201
188
} ;
0 commit comments