Skip to content

Commit df97445

Browse files
committed
Merge branch 'master' into develop
2 parents 3427e4c + 67bce21 commit df97445

File tree

6 files changed

+301
-36
lines changed

6 files changed

+301
-36
lines changed

lib/caching.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*jshint maxcomplexity:15*/
2+
/*jshint -W072 */
23
var domain = require('domain');
34

45
var caching = function(args) {
@@ -36,10 +37,10 @@ var caching = function(args) {
3637
* console.log(user);
3738
* });
3839
*/
39-
self.wrap = function(key, work, ttl, cb) {
40-
if (typeof ttl === 'function') {
41-
cb = ttl;
42-
ttl = undefined;
40+
self.wrap = function(key, work, options, cb) {
41+
if (typeof options === 'function') {
42+
cb = options;
43+
options = undefined;
4344
}
4445

4546
if (self.queues[key]) {
@@ -59,7 +60,7 @@ var caching = function(args) {
5960
});
6061
}
6162

62-
self.store.get(key, function(err, result) {
63+
self.store.get(key, options, function(err, result) {
6364
if (err && (!self.ignoreCacheErrors)) {
6465
fillCallbacks(err);
6566
} else if (result) {
@@ -75,7 +76,7 @@ var caching = function(args) {
7576
fillCallbacks(err);
7677
return;
7778
}
78-
self.store.set(key, data, ttl, function(err) {
79+
self.store.set(key, data, options, function(err) {
7980
if (err && (!self.ignoreCacheErrors)) {
8081
fillCallbacks(err);
8182
} else {

lib/multi_caching.js

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,47 @@ var domain = require('domain');
66
*/
77
var multi_caching = function(caches) {
88
var self = {};
9-
if (!Array.isArray(caches)) { throw new Error('multi_caching requires an array of caches'); }
9+
if (!Array.isArray(caches)) {
10+
throw new Error('multi_caching requires an array of caches');
11+
}
1012

1113
self.queues = {};
1214

13-
function get_from_highest_priority_cache(key, cb) {
15+
function get_from_highest_priority_cache(key, options, cb) {
16+
if (typeof options === 'function') {
17+
cb = options;
18+
options = undefined;
19+
}
20+
1421
var i = 0;
1522
async.forEachSeries(caches, function(cache, async_cb) {
16-
cache.store.get(key, function(err, result) {
17-
if (err) { return cb(err); }
23+
var callback = function(err, result) {
24+
if (err) {
25+
return cb(err);
26+
}
1827
if (result) {
1928
// break out of async loop.
2029
return cb(err, result, i);
2130
}
2231

2332
i += 1;
2433
async_cb(err);
25-
});
34+
};
35+
if (typeof options === 'object') {
36+
cache.store.get(key, options, callback);
37+
}else {
38+
cache.store.get(key, callback);
39+
}
2640
}, cb);
2741
}
2842

2943
function set_in_multiple_caches(caches, opts, cb) {
3044
async.forEach(caches, function(cache, async_cb) {
31-
cache.store.set(opts.key, opts.value, opts.ttl, async_cb);
45+
if (typeof opts.options !== 'object') {
46+
cache.store.set(opts.key, opts.value, opts.ttl, async_cb);
47+
} else {
48+
cache.store.set(opts.key, opts.value, opts.options, async_cb);
49+
}
3250
}, cb);
3351
}
3452

@@ -64,10 +82,10 @@ var multi_caching = function(caches) {
6482
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority
6583
* cache, it gets set in all higher-priority caches.
6684
*/
67-
self.wrap = function(key, work, ttl, cb) {
68-
if (typeof ttl === 'function') {
69-
cb = ttl;
70-
ttl = undefined;
85+
self.wrap = function(key, work, options, cb) {
86+
if (typeof options === 'function') {
87+
cb = options;
88+
options = undefined;
7189
}
7290

7391
if (self.queues[key]) {
@@ -95,27 +113,36 @@ var multi_caching = function(caches) {
95113
var opts = {
96114
key: key,
97115
value: result,
98-
ttl: ttl
116+
options: options
99117
};
118+
119+
if (typeof options !== 'object') {
120+
opts.ttl = options;
121+
}
122+
100123
set_in_multiple_caches(caches_to_update, opts, function(err) {
101124
fillCallbacks(err, result);
102125
});
103126
} else {
104127
domain
105-
.create()
106-
.on('error', function(err) {
107-
fillCallbacks(err);
108-
})
109-
.bind(work)(function(err, data) {
128+
.create()
129+
.on('error', function(err) {
130+
fillCallbacks(err);
131+
})
132+
.bind(work)(function(err, data) {
110133
if (err) {
111134
fillCallbacks(err);
112135
return;
113136
}
114137
var opts = {
115138
key: key,
116139
value: data,
117-
ttl: ttl
140+
options: options
118141
};
142+
143+
if (typeof options !== 'object') {
144+
opts.ttl = options;
145+
}
119146
set_in_multiple_caches(caches, opts, function(err) {
120147
if (err) {
121148
fillCallbacks(err);
@@ -128,22 +155,37 @@ var multi_caching = function(caches) {
128155
});
129156
};
130157

131-
self.set = function(key, value, ttl, cb) {
158+
self.set = function(key, value, options, cb) {
132159
var opts = {
133160
key: key,
134161
value: value,
135-
ttl: ttl
162+
options: options
136163
};
164+
if (typeof options !== 'object') {
165+
opts.ttl = options;
166+
}
137167
set_in_multiple_caches(caches, opts, cb);
138168
};
139169

140-
self.get = function(key, cb) {
141-
get_from_highest_priority_cache(key, cb);
170+
self.get = function(key, options, cb) {
171+
if (typeof options === 'function') {
172+
cb = options;
173+
options = false;
174+
}
175+
get_from_highest_priority_cache(key, options, cb);
142176
};
143177

144-
self.del = function(key, cb) {
178+
self.del = function(key, options, cb) {
179+
if (typeof options === 'function') {
180+
cb = options;
181+
options = false;
182+
}
145183
async.forEach(caches, function(cache, async_cb) {
146-
cache.store.del(key, async_cb);
184+
if (typeof options === 'object') {
185+
cache.store.del(key, options, async_cb);
186+
}else {
187+
cache.store.del(key, async_cb);
188+
}
147189
}, cb);
148190
};
149191

lib/stores/memory.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ var memory_store = function(args) {
1212

1313
var lru_cache = new Lru(lru_opts);
1414

15-
self.set = function(key, value, ttl, cb) {
15+
self.set = function(key, value, options, cb) {
1616
lru_cache.set(key, value);
1717
if (cb) {
1818
process.nextTick(cb);
1919
}
2020
};
2121

22-
self.get = function(key, cb) {
22+
self.get = function(key, options, cb) {
23+
if (typeof options === 'function') {
24+
cb = options;
25+
}
2326
var value = lru_cache.get(key);
2427
if (cb) {
2528
process.nextTick(function() {
@@ -30,7 +33,10 @@ var memory_store = function(args) {
3033
}
3134
};
3235

33-
self.del = function(key, cb) {
36+
self.del = function(key, options, cb) {
37+
if (typeof options === 'function') {
38+
cb = options;
39+
}
3440
lru_cache.del(key);
3541
if (cb) {
3642
process.nextTick(cb);

test/caching.unit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ describe("caching", function() {
403403
it("bubbles up that error", function(done) {
404404
var fake_error = new Error(support.random.string());
405405

406-
sinon.stub(memory_store_stub, 'get', function(key, cb) {
406+
sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
407407
cb(fake_error);
408408
});
409409

@@ -423,7 +423,7 @@ describe("caching", function() {
423423

424424
var fake_error = new Error(support.random.string());
425425

426-
sinon.stub(memory_store_stub, 'get', function(key, cb) {
426+
sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
427427
cb(fake_error);
428428
});
429429

test/multi_caching.unit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ describe("multi_caching", function() {
172172
});
173173
});
174174
});
175-
});
176-
}, 10);
175+
}, 10);
176+
});
177177
});
178178
});
179179
});

0 commit comments

Comments
 (0)