WIP - add extractor, generate snippet_data
This commit is contained in:
132
node_modules/cache-manager/examples/example.js
generated
vendored
Normal file
132
node_modules/cache-manager/examples/example.js
generated
vendored
Normal file
@ -0,0 +1,132 @@
|
||||
/*jshint unused:false*/
|
||||
// Note: ttls are in seconds
|
||||
var cacheManager = require('../');
|
||||
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10});
|
||||
var memoryCache2 = cacheManager.caching({store: 'memory', max: 100, ttl: 100});
|
||||
var ttl; //Can't use a different ttl per set() call with memory cache
|
||||
|
||||
//
|
||||
// Basic usage
|
||||
//
|
||||
memoryCache.set('foo', 'bar', function(err) {
|
||||
if (err) { throw err; }
|
||||
|
||||
memoryCache.get('foo', function(err, result) {
|
||||
console.log(result);
|
||||
// >> 'bar'
|
||||
memoryCache.del('foo', function(err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function getUser(id, cb) {
|
||||
setTimeout(function() {
|
||||
console.log("Fetching user from slow database.");
|
||||
cb(null, {id: id, name: 'Bob'});
|
||||
}, 100);
|
||||
}
|
||||
|
||||
var userId = 123;
|
||||
var key = 'user_' + userId;
|
||||
|
||||
//
|
||||
// wrap() example
|
||||
//
|
||||
|
||||
// Instead of manually managing the cache like this:
|
||||
function getCachedUserManually(id, cb) {
|
||||
memoryCache.get(id, function(err, result) {
|
||||
if (err) { return cb(err); }
|
||||
|
||||
if (result) {
|
||||
return cb(null, result);
|
||||
}
|
||||
|
||||
getUser(id, function(err, result) {
|
||||
if (err) { return cb(err); }
|
||||
memoryCache.set(id, result);
|
||||
cb(null, result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ... you can instead use the `wrap` function:
|
||||
function getCachedUser(id, cb) {
|
||||
memoryCache.wrap(id, function(cacheCallback) {
|
||||
getUser(id, cacheCallback);
|
||||
}, cb);
|
||||
}
|
||||
|
||||
getCachedUser(userId, function(err, user) {
|
||||
// First time fetches the user from the (fake) database:
|
||||
console.log(user);
|
||||
|
||||
getCachedUser(userId, function(err, user) {
|
||||
// Second time fetches from cache.
|
||||
console.log(user);
|
||||
});
|
||||
});
|
||||
|
||||
// Outputs:
|
||||
// Returning user from slow database.
|
||||
// { id: 123, name: 'Bob' }
|
||||
// { id: 123, name: 'Bob' }
|
||||
|
||||
// Same as above, but written differently:
|
||||
memoryCache.wrap(key, function(cb) {
|
||||
getUser(userId, cb);
|
||||
}, function(err, user) {
|
||||
console.log(user);
|
||||
|
||||
// Second time fetches user from memoryCache
|
||||
memoryCache.wrap(key, function(cb) {
|
||||
getUser(userId, cb);
|
||||
}, function(err, user) {
|
||||
console.log(user);
|
||||
});
|
||||
});
|
||||
|
||||
//
|
||||
// multi-cache example
|
||||
//
|
||||
var multiCache = cacheManager.multiCaching([memoryCache, memoryCache2]);
|
||||
var userId2 = 456;
|
||||
var key2 = 'user_' + userId;
|
||||
var ttl2; //Can't use a different ttl per set() call with memory cache
|
||||
|
||||
multiCache.wrap(key2, function(cb) {
|
||||
getUser(userId2, cb);
|
||||
}, function(err, user) {
|
||||
console.log(user);
|
||||
|
||||
// Second time fetches user from memoryCache, since it's highest priority.
|
||||
// If the data expires in memoryCache, the next fetch would pull it from
|
||||
// the memoryCache2, and set the data in memoryCache.
|
||||
multiCache.wrap(key2, function(cb) {
|
||||
getUser(userId2, cb);
|
||||
}, function(err, user) {
|
||||
console.log(user);
|
||||
});
|
||||
|
||||
// Sets in all caches.
|
||||
multiCache.set('foo2', 'bar2', {ttl: ttl2}, function(err) {
|
||||
if (err) { throw err; }
|
||||
|
||||
// Fetches from highest priority cache that has the key.
|
||||
multiCache.get('foo2', function(err, result) {
|
||||
console.log(result);
|
||||
// >> 'bar2'
|
||||
|
||||
// Delete from all caches
|
||||
multiCache.del('foo2', function(err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
147
node_modules/cache-manager/examples/redis_example/example.js
generated
vendored
Normal file
147
node_modules/cache-manager/examples/redis_example/example.js
generated
vendored
Normal file
@ -0,0 +1,147 @@
|
||||
// Setup:
|
||||
// npm install redis@0.12.1 sol-redis-pool@0.2.0
|
||||
// node examples/redis_example/example.js
|
||||
|
||||
var util = require('util');
|
||||
var assert = require('assert');
|
||||
var cacheManager = require('../../');
|
||||
var redisStore = require('./redis_store');
|
||||
// Note: ttl is in seconds
|
||||
var redisCache = cacheManager.caching({store: redisStore, db: 0, ttl: 100});
|
||||
var ttl = 60;
|
||||
|
||||
console.log("set/get/del example:");
|
||||
|
||||
redisCache.set('foo', 'bar', {ttl: ttl}, function(err) {
|
||||
if (err) { throw err; }
|
||||
|
||||
redisCache.get('foo', function(err, result) {
|
||||
if (err) { throw err; }
|
||||
console.log("result fetched from cache: " + result);
|
||||
// >> 'bar'
|
||||
|
||||
redisCache.ttl('foo', function(err, result) {
|
||||
if (err) { throw err; }
|
||||
assert.ok(result > 59 && result < 61);
|
||||
|
||||
redisCache.del('foo', function(err) {
|
||||
if (err) { throw err; }
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// TTL defaults to what we passed into the caching function (100)
|
||||
redisCache.set('foo-no-ttl', 'bar-no-ttl', function(err) {
|
||||
if (err) { throw err; }
|
||||
|
||||
redisCache.get('foo-no-ttl', function(err, result) {
|
||||
if (err) { throw err; }
|
||||
console.log("result fetched from cache: " + result);
|
||||
// >> 'bar'
|
||||
|
||||
redisCache.ttl('foo-no-ttl', function(err, result) {
|
||||
if (err) { throw err; }
|
||||
assert.ok(result > 99 && result < 101);
|
||||
|
||||
redisCache.del('foo-no-ttl', function(err) {
|
||||
if (err) { throw err; }
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Calls Redis 'set' instead of 'setex'
|
||||
redisCache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) {
|
||||
if (err) { throw err; }
|
||||
|
||||
redisCache.get('foo-zero-ttl', function(err, result) {
|
||||
if (err) { throw err; }
|
||||
console.log("result fetched from cache: " + result);
|
||||
// >> 'bar'
|
||||
|
||||
redisCache.ttl('foo-zero-ttl', function(err, result) {
|
||||
if (err) { throw err; }
|
||||
assert.ok(result < 0);
|
||||
|
||||
redisCache.del('foo-zero-ttl', function(err) {
|
||||
if (err) { throw err; }
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
var userId = 123;
|
||||
|
||||
function createKey(id) {
|
||||
return 'user_' + id;
|
||||
}
|
||||
|
||||
function getUser(id, cb) {
|
||||
setTimeout(function() {
|
||||
console.log("\n\nReturning user from slow database.");
|
||||
cb(null, {id: id, name: 'Bob'});
|
||||
}, 100);
|
||||
}
|
||||
|
||||
function getUserFromCache(id, cb) {
|
||||
var key = createKey(id);
|
||||
redisCache.wrap(key, function(cacheCb) {
|
||||
getUser(userId, cacheCb);
|
||||
}, {ttl: ttl}, cb);
|
||||
}
|
||||
|
||||
getUserFromCache(userId, function(err, user) {
|
||||
console.log(user);
|
||||
|
||||
// Second time fetches user from redisCache
|
||||
getUserFromCache(userId, function(err, user) {
|
||||
console.log("user from second cache request:");
|
||||
console.log(user);
|
||||
|
||||
redisCache.keys(function(err, keys) {
|
||||
console.log("keys: " + util.inspect(keys));
|
||||
|
||||
var key = createKey(userId);
|
||||
redisCache.del(key, function(err) {
|
||||
if (err) { throw err; }
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Outputs:
|
||||
// { id: 123, name: 'Bob' }
|
||||
// user from second cache request:
|
||||
// { id: 123, name: 'Bob' }
|
||||
// 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
124
node_modules/cache-manager/examples/redis_example/redis_store.js
generated
vendored
Normal file
124
node_modules/cache-manager/examples/redis_example/redis_store.js
generated
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
/**
|
||||
* This is a very basic example of how you can implement your own Redis-based
|
||||
* cache store with connection pooling.
|
||||
*/
|
||||
|
||||
var RedisPool = require('sol-redis-pool');
|
||||
|
||||
function redisStore(args) {
|
||||
args = args || {};
|
||||
var self = {};
|
||||
var ttlDefault = args.ttl;
|
||||
self.name = 'redis';
|
||||
|
||||
var redisOptions = {
|
||||
host: args.host || '127.0.0.1',
|
||||
port: args.port || 6379
|
||||
};
|
||||
|
||||
var pool = new RedisPool(redisOptions, {});
|
||||
|
||||
function connect(cb) {
|
||||
pool.acquire(function(err, conn) {
|
||||
if (err) {
|
||||
pool.release(conn);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (args.db || args.db === 0) {
|
||||
conn.select(args.db);
|
||||
}
|
||||
|
||||
cb(null, conn);
|
||||
});
|
||||
}
|
||||
|
||||
function handleResponse(conn, cb, opts) {
|
||||
opts = opts || {};
|
||||
|
||||
return function(err, result) {
|
||||
pool.release(conn);
|
||||
|
||||
if (err) { return cb(err); }
|
||||
|
||||
if (opts.parse) {
|
||||
result = JSON.parse(result);
|
||||
}
|
||||
|
||||
cb(null, result);
|
||||
};
|
||||
}
|
||||
|
||||
self.get = function(key, options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
}
|
||||
|
||||
connect(function(err, conn) {
|
||||
if (err) { return cb(err); }
|
||||
conn.get(key, handleResponse(conn, cb, {parse: true}));
|
||||
});
|
||||
};
|
||||
|
||||
self.set = function(key, value, options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
options = options || {};
|
||||
|
||||
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : ttlDefault;
|
||||
|
||||
connect(function(err, conn) {
|
||||
if (err) { return cb(err); }
|
||||
var val = JSON.stringify(value);
|
||||
|
||||
if (ttl) {
|
||||
conn.setex(key, ttl, val, handleResponse(conn, cb));
|
||||
} else {
|
||||
conn.set(key, val, handleResponse(conn, cb));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
self.del = function(key, options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
}
|
||||
connect(function(err, conn) {
|
||||
if (err) { return cb(err); }
|
||||
conn.del(key, handleResponse(conn, cb));
|
||||
});
|
||||
};
|
||||
|
||||
self.ttl = function(key, cb) {
|
||||
connect(function(err, conn) {
|
||||
if (err) { return cb(err); }
|
||||
conn.ttl(key, handleResponse(conn, cb));
|
||||
});
|
||||
};
|
||||
|
||||
self.keys = function(pattern, cb) {
|
||||
if (typeof pattern === 'function') {
|
||||
cb = pattern;
|
||||
pattern = '*';
|
||||
}
|
||||
|
||||
connect(function(err, conn) {
|
||||
if (err) { return cb(err); }
|
||||
conn.keys(pattern, handleResponse(conn, cb));
|
||||
});
|
||||
};
|
||||
|
||||
self.isCacheableValue = function(value) {
|
||||
return value !== null && value !== undefined;
|
||||
};
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
create: function(args) {
|
||||
return redisStore(args);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user