@@ -35,17 +35,17 @@ First, it includes a `wrap` function that lets you wrap any function in cache.
35
35
This is probably the feature you're looking for. As an example, where you might have to do this:
36
36
37
37
``` javascript
38
- function get_cached_user (id , cb ) {
39
- memory_cache .get (id, function (err , result ) {
38
+ function getCachedUser (id , cb ) {
39
+ memoryCache .get (id, function (err , result ) {
40
40
if (err) { return cb (err); }
41
41
42
42
if (result) {
43
43
return cb (null , result);
44
44
}
45
45
46
- get_user (id, function (err , result ) {
46
+ getUser (id, function (err , result ) {
47
47
if (err) { return cb (err); }
48
- memory_cache .set (id, result);
48
+ memoryCache .set (id, result);
49
49
cb (null , result);
50
50
});
51
51
});
@@ -54,9 +54,9 @@ function get_cached_user(id, cb) {
54
54
... you can instead use the ` wrap ` function:
55
55
56
56
``` javascript
57
- function get_cached_user (id , cb ) {
58
- memory_cache .wrap (id, function (cache_callback ) {
59
- get_user (id, cache_callback );
57
+ function getCachedUser (id , cb ) {
58
+ memoryCache .wrap (id, function (cacheCallback ) {
59
+ getUser (id, cacheCallback );
60
60
}, ttl, cb);
61
61
}
62
62
```
@@ -86,40 +86,40 @@ Redis cache store with connection pooling.
86
86
### Single Store
87
87
88
88
``` javascript
89
- var cache_manager = require (' cache-manager' );
90
- var memory_cache = cache_manager .caching ({store: ' memory' , max: 100 , ttl: 10 /* seconds*/ });
89
+ var cacheManager = require (' cache-manager' );
90
+ var memoryCache = cacheManager .caching ({store: ' memory' , max: 100 , ttl: 10 /* seconds*/ });
91
91
var ttl = 5 ;
92
92
// Note: callback is optional in set() and del().
93
93
94
- memory_cache .set (' foo' , ' bar' , ttl, function (err ) {
94
+ memoryCache .set (' foo' , ' bar' , ttl, function (err ) {
95
95
if (err) { throw err; }
96
96
97
- memory_cache .get (' foo' , function (err , result ) {
97
+ memoryCache .get (' foo' , function (err , result ) {
98
98
console .log (result);
99
99
// >> 'bar'
100
- memory_cache .del (' foo' , function (err ) {});
100
+ memoryCache .del (' foo' , function (err ) {});
101
101
});
102
102
});
103
103
104
- function get_user (id , cb ) {
104
+ function getUser (id , cb ) {
105
105
setTimeout (function () {
106
106
console .log (" Returning user from slow database." );
107
107
cb (null , {id: id, name: ' Bob' });
108
108
}, 100 );
109
109
}
110
110
111
- var user_id = 123 ;
112
- var key = ' user_' + user_id ;
111
+ var userId = 123 ;
112
+ var key = ' user_' + userId ;
113
113
114
114
// Note: ttl is optional in wrap()
115
- memory_cache .wrap (key, function (cb ) {
116
- get_user (user_id , cb);
115
+ memoryCache .wrap (key, function (cb ) {
116
+ getUser (userId , cb);
117
117
}, ttl, function (err , user ) {
118
118
console .log (user);
119
119
120
- // Second time fetches user from memory_cache
121
- memory_cache .wrap (key, function (cb ) {
122
- get_user (user_id , cb);
120
+ // Second time fetches user from memoryCache
121
+ memoryCache .wrap (key, function (cb ) {
122
+ getUser (userId , cb);
123
123
}, function (err , user ) {
124
124
console .log (user);
125
125
});
@@ -143,10 +143,10 @@ function respond(res, err, data) {
143
143
}
144
144
145
145
app .get (' /foo/bar' , function (req , res ) {
146
- var cache_key = ' foo-bar:' + JSON .stringify (req .query );
146
+ var cacheKey = ' foo-bar:' + JSON .stringify (req .query );
147
147
var ttl = 10 ;
148
- memory_cache .wrap (cache_key , function (cache_cb ) {
149
- DB .find (req .query , cache_cb );
148
+ memoryCache .wrap (cacheKey , function (cacheCallback ) {
149
+ DB .find (req .query , cacheCallback );
150
150
}, ttl, function (err , result ) {
151
151
respond (res, err, result);
152
152
});
@@ -162,45 +162,45 @@ in an instance of it, or pass in the path to the module.
162
162
E.g.,
163
163
164
164
``` javascript
165
- var my_store = require (' your-homemade-store' );
166
- var cache = cache_manager .caching ({store: my_store });
165
+ var myStore = require (' your-homemade-store' );
166
+ var cache = cacheManager .caching ({store: myStore });
167
167
// or
168
- var cache = cache_manager .caching ({store: ' /path/to/your/store' });
168
+ var cache = cacheManager .caching ({store: ' /path/to/your/store' });
169
169
```
170
170
171
171
### Multi-Store
172
172
173
173
``` javascript
174
- var multi_cache = cache_manager . multi_caching ([memory_cache, some_other_cache ]);
175
- user_id2 = 456 ;
176
- key2 = ' user_' + user_id ;
174
+ var multiCache = cacheManager . multiCaching ([memoryCache, someOtherCache ]);
175
+ userId2 = 456 ;
176
+ key2 = ' user_' + userId ;
177
177
ttl = 5 ;
178
178
179
179
// Sets in all caches.
180
- multi_cache .set (' foo2' , ' bar2' , ttl, function (err ) {
180
+ multiCache .set (' foo2' , ' bar2' , ttl, function (err ) {
181
181
if (err) { throw err; }
182
182
183
183
// Fetches from highest priority cache that has the key.
184
- multi_cache .get (' foo2' , function (err , result ) {
184
+ multiCache .get (' foo2' , function (err , result ) {
185
185
console .log (result);
186
186
// >> 'bar2'
187
187
188
188
// Delete from all caches
189
- multi_cache .del (' foo2' );
189
+ multiCache .del (' foo2' );
190
190
});
191
191
});
192
192
193
193
// Note: ttl is optional in wrap()
194
- multi_cache .wrap (key2, function (cb ) {
195
- get_user (user_id2 , cb);
194
+ multiCache .wrap (key2, function (cb ) {
195
+ getUser (userId2 , cb);
196
196
}, ttl, function (err , user ) {
197
197
console .log (user);
198
198
199
- // Second time fetches user from memory_cache , since it's highest priority.
199
+ // Second time fetches user from memoryCache , since it's highest priority.
200
200
// If the data expires in the memory cache, the next fetch would pull it from
201
- // the 'some_other_cache ', and set the data in memory again.
202
- multi_cache .wrap (key2, function (cb ) {
203
- get_user (user_id2 , cb);
201
+ // the 'someOtherCache ', and set the data in memory again.
202
+ multiCache .wrap (key2, function (cb ) {
203
+ getUser (userId2 , cb);
204
204
}, function (err , user ) {
205
205
console .log (user);
206
206
});
0 commit comments