You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.4 KiB

11 years ago
/*jshint unused:false*/
10 years ago
// Note: ttls are in seconds
var cache_manager = require('../');
10 years ago
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10});
var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100});
10 years ago
var ttl; //Can't use a different ttl per set() call with memory cache
12 years ago
//
// Basic usage
//
10 years ago
memory_cache.set('foo', 'bar', ttl, function (err) {
12 years ago
if (err) { throw err; }
11 years ago
memory_cache.get('foo', function (err, result) {
12 years ago
console.log(result);
// >> 'bar'
11 years ago
memory_cache.del('foo', function (err) {
if (err) {
console.log(err);
}
});
12 years ago
});
});
function get_user(id, cb) {
setTimeout(function () {
11 years ago
console.log("Fetching user from slow database.");
12 years ago
cb(null, {id: id, name: 'Bob'});
}, 100);
}
var user_id = 123;
var key = 'user_' + user_id;
12 years ago
//
10 years ago
// wrap() example
//
11 years ago
// Instead of manually managing the cache like this:
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:
12 years ago
console.log(user);
11 years ago
get_cached_user(user_id, function (err, user) {
// Second time fetches from cache.
12 years ago
console.log(user);
});
});
// Outputs:
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
11 years ago
// 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 user_id2 = 456;
var key2 = 'user_' + user_id;
10 years ago
var ttl2; //Can't use a different ttl per set() call with memory cache
12 years ago
multi_cache.wrap(key2, function (cb) {
get_user(user_id2, cb);
}, function (err, user) {
console.log(user);
// Second time fetches user from memory_cache, since it's highest priority.
// If the data expires in the memory cache, the next fetch would pull it from
// the Redis cache, and set the data in memory again.
multi_cache.wrap(key2, function (cb) {
get_user(user_id2, cb);
}, function (err, user) {
console.log(user);
});
// Sets in all caches.
10 years ago
multi_cache.set('foo2', 'bar2', ttl2, function (err) {
12 years ago
if (err) { throw err; }
// Fetches from highest priority cache that has the key.
multi_cache.get('foo2', function (err, result) {
12 years ago
console.log(result);
// >> 'bar2'
// Delete from all caches
multi_cache.del('foo2', function (err) {
11 years ago
if (err) {
console.log(err);
}
12 years ago
process.exit();
});
});
});
});