cache-manager 4.1.0 → 5.0.1
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.
- package/README.md +25 -450
- package/dist/caching.d.ts +33 -0
- package/dist/caching.js +103 -0
- package/dist/caching.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/multi-caching.d.ts +6 -0
- package/dist/multi-caching.js +155 -0
- package/dist/multi-caching.js.map +1 -0
- package/dist/stores/index.d.ts +1 -0
- package/dist/stores/index.js +18 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/memory.d.ts +19 -0
- package/dist/stores/memory.js +232 -0
- package/dist/stores/memory.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +50 -21
- package/History.md +0 -262
- package/examples/example.js +0 -133
- package/examples/redis_example/example.js +0 -147
- package/examples/redis_example/redis_store.js +0 -124
- package/index.js +0 -1
- package/lib/caching.js +0 -384
- package/lib/callback_filler.js +0 -28
- package/lib/index.js +0 -7
- package/lib/multi_caching.js +0 -657
- package/lib/stores/memory.js +0 -244
- package/lib/stores/none.js +0 -143
- package/lib/utils.js +0 -42
package/History.md
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
- 4.1.0
|
|
2
|
-
- Support for TTL as third arg in memory store set function (#196). - @ZirionNeft
|
|
3
|
-
|
|
4
|
-
- 4.0.1
|
|
5
|
-
- Fix TTL option bug for memory cache (#194). - @anchan828
|
|
6
|
-
|
|
7
|
-
- 4.0.0
|
|
8
|
-
- Upgrade to lru-cache 7.x (#193). - @orgads
|
|
9
|
-
- This has a breaking change in memoryCache.dump().
|
|
10
|
-
|
|
11
|
-
- 3.6.3 2022-05-20
|
|
12
|
-
- Remove import from the code in order to be able to bundle and minify the module (#191) - @ricardomozartlino
|
|
13
|
-
|
|
14
|
-
- 3.6.2 2022-05-19
|
|
15
|
-
- Use lodash.cloneDeep npm instead of full lodash. (#190). - @ricardomozartlino
|
|
16
|
-
|
|
17
|
-
- 3.6.1 2022-04-07
|
|
18
|
-
- Fix for potential prototype polution exploit in async < 3.2.2 (#186). - @tmbobbins
|
|
19
|
-
|
|
20
|
-
- 3.6.0 2021-11-12
|
|
21
|
-
- Added keyCount() method to memory store (#173). - @css-master
|
|
22
|
-
|
|
23
|
-
- 3.5.0 2021-11-11
|
|
24
|
-
- Added dump() and load() methods to memory store (#171). - @jeoy
|
|
25
|
-
|
|
26
|
-
- 3.4.4 2021-06-11
|
|
27
|
-
- vulnerability fix in package-lock.json.
|
|
28
|
-
|
|
29
|
-
- 3.4.3 2021-03-30
|
|
30
|
-
- Bump y18n in package-lock.json (vulnerability fix).
|
|
31
|
-
|
|
32
|
-
- 3.4.2 2021-03-29
|
|
33
|
-
- Catch uncaught exception to pass it to callback (#154) - @lchenay
|
|
34
|
-
|
|
35
|
-
- 3.4.1 2021-03-05
|
|
36
|
-
- Update lodash to fix vulnerability (#152).
|
|
37
|
-
|
|
38
|
-
- 3.4.0 2020-04-30
|
|
39
|
-
- Update dependencies (#145). - @orgads
|
|
40
|
-
|
|
41
|
-
- 3.3.0 2020-04-30
|
|
42
|
-
- Use full lodash lib instead of lodash.clonedeep npm (#141)
|
|
43
|
-
|
|
44
|
-
- 3.2.1 2020-03-20
|
|
45
|
-
- Background refresh fixes (#139). -@marcoreni
|
|
46
|
-
|
|
47
|
-
- 3.2.0 2020-03-13
|
|
48
|
-
- Background refresh of expiring entries (#138). - @marcoreni
|
|
49
|
-
|
|
50
|
-
- 3.1.0 2020-02-24
|
|
51
|
-
- Pass updateAgeOnGet to lru-cache in memory-store (#136). - @BastianKubaile
|
|
52
|
-
|
|
53
|
-
- 3.0.0 2020-02-21
|
|
54
|
-
- Added way to disable cloning of values in memory store (#135).
|
|
55
|
-
|
|
56
|
-
- 2.11.1 2020-02-03
|
|
57
|
-
- Bugfix: Preserve function prototypes in memory store (#133). - @ryanbecker
|
|
58
|
-
|
|
59
|
-
- 2.11.0 2020-01-31
|
|
60
|
-
- Use eslint instead of jshint/jscs; use nyc instead of istanbul; mocha/coveralls upgrades.
|
|
61
|
-
|
|
62
|
-
- 2.10.3 2020-01-31
|
|
63
|
-
- Preserve functions in memory store when cloning objects (#132).
|
|
64
|
-
|
|
65
|
-
- 2.10.2 2020-01-30
|
|
66
|
-
- Disallow mutation of values in memory store (#103).
|
|
67
|
-
|
|
68
|
-
- 2.10.1 2019-11-06
|
|
69
|
-
- Add .js to module path to fix compilers (#131). - @imjohnbo
|
|
70
|
-
|
|
71
|
-
- 2.10.0 2019-07-11
|
|
72
|
-
- Add development store "none" (#129). -@R3VoLuT1OneR
|
|
73
|
-
|
|
74
|
-
- 2.9.1 2019-05-28
|
|
75
|
-
- Bind isCacheableValue to its underlying store (#126). - @arash16
|
|
76
|
-
|
|
77
|
-
- 2.9.0 2018-03-26
|
|
78
|
-
- Fix store's this context for isCacheableValue method (#119). - @alexandrusavin
|
|
79
|
-
|
|
80
|
-
- 2.8.0 2018-02-28
|
|
81
|
-
- Added link to node-cache-manager-fs-hash - @rolandstarke
|
|
82
|
-
- Bugfix for options object in multiCache.set (#115) - @sebelga
|
|
83
|
-
- Bugfix for keysToFetch in getFromHighestProrityCache (#117) - @sebelga
|
|
84
|
-
|
|
85
|
-
- 2.7.0 2018-02-13
|
|
86
|
-
- allow setting, getting and deleting multiple keys (#114) - @sebelga
|
|
87
|
-
- allow passing in a function to determine TTL based on store - @sebelga
|
|
88
|
-
|
|
89
|
-
- 2.6.0 2017-12-08
|
|
90
|
-
- fix multicaching when result is not cacheable (#106) - @gswalden
|
|
91
|
-
|
|
92
|
-
- 2.5.0 2017-10-09
|
|
93
|
-
- Add explicit return in wrapPromise (#109) - @jeff-kilbride
|
|
94
|
-
|
|
95
|
-
- 2.4.0 2017-01-17
|
|
96
|
-
- Added ability to use a dynamic cache ttl (#65) - @philippeauriach
|
|
97
|
-
|
|
98
|
-
- 2.3.0 2016-12-22
|
|
99
|
-
- Updating isCacheableValue description in README; README syntax error fix (#70, #71) - @lukechilds
|
|
100
|
-
- Calling back with null as first argument in memory store to keep style consistent (#72) - @defcc
|
|
101
|
-
|
|
102
|
-
- 2.2.0 2016-10-19
|
|
103
|
-
- Adding multi_caching.reset() (#63) - @disjunction
|
|
104
|
-
|
|
105
|
-
- 2.1.2 2016-06-08
|
|
106
|
-
- Checking that callback array exists before iterating over it (#57).
|
|
107
|
-
|
|
108
|
-
- 2.1.1 2016-05-24
|
|
109
|
-
- Fixing version number in package.json.
|
|
110
|
-
|
|
111
|
-
- 2.1.0 2016-05-24
|
|
112
|
-
- Allow passing in a promise dependency (#55). - @siddharthkchatterjee
|
|
113
|
-
|
|
114
|
-
- 2.0.1 2016-04-18
|
|
115
|
-
- Fixed triggering callback in wrap after it was queued (#48) - @theorm
|
|
116
|
-
|
|
117
|
-
- 2.0.0 2016-03-13
|
|
118
|
-
- Removing domain integration (#38), no longer actively supporting node 0.10.x.
|
|
119
|
-
|
|
120
|
-
- 1.5.0 2016-03-13
|
|
121
|
-
- npm bumps, making sure global.Promise is not defined in node 0.10 memory store test.
|
|
122
|
-
|
|
123
|
-
- 1.4.1 2016-03-13
|
|
124
|
-
- Fixing backward-compatibility Promise issue with node 0.10.x in memory store.
|
|
125
|
-
|
|
126
|
-
- 1.4.0 2016-02-03
|
|
127
|
-
- Passing ttl of 0 to lruCache, upgrading to lru-cache 4.0.0
|
|
128
|
-
|
|
129
|
-
- 1.3.0 2016-01-26
|
|
130
|
-
- Promise support (#39, #24) - @PuKoren
|
|
131
|
-
|
|
132
|
-
- 1.2.2 2015-10-19
|
|
133
|
-
- Bugfix: Fixing domain error issues when error is thrown inside 'work' function (#28).
|
|
134
|
-
|
|
135
|
-
- 1.2.1 2015-10-17
|
|
136
|
-
- Bugfix: multi-caching: using underlying store's isCacheableValue function when it exists (#34).
|
|
137
|
-
|
|
138
|
-
- 1.2.0 2015-10-07
|
|
139
|
-
- using `isCacheableValue` in `getFromHighestPriorityCache` and `getAndPassUp` (#32).
|
|
140
|
-
|
|
141
|
-
- 1.1.0 2015-07-22
|
|
142
|
-
- Allow stores to override isCacheableValue. - @PuKoren
|
|
143
|
-
- Allow overriding ttl in memory cache's set function. - @zhudan
|
|
144
|
-
|
|
145
|
-
- 1.0.0 2015-05-23
|
|
146
|
-
- Added JSDOC generation (`make docs`)
|
|
147
|
-
- (Breaking change) By default, cache falsey values like `false`, `0`, and `null`, but not `undefined` (#25).
|
|
148
|
-
- Allow users to pass in callback function `isCacheableValue` to specify what to cache.
|
|
149
|
-
- (Breaking change) Removed deprecated lower-case `multi_caching` export (use `multiCaching` instead).
|
|
150
|
-
- (Breaking change) Removed `multiCaching#get_and_pass_up` (use `getAndPassUp` instead).
|
|
151
|
-
- (Breaking change) Cache store methods must accept an `options` param (which can be ignored). Eg.,
|
|
152
|
-
`function set(key, val, options, cb) { }`
|
|
153
|
-
- (Breaking change) caching/multicaching methods no longer accept a `ttl` param. You must instead pass
|
|
154
|
-
in an options object which will be passed to the cache store's `set` method.
|
|
155
|
-
- (Breaking change) caching.js no longer accepts a path to cache store. Pass in an object or 'memory' instead.
|
|
156
|
-
|
|
157
|
-
- 0.19.0 2015-03-29
|
|
158
|
-
- Pass dispose, length & stale options to lru-cache (#22). - @gmaclennan
|
|
159
|
-
|
|
160
|
-
- 0.18.0 2015-02-12
|
|
161
|
-
- Minor changes and refactorings including:
|
|
162
|
-
- converting to camelcase
|
|
163
|
-
- hiding queues inside CallbackFiller
|
|
164
|
-
- general example updates
|
|
165
|
-
- updated redis example to use latest redis npm
|
|
166
|
-
- not trying to pass ttl into cache.set() in getAndPassUp() (this didn't
|
|
167
|
-
work anyway)
|
|
168
|
-
|
|
169
|
-
- 0.17.0 2015-02-05
|
|
170
|
-
- Add Additional Options Parameter (#20) - @seanzx85
|
|
171
|
-
- Fixing bug with nested calls to wrap() (#21)
|
|
172
|
-
|
|
173
|
-
- 0.16.0 2015-01-07
|
|
174
|
-
- Get and pass up feature to update higher caches. (#19) - raadad
|
|
175
|
-
- Minor style tweaks/jscs update.
|
|
176
|
-
|
|
177
|
-
- 0.15.0 2014-12-18
|
|
178
|
-
- Moved cache queue before the store get function (up to 2x performance boost). (#18) - aletorrado
|
|
179
|
-
- Added domain support to make sure the wrap callback function is always called - aletorrado
|
|
180
|
-
|
|
181
|
-
- 0.14.0 2014-10-15
|
|
182
|
-
- Set ttl in wrap #14 - nguyenchr
|
|
183
|
-
- Added JSCS for style checking
|
|
184
|
-
|
|
185
|
-
- 0.13.0 2014-10-14
|
|
186
|
-
- Applied work function locking for multi_caching (#13). -aletorrado
|
|
187
|
-
|
|
188
|
-
- 0.12.0 2014-10-09
|
|
189
|
-
- Checking for existence of del() method before binding to it. Fixes #11.
|
|
190
|
-
|
|
191
|
-
- 0.11.0 2014-09-18
|
|
192
|
-
- Prevent stalemate by executing callbacks on error. Fixes #10 - elliotttf
|
|
193
|
-
|
|
194
|
-
- 0.10.1 2014-09-10
|
|
195
|
-
- Fixed tag/version mismatch
|
|
196
|
-
|
|
197
|
-
- 0.10.0 2014-09-10
|
|
198
|
-
- Fixing Use call instead of apply for cached results, issue #9 (thanks elliotttf)
|
|
199
|
-
|
|
200
|
-
- 0.9.0 2014-08-19
|
|
201
|
-
- Fixing issue #8 - parallel requests to a wrapped function were calling the
|
|
202
|
-
function multiple times. (Thanks alex-whitney).
|
|
203
|
-
|
|
204
|
-
- 0.8.0 2014-07-07
|
|
205
|
-
- Adding setex() (Thanks evanlucas)
|
|
206
|
-
|
|
207
|
-
- 0.7.1 2014-06-15
|
|
208
|
-
- Adding link to Express.js cache-manager example app
|
|
209
|
-
|
|
210
|
-
- 0.7.0 2014-06-15
|
|
211
|
-
- Bumping package versions, mostly devDependencies
|
|
212
|
-
|
|
213
|
-
- 0.6.0 2014-06-15
|
|
214
|
-
- Adding caching.keys() function (issue #6)
|
|
215
|
-
- Updating examples/redis_example/example.js with cache.keys() usage
|
|
216
|
-
- Allow calling memory store get() without callback
|
|
217
|
-
|
|
218
|
-
- 0.5.0 2014-05-02
|
|
219
|
-
- Adding reset() function to caching.js. Closes #5.
|
|
220
|
-
|
|
221
|
-
- 0.4.0 2014-05-02
|
|
222
|
-
- New arg to ignore cache errors. if set cache errors will be ignored
|
|
223
|
-
and the cache_manager will go to the backing store. (Thanks londonjamo).
|
|
224
|
-
|
|
225
|
-
- 0.3.0 2013-12-08
|
|
226
|
-
- Bound the get, set and del functions to their original “this” context when assigning a store.
|
|
227
|
-
(Thanks to Boyan Rabchev)
|
|
228
|
-
|
|
229
|
-
- 0.2.0 2013-10-31
|
|
230
|
-
- Better examples, version bump.
|
|
231
|
-
|
|
232
|
-
- 0.1.3 2013-10-31
|
|
233
|
-
- Fixing unreleased connection in redis example.
|
|
234
|
-
|
|
235
|
-
- 0.1.2 2013-10-13
|
|
236
|
-
- Wrapping synchronous memory cache callbacks in process.nextTick() for the purists.
|
|
237
|
-
|
|
238
|
-
- 0.1.1 2013-10-13
|
|
239
|
-
- Travis and Coveralls integration testing.
|
|
240
|
-
|
|
241
|
-
- 0.1.0 2013-10-13
|
|
242
|
-
- Removing built-in Redis store to emphasize that you should plug in your own
|
|
243
|
-
cache store.
|
|
244
|
-
|
|
245
|
-
- 0.0.5 2013-10-13
|
|
246
|
-
- Removing hiredis requirement.
|
|
247
|
-
|
|
248
|
-
- 0.0.4 2013-08-01
|
|
249
|
-
- Better error checking in multi_cache.wrap();
|
|
250
|
-
|
|
251
|
-
- 0.0.3 2013-07-10
|
|
252
|
-
- Better error checking in cache.wrap();
|
|
253
|
-
|
|
254
|
-
- 0.0.2 2013-04-08
|
|
255
|
-
- Added ability to pass in a store module that isn't already instantiated. E.g.,
|
|
256
|
-
|
|
257
|
-
```javascript
|
|
258
|
-
var store = require('/path/to/my_memory_store');
|
|
259
|
-
cache = caching({store: store});
|
|
260
|
-
```
|
|
261
|
-
- 0.0.1 2013-04-08
|
|
262
|
-
- Initial release.
|
package/examples/example.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/*jshint unused:false*/
|
|
2
|
-
// Note: ttls are in seconds
|
|
3
|
-
var cacheManager = require('../');
|
|
4
|
-
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10});
|
|
5
|
-
var memoryCache2 = cacheManager.caching({store: 'memory', max: 100, ttl: 100});
|
|
6
|
-
|
|
7
|
-
//
|
|
8
|
-
// Basic usage
|
|
9
|
-
//
|
|
10
|
-
memoryCache.set('foo', 'bar', function(err) {
|
|
11
|
-
if (err) { throw err; }
|
|
12
|
-
|
|
13
|
-
memoryCache.get('foo', function(err, result) {
|
|
14
|
-
console.log(result);
|
|
15
|
-
// >> 'bar'
|
|
16
|
-
memoryCache.del('foo', function(err) {
|
|
17
|
-
if (err) {
|
|
18
|
-
console.log(err);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
function getUser(id, cb) {
|
|
25
|
-
setTimeout(function() {
|
|
26
|
-
console.log("Fetching user from slow database.");
|
|
27
|
-
cb(null, {id: id, name: 'Bob'});
|
|
28
|
-
}, 100);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
var userId = 123;
|
|
32
|
-
var key = 'user_' + userId;
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
// wrap() example
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
Instead of manually managing the cache like this:
|
|
40
|
-
function getCachedUserManually(id, cb) {
|
|
41
|
-
memoryCache.get(id, function(err, result) {
|
|
42
|
-
if (err) { return cb(err); }
|
|
43
|
-
|
|
44
|
-
if (result) {
|
|
45
|
-
return cb(null, result);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
getUser(id, function(err, result) {
|
|
49
|
-
if (err) { return cb(err); }
|
|
50
|
-
memoryCache.set(id, result);
|
|
51
|
-
cb(null, result);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
*/
|
|
56
|
-
|
|
57
|
-
// ... you can instead use the `wrap` function:
|
|
58
|
-
function getCachedUser(id, cb) {
|
|
59
|
-
memoryCache.wrap(id, function(cacheCallback) {
|
|
60
|
-
getUser(id, cacheCallback);
|
|
61
|
-
}, cb);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
getCachedUser(userId, function(err, user) {
|
|
65
|
-
// First time fetches the user from the (fake) database:
|
|
66
|
-
console.log(user);
|
|
67
|
-
|
|
68
|
-
getCachedUser(userId, function(err, user) {
|
|
69
|
-
// Second time fetches from cache.
|
|
70
|
-
console.log(user);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Outputs:
|
|
75
|
-
// Returning user from slow database.
|
|
76
|
-
// { id: 123, name: 'Bob' }
|
|
77
|
-
// { id: 123, name: 'Bob' }
|
|
78
|
-
|
|
79
|
-
// Same as above, but written differently:
|
|
80
|
-
memoryCache.wrap(key, function(cb) {
|
|
81
|
-
getUser(userId, cb);
|
|
82
|
-
}, function(err, user) {
|
|
83
|
-
console.log(user);
|
|
84
|
-
|
|
85
|
-
// Second time fetches user from memoryCache
|
|
86
|
-
memoryCache.wrap(key, function(cb) {
|
|
87
|
-
getUser(userId, cb);
|
|
88
|
-
}, function(err, user) {
|
|
89
|
-
console.log(user);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
// multi-cache example
|
|
95
|
-
//
|
|
96
|
-
var multiCache = cacheManager.multiCaching([memoryCache, memoryCache2]);
|
|
97
|
-
var userId2 = 456;
|
|
98
|
-
var key2 = 'user_' + userId;
|
|
99
|
-
var ttl2; //Can't use a different ttl per set() call with memory cache
|
|
100
|
-
|
|
101
|
-
multiCache.wrap(key2, function(cb) {
|
|
102
|
-
getUser(userId2, cb);
|
|
103
|
-
}, function(err, user) {
|
|
104
|
-
console.log(user);
|
|
105
|
-
|
|
106
|
-
// Second time fetches user from memoryCache, since it's highest priority.
|
|
107
|
-
// If the data expires in memoryCache, the next fetch would pull it from
|
|
108
|
-
// the memoryCache2, and set the data in memoryCache.
|
|
109
|
-
multiCache.wrap(key2, function(cb) {
|
|
110
|
-
getUser(userId2, cb);
|
|
111
|
-
}, function(err, user) {
|
|
112
|
-
console.log(user);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Sets in all caches.
|
|
116
|
-
multiCache.set('foo2', 'bar2', {ttl: ttl2}, function(err) {
|
|
117
|
-
if (err) { throw err; }
|
|
118
|
-
|
|
119
|
-
// Fetches from highest priority cache that has the key.
|
|
120
|
-
multiCache.get('foo2', function(err, result) {
|
|
121
|
-
console.log(result);
|
|
122
|
-
// >> 'bar2'
|
|
123
|
-
|
|
124
|
-
// Delete from all caches
|
|
125
|
-
multiCache.del('foo2', function(err) {
|
|
126
|
-
if (err) {
|
|
127
|
-
console.log(err);
|
|
128
|
-
}
|
|
129
|
-
process.exit();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
});
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
// Setup:
|
|
2
|
-
// npm install redis@0.12.1 sol-redis-pool@0.2.0
|
|
3
|
-
// node examples/redis_example/example.js
|
|
4
|
-
|
|
5
|
-
var util = require('util');
|
|
6
|
-
var assert = require('assert');
|
|
7
|
-
var cacheManager = require('../../');
|
|
8
|
-
var redisStore = require('./redis_store');
|
|
9
|
-
// Note: ttl is in seconds
|
|
10
|
-
var redisCache = cacheManager.caching({store: redisStore, db: 0, ttl: 100});
|
|
11
|
-
var ttl = 60;
|
|
12
|
-
|
|
13
|
-
console.log("set/get/del example:");
|
|
14
|
-
|
|
15
|
-
redisCache.set('foo', 'bar', {ttl: ttl}, function(err) {
|
|
16
|
-
if (err) { throw err; }
|
|
17
|
-
|
|
18
|
-
redisCache.get('foo', function(err, result) {
|
|
19
|
-
if (err) { throw err; }
|
|
20
|
-
console.log("result fetched from cache: " + result);
|
|
21
|
-
// >> 'bar'
|
|
22
|
-
|
|
23
|
-
redisCache.ttl('foo', function(err, result) {
|
|
24
|
-
if (err) { throw err; }
|
|
25
|
-
assert.ok(result > 59 && result < 61);
|
|
26
|
-
|
|
27
|
-
redisCache.del('foo', function(err) {
|
|
28
|
-
if (err) { throw err; }
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// TTL defaults to what we passed into the caching function (100)
|
|
35
|
-
redisCache.set('foo-no-ttl', 'bar-no-ttl', function(err) {
|
|
36
|
-
if (err) { throw err; }
|
|
37
|
-
|
|
38
|
-
redisCache.get('foo-no-ttl', function(err, result) {
|
|
39
|
-
if (err) { throw err; }
|
|
40
|
-
console.log("result fetched from cache: " + result);
|
|
41
|
-
// >> 'bar'
|
|
42
|
-
|
|
43
|
-
redisCache.ttl('foo-no-ttl', function(err, result) {
|
|
44
|
-
if (err) { throw err; }
|
|
45
|
-
assert.ok(result > 99 && result < 101);
|
|
46
|
-
|
|
47
|
-
redisCache.del('foo-no-ttl', function(err) {
|
|
48
|
-
if (err) { throw err; }
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Calls Redis 'set' instead of 'setex'
|
|
55
|
-
redisCache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) {
|
|
56
|
-
if (err) { throw err; }
|
|
57
|
-
|
|
58
|
-
redisCache.get('foo-zero-ttl', function(err, result) {
|
|
59
|
-
if (err) { throw err; }
|
|
60
|
-
console.log("result fetched from cache: " + result);
|
|
61
|
-
// >> 'bar'
|
|
62
|
-
|
|
63
|
-
redisCache.ttl('foo-zero-ttl', function(err, result) {
|
|
64
|
-
if (err) { throw err; }
|
|
65
|
-
assert.ok(result < 0);
|
|
66
|
-
|
|
67
|
-
redisCache.del('foo-zero-ttl', function(err) {
|
|
68
|
-
if (err) { throw err; }
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
var userId = 123;
|
|
75
|
-
|
|
76
|
-
function createKey(id) {
|
|
77
|
-
return 'user_' + id;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function getUser(id, cb) {
|
|
81
|
-
setTimeout(function() {
|
|
82
|
-
console.log("\n\nReturning user from slow database.");
|
|
83
|
-
cb(null, {id: id, name: 'Bob'});
|
|
84
|
-
}, 100);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function getUserFromCache(id, cb) {
|
|
88
|
-
var key = createKey(id);
|
|
89
|
-
redisCache.wrap(key, function(cacheCb) {
|
|
90
|
-
getUser(userId, cacheCb);
|
|
91
|
-
}, {ttl: ttl}, cb);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
getUserFromCache(userId, function(err, user) {
|
|
95
|
-
console.log(user);
|
|
96
|
-
|
|
97
|
-
// Second time fetches user from redisCache
|
|
98
|
-
getUserFromCache(userId, function(err, user) {
|
|
99
|
-
console.log("user from second cache request:");
|
|
100
|
-
console.log(user);
|
|
101
|
-
|
|
102
|
-
redisCache.keys(function(err, keys) {
|
|
103
|
-
console.log("keys: " + util.inspect(keys));
|
|
104
|
-
|
|
105
|
-
var key = createKey(userId);
|
|
106
|
-
redisCache.del(key, function(err) {
|
|
107
|
-
if (err) { throw err; }
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// Outputs:
|
|
114
|
-
// { id: 123, name: 'Bob' }
|
|
115
|
-
// user from second cache request:
|
|
116
|
-
// { id: 123, name: 'Bob' }
|
|
117
|
-
// keys: [ 'user_123' ]
|
|
118
|
-
|
|
119
|
-
var redisCache2 = cacheManager.caching({store: redisStore, db: 1, ttl: 100});
|
|
120
|
-
var multiCache = cacheManager.multiCaching([redisCache, redisCache2]);
|
|
121
|
-
var userId2 = 456;
|
|
122
|
-
var key2 = 'user_' + userId;
|
|
123
|
-
var ttl2 = 50;
|
|
124
|
-
|
|
125
|
-
multiCache.wrap(key2, function(cb) {
|
|
126
|
-
getUser(userId2, cb);
|
|
127
|
-
}, {ttl: ttl2}, function(err, user) {
|
|
128
|
-
console.log("user: ", user);
|
|
129
|
-
|
|
130
|
-
// Second time fetches user from redisCache, since it's highest priority.
|
|
131
|
-
// If the data expires in the redisCache, the next fetch would pull it from
|
|
132
|
-
// redisCache2, and set the data in redisCache again.
|
|
133
|
-
multiCache.wrap(key2, function(cb) {
|
|
134
|
-
getUser(userId2, cb);
|
|
135
|
-
}, function(err, user) {
|
|
136
|
-
console.log("user, second fetch:", user);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
multiCache.getAndPassUp(key2, function(err, result) {
|
|
140
|
-
console.log("\ngetAndPassUp result: ", result);
|
|
141
|
-
|
|
142
|
-
multiCache.del(key2, function(err) {
|
|
143
|
-
if (err) { throw err; }
|
|
144
|
-
process.exit();
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
});
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is a very basic example of how you can implement your own Redis-based
|
|
3
|
-
* cache store with connection pooling.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
var RedisPool = require('sol-redis-pool');
|
|
7
|
-
|
|
8
|
-
function redisStore(args) {
|
|
9
|
-
args = args || {};
|
|
10
|
-
var self = {};
|
|
11
|
-
var ttlDefault = args.ttl;
|
|
12
|
-
self.name = 'redis';
|
|
13
|
-
|
|
14
|
-
var redisOptions = {
|
|
15
|
-
host: args.host || '127.0.0.1',
|
|
16
|
-
port: args.port || 6379
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
var pool = new RedisPool(redisOptions, {});
|
|
20
|
-
|
|
21
|
-
function connect(cb) {
|
|
22
|
-
pool.acquire(function(err, conn) {
|
|
23
|
-
if (err) {
|
|
24
|
-
pool.release(conn);
|
|
25
|
-
return cb(err);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (args.db || args.db === 0) {
|
|
29
|
-
conn.select(args.db);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
cb(null, conn);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function handleResponse(conn, cb, opts) {
|
|
37
|
-
opts = opts || {};
|
|
38
|
-
|
|
39
|
-
return function(err, result) {
|
|
40
|
-
pool.release(conn);
|
|
41
|
-
|
|
42
|
-
if (err) { return cb(err); }
|
|
43
|
-
|
|
44
|
-
if (opts.parse) {
|
|
45
|
-
result = JSON.parse(result);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
cb(null, result);
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
self.get = function(key, options, cb) {
|
|
53
|
-
if (typeof options === 'function') {
|
|
54
|
-
cb = options;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
connect(function(err, conn) {
|
|
58
|
-
if (err) { return cb(err); }
|
|
59
|
-
conn.get(key, handleResponse(conn, cb, {parse: true}));
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
self.set = function(key, value, options, cb) {
|
|
64
|
-
if (typeof options === 'function') {
|
|
65
|
-
cb = options;
|
|
66
|
-
options = {};
|
|
67
|
-
}
|
|
68
|
-
options = options || {};
|
|
69
|
-
|
|
70
|
-
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : ttlDefault;
|
|
71
|
-
|
|
72
|
-
connect(function(err, conn) {
|
|
73
|
-
if (err) { return cb(err); }
|
|
74
|
-
var val = JSON.stringify(value);
|
|
75
|
-
|
|
76
|
-
if (ttl) {
|
|
77
|
-
conn.setex(key, ttl, val, handleResponse(conn, cb));
|
|
78
|
-
} else {
|
|
79
|
-
conn.set(key, val, handleResponse(conn, cb));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
self.del = function(key, options, cb) {
|
|
85
|
-
if (typeof options === 'function') {
|
|
86
|
-
cb = options;
|
|
87
|
-
}
|
|
88
|
-
connect(function(err, conn) {
|
|
89
|
-
if (err) { return cb(err); }
|
|
90
|
-
conn.del(key, handleResponse(conn, cb));
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
self.ttl = function(key, cb) {
|
|
95
|
-
connect(function(err, conn) {
|
|
96
|
-
if (err) { return cb(err); }
|
|
97
|
-
conn.ttl(key, handleResponse(conn, cb));
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
self.keys = function(pattern, cb) {
|
|
102
|
-
if (typeof pattern === 'function') {
|
|
103
|
-
cb = pattern;
|
|
104
|
-
pattern = '*';
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
connect(function(err, conn) {
|
|
108
|
-
if (err) { return cb(err); }
|
|
109
|
-
conn.keys(pattern, handleResponse(conn, cb));
|
|
110
|
-
});
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
self.isCacheableValue = function(value) {
|
|
114
|
-
return value !== null && value !== undefined;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
return self;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
module.exports = {
|
|
121
|
-
create: function(args) {
|
|
122
|
-
return redisStore(args);
|
|
123
|
-
}
|
|
124
|
-
};
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./lib');
|