Skip to content

Commit 2a51739

Browse files
committed
changing "run()" to "wrap()", allowing us to pass in our own custom store.
1 parent eeb9f7f commit 2a51739

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

Diff for: lib/caching.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
var caching = function(args) {
22
args = args || {};
33
var self = {};
4-
self.store_name = args.store || 'redis';
5-
self.store = require('./stores/' + self.store_name).create(args);
4+
if (typeof args.store === 'object') {
5+
self.store = args.store;
6+
} else if (typeof args.store === 'string' && args.store.match(/\//)) {
7+
self.store = require(args.store).create(args);
8+
} else {
9+
var store_name = args.store || 'redis';
10+
self.store = require('./stores/' + store_name).create(args);
11+
}
612

713
/**
814
* Wraps a function in cache. I.e., the first time the function is run,
@@ -12,13 +18,13 @@ var caching = function(args) {
1218
* @example
1319
*
1420
* var key = 'user_' + user_id;
15-
* cache.run(key, function(cb) {
16-
* user_adapter.get(user_id, cb);
21+
* cache.wrap(key, function(cb) {
22+
* User.get(user_id, cb);
1723
* }, function(err, user) {
1824
* console.log(user);
1925
* });
2026
*/
21-
self.run = function(key, work, cb) {
27+
self.wrap = function(key, work, cb) {
2228
self.store.get(key, function(err, result) {
2329
if (err) { return cb(err); }
2430
if (result) {

Diff for: lib/multi_caching.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var multi_caching = function(caches) {
3939
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority
4040
* cache, it gets set in all higher-priority caches.
4141
*/
42-
self.run = function(key, work, cb) {
42+
self.wrap = function(key, work, cb) {
4343
get_from_highest_priority_cache(key, function(err, result, index) {
4444
if (err) { return cb(err); }
4545
if (result) {

Diff for: test/caching.unit.js

+37-11
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe("caching", function() {
8787
});
8888
});
8989

90-
describe("run()", function() {
90+
describe("wrap()", function() {
9191
context("using redis store", function() {
9292
var redis_client;
9393

@@ -107,7 +107,7 @@ describe("caching", function() {
107107
});
108108

109109
it("calls back with the result of the wrapped function", function(done) {
110-
cache.run(key, function(cb) {
110+
cache.wrap(key, function(cb) {
111111
get_widget(name, cb);
112112
}, function(err, widget) {
113113
check_err(err);
@@ -117,7 +117,7 @@ describe("caching", function() {
117117
});
118118

119119
it("caches the result of the function in redis", function(done) {
120-
cache.run(key, function(cb) {
120+
cache.wrap(key, function(cb) {
121121
get_widget(name, cb);
122122
}, function(err, widget) {
123123
check_err(err);
@@ -132,7 +132,7 @@ describe("caching", function() {
132132
});
133133

134134
it("retrieves data from redis when available", function(done) {
135-
cache.run(key, function(cb) {
135+
cache.wrap(key, function(cb) {
136136
get_widget(name, cb);
137137
}, function(err, widget) {
138138
check_err(err);
@@ -142,7 +142,7 @@ describe("caching", function() {
142142

143143
sinon.spy(redis_client, 'get');
144144

145-
cache.run(key, function(cb) {
145+
cache.wrap(key, function(cb) {
146146
get_widget(name, cb);
147147
}, function(err, widget) {
148148
check_err(err);
@@ -162,7 +162,7 @@ describe("caching", function() {
162162
});
163163

164164
it("expires cached result after ttl seconds", function(done) {
165-
cache.run(key, function(cb) {
165+
cache.wrap(key, function(cb) {
166166
get_widget(name, cb);
167167
}, function(err, widget) {
168168
check_err(err);
@@ -197,7 +197,7 @@ describe("caching", function() {
197197
});
198198

199199
it("calls back with the result of a function", function(done) {
200-
cache.run(key, function(cb) {
200+
cache.wrap(key, function(cb) {
201201
get_widget(name, cb);
202202
}, function(err, widget) {
203203
check_err(err);
@@ -207,7 +207,7 @@ describe("caching", function() {
207207
});
208208

209209
it("retrieves data from memory when available", function(done) {
210-
cache.run(key, function(cb) {
210+
cache.wrap(key, function(cb) {
211211
get_widget(name, cb);
212212
}, function(err, widget) {
213213
check_err(err);
@@ -218,7 +218,7 @@ describe("caching", function() {
218218
sinon.spy(memory_store_stub, 'get');
219219
var func_called = false;
220220

221-
cache.run(key, function(cb) {
221+
cache.wrap(key, function(cb) {
222222
get_widget(name, function(err, result) {
223223
func_called = true;
224224
cb(err, result);
@@ -236,7 +236,7 @@ describe("caching", function() {
236236
});
237237

238238
it("expires cached result after ttl seconds", function(done) {
239-
cache.run(key, function(cb) {
239+
cache.wrap(key, function(cb) {
240240
get_widget(name, cb);
241241
}, function(err, widget) {
242242
check_err(err);
@@ -248,7 +248,7 @@ describe("caching", function() {
248248
var func_called = false;
249249

250250
setTimeout(function () {
251-
cache.run(key, function(cb) {
251+
cache.wrap(key, function(cb) {
252252
get_widget(name, function(err, result) {
253253
func_called = true;
254254
cb(err, result);
@@ -265,4 +265,30 @@ describe("caching", function() {
265265
});
266266
});
267267
});
268+
269+
describe("instantiating with custom store", function() {
270+
it("allows us to pass in our own store object", function(done) {
271+
var store = memory_store.create({ttl: ttl});
272+
cache = caching({store: store});
273+
cache.set(key, value, function(err, result) {
274+
check_err(err);
275+
cache.get(key, function(err, result) {
276+
assert.equal(result, value);
277+
done();
278+
});
279+
});
280+
});
281+
282+
it("allows us to pass in a path to our own store", function(done) {
283+
var store_path = '../lib/stores/memory';
284+
cache = caching({store: store_path});
285+
cache.set(key, value, function(err, result) {
286+
check_err(err);
287+
cache.get(key, function(err, result) {
288+
assert.equal(result, value);
289+
done();
290+
});
291+
});
292+
});
293+
});
268294
});

Diff for: test/multi_caching.unit.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ describe("multi_caching", function() {
105105
});
106106
});
107107

108-
describe("run()", function() {
108+
describe("wrap()", function() {
109109
describe("using a single cache store", function() {
110110
beforeEach(function() {
111111
multi_cache = multi_caching([redis_cache]);
112112
});
113113

114114
it("calls back with the result of a function", function(done) {
115-
multi_cache.run(key, function(cb) {
115+
multi_cache.wrap(key, function(cb) {
116116
get_widget(name, cb);
117117
}, function(err, widget) {
118118
check_err(err);
@@ -128,7 +128,7 @@ describe("multi_caching", function() {
128128
});
129129

130130
it("calls back with the result of a function", function(done) {
131-
multi_cache.run(key, function(cb) {
131+
multi_cache.wrap(key, function(cb) {
132132
get_widget(name, cb);
133133
}, function(err, widget) {
134134
check_err(err);
@@ -138,7 +138,7 @@ describe("multi_caching", function() {
138138
});
139139

140140
it("sets value in all caches", function(done) {
141-
multi_cache.run(key, function(cb) {
141+
multi_cache.wrap(key, function(cb) {
142142
get_widget(name, cb);
143143
}, function(err, widget) {
144144
check_err(err);
@@ -160,7 +160,7 @@ describe("multi_caching", function() {
160160
context("when value exists in first store but not second", function() {
161161
it("returns value from first store, does not set it in second", function(done) {
162162
memory_cache.set(key, {name: name}, function(err) {
163-
multi_cache.run(key, function(cb) {
163+
multi_cache.wrap(key, function(cb) {
164164
get_widget(name, cb);
165165
}, function(err, widget) {
166166
check_err(err);
@@ -179,7 +179,7 @@ describe("multi_caching", function() {
179179
context("when value exists in second store but not first", function() {
180180
it("returns value from second store, sets it in first store", function(done) {
181181
redis_cache.set(key, {name: name}, function(err) {
182-
multi_cache.run(key, function(cb) {
182+
multi_cache.wrap(key, function(cb) {
183183
get_widget(name, cb);
184184
}, function(err, widget) {
185185
check_err(err);
@@ -202,7 +202,7 @@ describe("multi_caching", function() {
202202
});
203203

204204
it("calls back with the result of a function", function(done) {
205-
multi_cache.run(key, function(cb) {
205+
multi_cache.wrap(key, function(cb) {
206206
get_widget(name, cb);
207207
}, function(err, widget) {
208208
check_err(err);
@@ -212,7 +212,7 @@ describe("multi_caching", function() {
212212
});
213213

214214
it("sets value in all caches", function(done) {
215-
multi_cache.run(key, function(cb) {
215+
multi_cache.wrap(key, function(cb) {
216216
get_widget(name, cb);
217217
}, function(err, widget) {
218218
check_err(err);
@@ -239,7 +239,7 @@ describe("multi_caching", function() {
239239
context("when value exists in first store only", function() {
240240
it("returns value from first store, does not set it in second or third", function(done) {
241241
memory_cache.set(key, {name: name}, function(err) {
242-
multi_cache.run(key, function(cb) {
242+
multi_cache.wrap(key, function(cb) {
243243
get_widget(name, cb);
244244
}, function(err, widget) {
245245
check_err(err);
@@ -263,7 +263,7 @@ describe("multi_caching", function() {
263263
context("when value exists in second store only", function() {
264264
it("returns value from second store, sets it in first store, does not set third store", function(done) {
265265
redis_cache.set(key, {name: name}, function(err) {
266-
multi_cache.run(key, function(cb) {
266+
multi_cache.wrap(key, function(cb) {
267267
get_widget(name, cb);
268268
}, function(err, widget) {
269269
check_err(err);
@@ -287,7 +287,7 @@ describe("multi_caching", function() {
287287
context("when value exists in third store only", function() {
288288
it("returns value from third store, sets it in first and second stores", function(done) {
289289
memory_cache2.set(key, {name: name}, function(err) {
290-
multi_cache.run(key, function(cb) {
290+
multi_cache.wrap(key, function(cb) {
291291
get_widget(name, cb);
292292
}, function(err, widget) {
293293
check_err(err);

0 commit comments

Comments
 (0)