|
@ -1,3 +1,4 @@ |
|
|
|
|
|
/*jshint unused:false*/ |
|
|
var cache_manager = require('../'); |
|
|
var cache_manager = require('../'); |
|
|
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); |
|
|
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); |
|
|
var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100/*seconds*/}); |
|
|
var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100/*seconds*/}); |
|
@ -5,19 +6,23 @@ var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100/* |
|
|
//
|
|
|
//
|
|
|
// Basic usage
|
|
|
// Basic usage
|
|
|
//
|
|
|
//
|
|
|
memory_cache2.set('foo', 'bar', function (err) { |
|
|
memory_cache.set('foo', 'bar', function (err) { |
|
|
if (err) { throw err; } |
|
|
if (err) { throw err; } |
|
|
|
|
|
|
|
|
memory_cache2.get('foo', function (err, result) { |
|
|
memory_cache.get('foo', function (err, result) { |
|
|
console.log(result); |
|
|
console.log(result); |
|
|
// >> 'bar'
|
|
|
// >> 'bar'
|
|
|
memory_cache2.del('foo', function (err) { console.log(err); }); |
|
|
memory_cache.del('foo', function (err) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
console.log(err); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
function get_user(id, cb) { |
|
|
function get_user(id, cb) { |
|
|
setTimeout(function () { |
|
|
setTimeout(function () { |
|
|
console.log("Returning user from slow database."); |
|
|
console.log("Fetching user from slow database."); |
|
|
cb(null, {id: id, name: 'Bob'}); |
|
|
cb(null, {id: id, name: 'Bob'}); |
|
|
}, 100); |
|
|
}, 100); |
|
|
} |
|
|
} |
|
@ -28,15 +33,37 @@ var key = 'user_' + user_id; |
|
|
//
|
|
|
//
|
|
|
// wrap() example
|
|
|
// wrap() example
|
|
|
//
|
|
|
//
|
|
|
memory_cache2.wrap(key, function (cb) { |
|
|
|
|
|
get_user(user_id, cb); |
|
|
// Instead of manually managing the cache like this:
|
|
|
}, function (err, user) { |
|
|
function get_cached_user_manually(id, cb) { |
|
|
|
|
|
memory_cache.get(id, function (err, result) { |
|
|
|
|
|
if (err) { return cb(err); } |
|
|
|
|
|
|
|
|
|
|
|
if (result) { |
|
|
|
|
|
return cb(null, result); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get_user(id, function (err, result) { |
|
|
|
|
|
if (err) { return cb(err); } |
|
|
|
|
|
memory_cache.set(id, result); |
|
|
|
|
|
cb(null, result); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ... you can instead use the `wrap` function:
|
|
|
|
|
|
function get_cached_user(id, cb) { |
|
|
|
|
|
memory_cache.wrap(id, function (cache_callback) { |
|
|
|
|
|
get_user(id, cache_callback); |
|
|
|
|
|
}, cb); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get_cached_user(user_id, function (err, user) { |
|
|
|
|
|
// First time fetches the user from the (fake) database:
|
|
|
console.log(user); |
|
|
console.log(user); |
|
|
|
|
|
|
|
|
// Second time fetches user from memory_cache2
|
|
|
get_cached_user(user_id, function (err, user) { |
|
|
memory_cache2.wrap(key, function (cb) { |
|
|
// Second time fetches from cache.
|
|
|
get_user(user_id, cb); |
|
|
|
|
|
}, function (err, user) { |
|
|
|
|
|
console.log(user); |
|
|
console.log(user); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
@ -47,6 +74,23 @@ memory_cache2.wrap(key, function (cb) { |
|
|
// { id: 123, name: 'Bob' }
|
|
|
// { id: 123, name: 'Bob' }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Same as above, but written differently:
|
|
|
|
|
|
memory_cache.wrap(key, function (cb) { |
|
|
|
|
|
get_user(user_id, cb); |
|
|
|
|
|
}, function (err, user) { |
|
|
|
|
|
console.log(user); |
|
|
|
|
|
|
|
|
|
|
|
// Second time fetches user from memory_cache
|
|
|
|
|
|
memory_cache.wrap(key, function (cb) { |
|
|
|
|
|
get_user(user_id, cb); |
|
|
|
|
|
}, function (err, user) { |
|
|
|
|
|
console.log(user); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
// multi-cache example
|
|
|
|
|
|
//
|
|
|
var multi_cache = cache_manager.multi_caching([memory_cache, memory_cache2]); |
|
|
var multi_cache = cache_manager.multi_caching([memory_cache, memory_cache2]); |
|
|
var user_id2 = 456; |
|
|
var user_id2 = 456; |
|
|
var key2 = 'user_' + user_id; |
|
|
var key2 = 'user_' + user_id; |
|
@ -76,7 +120,9 @@ multi_cache.wrap(key2, function (cb) { |
|
|
|
|
|
|
|
|
// Delete from all caches
|
|
|
// Delete from all caches
|
|
|
multi_cache.del('foo2', function (err) { |
|
|
multi_cache.del('foo2', function (err) { |
|
|
|
|
|
if (err) { |
|
|
console.log(err); |
|
|
console.log(err); |
|
|
|
|
|
} |
|
|
process.exit(); |
|
|
process.exit(); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|