|
1 | 1 | var caching = require("../../index");
|
2 | 2 | var assert = require("assert");
|
3 |
| -var support = require('../support'); |
| 3 | +var support = require("../support"); |
| 4 | +var check_err = support.check_err; |
4 | 5 | var memoryFlag = "";
|
5 | 6 | var key;
|
6 | 7 | var value;
|
7 | 8 | var testStore = function(args) {
|
8 | 9 | args = args || {};
|
9 | 10 | var self = {};
|
10 | 11 | self.name = "options";
|
| 12 | + self.store = {}; |
11 | 13 |
|
12 | 14 | self.get = function(key, options, cb) {
|
| 15 | + var optionsMapped = false; |
| 16 | + if (typeof options === "function") { |
| 17 | + cb = options; |
| 18 | + options = false; |
| 19 | + optionsMapped = true; |
| 20 | + } |
13 | 21 | if (options && options.value) {
|
14 | 22 | return cb(null, options.value + "ValueOption");
|
15 | 23 | } else if (options && options.fn) {
|
16 | 24 | options.fn("GetFunctionOption");
|
17 | 25 | return cb(null, "GetFunctionOption");
|
| 26 | + } else if (options && options.runNormal) { |
| 27 | + return cb(null, self.store[key]); |
| 28 | + } else if (optionsMapped) { |
| 29 | + return cb(); |
18 | 30 | }
|
19 | 31 | return cb("Error No Options");
|
20 | 32 | };
|
21 | 33 |
|
22 | 34 | self.set = function(key, value, options, cb) {
|
| 35 | + var optionsMapped = false; |
| 36 | + if (typeof options === "function") { |
| 37 | + cb = options; |
| 38 | + options = false; |
| 39 | + optionsMapped = true; |
| 40 | + } else if (typeof options !== 'object') { |
| 41 | + options = {ttl: options, runNormal: true}; |
| 42 | + } |
23 | 43 | if (options && options.value) {
|
24 | 44 | memoryFlag = options.value + "ValueOption";
|
25 | 45 | return cb();
|
26 | 46 | } else if (options && options.fn) {
|
27 | 47 | options.fn("SetFunctionOption");
|
28 | 48 | return cb();
|
| 49 | + } else if (options && options.runNormal) { |
| 50 | + self.store[key] = value; |
| 51 | + return cb(null, self.store[key]); |
| 52 | + } else if (optionsMapped) { |
| 53 | + return cb(); |
29 | 54 | }
|
30 | 55 | return cb("Error No Options");
|
31 | 56 | };
|
32 | 57 |
|
33 | 58 | self.del = function(key, options, cb) {
|
| 59 | + var optionsMapped = false; |
| 60 | + if (typeof options === "function") { |
| 61 | + cb = options; |
| 62 | + options = false; |
| 63 | + optionsMapped = true; |
| 64 | + } |
34 | 65 | if (options && options.value) {
|
35 | 66 | memoryFlag = options.value + "ValueOption";
|
36 | 67 | return cb();
|
37 | 68 | } else if (options && options.fn) {
|
38 | 69 | options.fn("DeleteFunctionOption");
|
39 | 70 | return cb();
|
| 71 | + } else if (options && options.runNormal) { |
| 72 | + delete self.store[key]; |
| 73 | + return cb(null, ""); |
| 74 | + } else if (optionsMapped) { |
| 75 | + return cb(); |
40 | 76 | }
|
41 | 77 | return cb("Error No Options");
|
42 | 78 | };
|
@@ -133,3 +169,48 @@ describe("Methods with options", function() {
|
133 | 169 | });
|
134 | 170 | });
|
135 | 171 | });
|
| 172 | +describe("Multiple stores with options", function() { |
| 173 | + var testInstance = caching.caching({store: testStore()}); |
| 174 | + var memInstance = caching.caching({store: "memory"}); |
| 175 | + var testCache; |
| 176 | + var options = {runNormal: true}; |
| 177 | + var ttl = 1; |
| 178 | + before(function() { |
| 179 | + key = support.random.string(20); |
| 180 | + value = support.random.string(20); |
| 181 | + testCache = caching.multi_caching([testInstance, memInstance]); |
| 182 | + }); |
| 183 | + |
| 184 | + it("lets us pass options which only one store uses", function() { |
| 185 | + testCache.set(key, value, options, function(err) { |
| 186 | + check_err(err); |
| 187 | + testCache.get(key, options, function(err, response) { |
| 188 | + check_err(err); |
| 189 | + assert.equal(response, value); |
| 190 | + testCache.del(key, options, function(err) { |
| 191 | + check_err(err); |
| 192 | + testCache.get(key, options, function(err, response) { |
| 193 | + check_err(err); |
| 194 | + assert.equal(response, undefined); |
| 195 | + }); |
| 196 | + }); |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | + it("lets us not pass options which only one store uses", function() { |
| 201 | + testCache.set(key, value, ttl, function(err) { |
| 202 | + check_err(err); |
| 203 | + testCache.get(key, function(err, response) { |
| 204 | + check_err(err); |
| 205 | + assert.equal(response, value); |
| 206 | + testCache.del(key, function(err) { |
| 207 | + check_err(err); |
| 208 | + testCache.get(key, function(err, response) { |
| 209 | + check_err(err); |
| 210 | + assert.equal(response, undefined); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments