Skip to content

Commit 67bce21

Browse files
committed
Merge pull request jaredwray#20 from seanzx85/options
Add Additional Options Parameter
2 parents 8a014ee + 519e745 commit 67bce21

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]) {
@@ -57,7 +58,7 @@ var caching = function(args) {
5758
delete self.queues[key];
5859
}
5960

60-
self.store.get(key, function(err, result) {
61+
self.store.get(key, options, function(err, result) {
6162
if (err && (!self.ignoreCacheErrors)) {
6263
fillCallbacks(err);
6364
} else if (result) {
@@ -73,7 +74,7 @@ var caching = function(args) {
7374
fillCallbacks(err);
7475
return;
7576
}
76-
self.store.set(key, data, ttl, function(err) {
77+
self.store.set(key, data, options, function(err) {
7778
if (err && (!self.ignoreCacheErrors)) {
7879
fillCallbacks(err);
7980
} 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]) {
@@ -93,27 +111,36 @@ var multi_caching = function(caches) {
93111
var opts = {
94112
key: key,
95113
value: result,
96-
ttl: ttl
114+
options: options
97115
};
116+
117+
if (typeof options !== 'object') {
118+
opts.ttl = options;
119+
}
120+
98121
set_in_multiple_caches(caches_to_update, opts, function(err) {
99122
fillCallbacks(err, result);
100123
});
101124
} else {
102125
domain
103-
.create()
104-
.on('error', function(err) {
105-
fillCallbacks(err);
106-
})
107-
.bind(work)(function(err, data) {
126+
.create()
127+
.on('error', function(err) {
128+
fillCallbacks(err);
129+
})
130+
.bind(work)(function(err, data) {
108131
if (err) {
109132
fillCallbacks(err);
110133
return;
111134
}
112135
var opts = {
113136
key: key,
114137
value: data,
115-
ttl: ttl
138+
options: options
116139
};
140+
141+
if (typeof options !== 'object') {
142+
opts.ttl = options;
143+
}
117144
set_in_multiple_caches(caches, opts, function(err) {
118145
if (err) {
119146
fillCallbacks(err);
@@ -126,22 +153,37 @@ var multi_caching = function(caches) {
126153
});
127154
};
128155

129-
self.set = function(key, value, ttl, cb) {
156+
self.set = function(key, value, options, cb) {
130157
var opts = {
131158
key: key,
132159
value: value,
133-
ttl: ttl
160+
options: options
134161
};
162+
if (typeof options !== 'object') {
163+
opts.ttl = options;
164+
}
135165
set_in_multiple_caches(caches, opts, cb);
136166
};
137167

138-
self.get = function(key, cb) {
139-
get_from_highest_priority_cache(key, cb);
168+
self.get = function(key, options, cb) {
169+
if (typeof options === 'function') {
170+
cb = options;
171+
options = false;
172+
}
173+
get_from_highest_priority_cache(key, options, cb);
140174
};
141175

142-
self.del = function(key, cb) {
176+
self.del = function(key, options, cb) {
177+
if (typeof options === 'function') {
178+
cb = options;
179+
options = false;
180+
}
143181
async.forEach(caches, function(cache, async_cb) {
144-
cache.store.del(key, async_cb);
182+
if (typeof options === 'object') {
183+
cache.store.del(key, options, async_cb);
184+
}else {
185+
cache.store.del(key, async_cb);
186+
}
145187
}, cb);
146188
};
147189

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
@@ -379,7 +379,7 @@ describe("caching", function() {
379379
it("bubbles up that error", function(done) {
380380
var fake_error = new Error(support.random.string());
381381

382-
sinon.stub(memory_store_stub, 'get', function(key, cb) {
382+
sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
383383
cb(fake_error);
384384
});
385385

@@ -399,7 +399,7 @@ describe("caching", function() {
399399

400400
var fake_error = new Error(support.random.string());
401401

402-
sinon.stub(memory_store_stub, 'get', function(key, cb) {
402+
sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
403403
cb(fake_error);
404404
});
405405

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)