cache-manager 4.1.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,244 +0,0 @@
1
- /*eslint no-unused-vars:0*/
2
- var Lru = require("lru-cache");
3
- var cloneDeep = require('lodash.clonedeep');
4
- var utils = require('../utils');
5
- var isObject = utils.isObject;
6
-
7
- function clone(object) {
8
- if (typeof object === 'object' && object !== null) {
9
- return cloneDeep(object);
10
- }
11
- return object;
12
- }
13
-
14
- /**
15
- * Wrapper for lru-cache.
16
- * NOTE: If you want to use a memory store, you want to write your own to have full
17
- * control over its behavior. E.g., you might need the extra Promise overhead or
18
- * you may want to clone objects a certain way before storing.
19
- *
20
- * @param {object} args - Args passed to underlying lru-cache, plus additional optional args:
21
- * @param {boolean} [args.shouldCloneBeforeSet=true] - Whether to clone the data being stored.
22
- * Default: true
23
- * @param {boolean} [args.usePromises=true] - Whether to enable the use of Promises. Default: true
24
- */
25
- var memoryStore = function(args) {
26
- args = args || {};
27
- var self = {};
28
- self.name = 'memory';
29
- var Promise = args.promiseDependency || global.Promise;
30
- self.usePromises = !(typeof Promise === 'undefined' || args.noPromises);
31
- self.shouldCloneBeforeSet = args.shouldCloneBeforeSet !== false; // clone by default
32
-
33
- var lruOpts = {
34
- max: args.max || 500,
35
- maxSize: args.maxSize,
36
- ttl: (args.ttl || args.ttl === 0) ? args.ttl * 1000 : undefined,
37
- dispose: args.dispose,
38
- sizeCalculation: args.sizeCalculation || args.length,
39
- allowStale: args.allowStale || args.stale,
40
- updateAgeOnGet: args.updateAgeOnGet,
41
- updateAgeOnHas: args.updateAgeOnHas
42
- };
43
-
44
- var lruCache = new Lru(lruOpts);
45
-
46
- var setMultipleKeys = function setMultipleKeys(keysValues, ttl) {
47
- var length = keysValues.length;
48
- var values = [];
49
- for (var i = 0; i < length; i += 2) {
50
- lruCache.set(keysValues[i], keysValues[i + 1], {ttl});
51
- values.push(keysValues[i + 1]);
52
- }
53
- return values;
54
- };
55
-
56
- self.set = function(key, value, options, cb) {
57
- if (self.shouldCloneBeforeSet) {
58
- value = clone(value);
59
- }
60
-
61
- if (typeof options === 'function') {
62
- cb = options;
63
- options = {};
64
- }
65
- options = options || {};
66
-
67
- if (options && typeof options === 'number') {
68
- options = {ttl: options};
69
- }
70
-
71
- var ttl = (options.ttl || options.ttl === 0) ? options.ttl * 1000 : lruOpts.ttl;
72
-
73
- lruCache.set(key, value, {ttl});
74
- if (cb) {
75
- process.nextTick(cb.bind(null, null));
76
- } else if (self.usePromises) {
77
- return Promise.resolve(value);
78
- }
79
- };
80
-
81
- self.mset = function() {
82
- var args = Array.prototype.slice.apply(arguments);
83
- var cb;
84
- var options = {};
85
-
86
- if (typeof args[args.length - 1] === 'function') {
87
- cb = args.pop();
88
- }
89
-
90
- if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
91
- options = args.pop();
92
- }
93
-
94
- var ttl = (options.ttl || options.ttl === 0) ? options.ttl * 1000 : lruOpts.ttl;
95
-
96
- var values = setMultipleKeys(args, ttl);
97
-
98
- if (cb) {
99
- process.nextTick(cb.bind(null, null));
100
- } else if (self.usePromises) {
101
- return Promise.resolve(values);
102
- }
103
- };
104
-
105
- self.get = function(key, options, cb) {
106
- if (typeof options === 'function') {
107
- cb = options;
108
- }
109
- var value = lruCache.get(key);
110
-
111
- if (cb) {
112
- process.nextTick(cb.bind(null, null, value));
113
- } else if (self.usePromises) {
114
- return Promise.resolve(value);
115
- } else {
116
- return value;
117
- }
118
- };
119
-
120
- self.mget = function() {
121
- var args = Array.prototype.slice.apply(arguments);
122
- var cb;
123
- var options = {};
124
-
125
- if (typeof args[args.length - 1] === 'function') {
126
- cb = args.pop();
127
- }
128
-
129
- if (isObject(args[args.length - 1])) {
130
- options = args.pop();
131
- }
132
-
133
- var values = args.map(function(key) {
134
- return lruCache.get(key);
135
- });
136
-
137
- if (cb) {
138
- process.nextTick(cb.bind(null, null, values));
139
- } else if (self.usePromises) {
140
- return Promise.resolve(values);
141
- } else {
142
- return values;
143
- }
144
- };
145
-
146
- self.del = function() {
147
- var args = Array.prototype.slice.apply(arguments);
148
- var cb;
149
- var options = {};
150
-
151
- if (typeof args[args.length - 1] === 'function') {
152
- cb = args.pop();
153
- }
154
-
155
- if (isObject(args[args.length - 1])) {
156
- options = args.pop();
157
- }
158
-
159
- if (Array.isArray(args[0])) {
160
- args = args[0];
161
- }
162
-
163
- args.forEach(function(key) {
164
- lruCache.delete(key);
165
- });
166
-
167
- if (cb) {
168
- process.nextTick(cb.bind(null, null));
169
- } else if (self.usePromises) {
170
- return Promise.resolve();
171
- }
172
- };
173
-
174
- self.reset = function(cb) {
175
- lruCache.clear();
176
- if (cb) {
177
- process.nextTick(cb.bind(null, null));
178
- } else if (self.usePromises) {
179
- return Promise.resolve();
180
- }
181
- };
182
-
183
- self.keys = function(cb) {
184
- var keys = [...lruCache.keys()];
185
- if (cb) {
186
- process.nextTick(cb.bind(null, null, keys));
187
- } else if (self.usePromises) {
188
- return Promise.resolve(keys);
189
- } else {
190
- return keys;
191
- }
192
- };
193
-
194
- /**
195
- * This method is not available in the caching modules.
196
- */
197
- self.keyCount = function() {
198
- return lruCache.size;
199
- };
200
-
201
- /**
202
- * This method is not available in the caching modules.
203
- */
204
- self.dump = function() {
205
- var args = Array.prototype.slice.apply(arguments);
206
- var cb;
207
-
208
- if (typeof args[args.length - 1] === 'function') {
209
- cb = args.pop();
210
- }
211
- var value = lruCache.dump();
212
-
213
- if (cb) {
214
- process.nextTick(cb.bind(null, null, value));
215
- } else if (self.usePromises) {
216
- return Promise.resolve(value);
217
- } else {
218
- return value;
219
- }
220
- };
221
-
222
- /**
223
- * This method is not available in the caching modules.
224
- */
225
- self.load = function(cacheEntries, cb) {
226
- lruCache.load(cacheEntries);
227
-
228
- if (typeof cb === 'function') {
229
- process.nextTick(cb.bind(null, null));
230
- } else if (self.usePromises) {
231
- return Promise.resolve();
232
- }
233
- };
234
-
235
- return self;
236
- };
237
-
238
- var methods = {
239
- create: function(args) {
240
- return memoryStore(args);
241
- }
242
- };
243
-
244
- module.exports = methods;
@@ -1,143 +0,0 @@
1
- /*eslint no-unused-vars:0*/
2
- var utils = require('../utils');
3
- var isObject = utils.isObject;
4
-
5
- /**
6
- * Store that do nothing.
7
- * Can be used for development environment.
8
- */
9
- var noneStore = function(args) {
10
- args = args || {};
11
- var Promise = args.promiseDependency || global.Promise;
12
-
13
- var self = {};
14
- self.name = 'none';
15
- self.usePromises = !((typeof Promise === 'undefined' || args.noPromises));
16
-
17
- self.set = function(key, value, options, cb) {
18
- if (typeof options === 'function') {
19
- cb = options;
20
- options = {};
21
- }
22
-
23
- if (cb) {
24
- process.nextTick(cb.bind(null, null));
25
- } else if (self.usePromises) {
26
- return Promise.resolve(value);
27
- }
28
- };
29
-
30
- self.mset = function() {
31
- var args = Array.prototype.slice.apply(arguments);
32
- var cb;
33
- var options = {};
34
-
35
- if (typeof args[args.length - 1] === 'function') {
36
- cb = args.pop();
37
- }
38
-
39
- if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
40
- options = args.pop();
41
- }
42
-
43
- if (cb) {
44
- process.nextTick(cb.bind(null, null));
45
- } else if (self.usePromises) {
46
- var values = [];
47
- for (var i = 0; i < args.length; i += 2) {
48
- values.push(args[i + 1]);
49
- }
50
- return Promise.resolve(values);
51
- }
52
- };
53
-
54
- self.get = function(key, options, cb) {
55
- var value;
56
- if (typeof options === 'function') {
57
- cb = options;
58
- }
59
-
60
- if (cb) {
61
- process.nextTick(cb.bind(null, null, value));
62
- } else if (self.usePromises) {
63
- return Promise.resolve(value);
64
- } else {
65
- return value;
66
- }
67
- };
68
-
69
- self.mget = function() {
70
- var args = Array.prototype.slice.apply(arguments);
71
- var cb;
72
- var options = {};
73
-
74
- if (typeof args[args.length - 1] === 'function') {
75
- cb = args.pop();
76
- }
77
-
78
- if (isObject(args[args.length - 1])) {
79
- options = args.pop();
80
- }
81
-
82
- var values = args.map(function() {
83
- return;
84
- });
85
-
86
- if (cb) {
87
- process.nextTick(cb.bind(null, null, values));
88
- } else if (self.usePromises) {
89
- return Promise.resolve(values);
90
- } else {
91
- return values;
92
- }
93
- };
94
-
95
- self.del = function() {
96
- var args = Array.prototype.slice.apply(arguments);
97
- var cb;
98
- var options = {};
99
-
100
- if (typeof args[args.length - 1] === 'function') {
101
- cb = args.pop();
102
- }
103
-
104
- if (isObject(args[args.length - 1])) {
105
- options = args.pop();
106
- }
107
-
108
- if (cb) {
109
- process.nextTick(cb.bind(null, null));
110
- } else if (self.usePromises) {
111
- return Promise.resolve();
112
- }
113
- };
114
-
115
- self.reset = function(cb) {
116
- if (cb) {
117
- process.nextTick(cb.bind(null, null));
118
- } else if (self.usePromises) {
119
- return Promise.resolve();
120
- }
121
- };
122
-
123
- self.keys = function(cb) {
124
- var keys = [];
125
- if (cb) {
126
- process.nextTick(cb.bind(null, null, keys));
127
- } else if (self.usePromises) {
128
- return Promise.resolve(keys);
129
- } else {
130
- return keys;
131
- }
132
- };
133
-
134
- return self;
135
- };
136
-
137
- var methods = {
138
- create: function(args) {
139
- return noneStore(args);
140
- }
141
- };
142
-
143
- module.exports = methods;
package/lib/utils.js DELETED
@@ -1,42 +0,0 @@
1
- var isObject = function isObject(value) {
2
- return value instanceof Object && value.constructor === Object;
3
- };
4
-
5
- var parseWrapArguments = function parseWrapArguments(args) {
6
- var length = args.length;
7
- var work;
8
- var options = {};
9
- var cb;
10
-
11
- /**
12
- * As we can receive an unlimited number of keys
13
- * we find the index of the first function which is
14
- * the "work" handler to fetch the keys.
15
- */
16
- for (var i = 0; i < length; i += 1) {
17
- if (typeof args[i] === 'function') {
18
- if (typeof args[i + 2] === 'function') {
19
- cb = args.pop();
20
- } else if (typeof args[i + 1] === 'function') {
21
- cb = args.pop();
22
- }
23
- if (isObject(args[i + 1])) {
24
- options = args.pop();
25
- }
26
- work = args.pop();
27
- break;
28
- }
29
- }
30
-
31
- return {
32
- keys: args,
33
- work: work,
34
- options: options,
35
- cb: cb
36
- };
37
- };
38
-
39
- module.exports = {
40
- isObject: isObject,
41
- parseWrapArguments: parseWrapArguments
42
- };