Browse Source

example updates

feature/specify-what-to-cache
Bryan Donovan 10 years ago
parent
commit
29c76151ca
  1. 4
      examples/example.js
  2. 31
      examples/redis_example/example.js
  3. 5
      lib/caching.js

4
examples/example.js

@ -103,8 +103,8 @@ multiCache.wrap(key2, function(cb) {
console.log(user); console.log(user);
// Second time fetches user from memoryCache, since it's highest priority. // Second time fetches user from memoryCache, since it's highest priority.
// If the data expires in the memory cache, the next fetch would pull it from // If the data expires in memoryCache, the next fetch would pull it from
// the Redis cache, and set the data in memory again. // the memoryCache2, and set the data in memoryCache.
multiCache.wrap(key2, function(cb) { multiCache.wrap(key2, function(cb) {
getUser(userId2, cb); getUser(userId2, cb);
}, function(err, user) { }, function(err, user) {

31
examples/redis_example/example.js

@ -86,7 +86,6 @@ getUserFromCache(userId, function(err, user) {
var key = createKey(userId); var key = createKey(userId);
redisCache.del(key, function(err) { redisCache.del(key, function(err) {
if (err) { throw err; } if (err) { throw err; }
process.exit();
}); });
}); });
}); });
@ -97,3 +96,33 @@ getUserFromCache(userId, function(err, user) {
// user from second cache request: // user from second cache request:
// { id: 123, name: 'Bob' } // { id: 123, name: 'Bob' }
// keys: [ 'user_123' ] // keys: [ 'user_123' ]
var redisCache2 = cacheManager.caching({store: redisStore, db: 1, ttl: 100});
var multiCache = cacheManager.multiCaching([redisCache, redisCache2]);
var userId2 = 456;
var key2 = 'user_' + userId;
var ttl2 = 50;
multiCache.wrap(key2, function(cb) {
getUser(userId2, cb);
}, {ttl: ttl2}, function(err, user) {
console.log("user: ", user);
// Second time fetches user from redisCache, since it's highest priority.
// If the data expires in the redisCache, the next fetch would pull it from
// redisCache2, and set the data in redisCache again.
multiCache.wrap(key2, function(cb) {
getUser(userId2, cb);
}, function(err, user) {
console.log("user, second fetch:", user);
});
multiCache.getAndPassUp(key2, function(err, result) {
console.log("\ngetAndPassUp result: ", result);
multiCache.del(key2, function(err) {
if (err) { throw err; }
process.exit();
});
});
});

5
lib/caching.js

@ -1,5 +1,4 @@
/*jshint maxcomplexity:15*/ /*jshint maxcomplexity:15*/
/*jshint -W072 */
var domain = require('domain'); var domain = require('domain');
var CallbackFiller = require('./callback_filler'); var CallbackFiller = require('./callback_filler');
@ -31,9 +30,9 @@ var caching = function(args) {
* *
* @example * @example
* *
* var key = 'user_' + user_id; * var key = 'user_' + userId;
* cache.wrap(key, function(cb) { * cache.wrap(key, function(cb) {
* User.get(user_id, cb); * User.get(userId, cb);
* }, function(err, user) { * }, function(err, user) {
* console.log(user); * console.log(user);
* }); * });

Loading…
Cancel
Save