Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[refactor] Caches now with a constructor/instance api

  • Loading branch information...
commit 1fec6e436f8d1fcc5b695fd272c81c8a0c1ed56c 1 parent 3172de8
@jfhbrook authored
View
108 bundler/cache.js
@@ -6,54 +6,54 @@ var level = require('level'),
cull = require('./npm-cull'),
log = require('minilog')('leveldb');
-var Cache = function (location) {
- this.db = sublevel(ttl(level(location)));
+var Cache = function (name, db, opts) {
+ opts = opts || {}
+ this.db = db.sublevel(name);
+ this.name = name;
+ this.hashfxn = opts.hashfxn || opts.hashFunction || defaultHashFxn
+ this.ttl = opts.ttl;
};
-Cache.prototype.open = function (name, options) {
- var db = this.db.sublevel(name),
- hashfxn = options.hashFunction || defaultHashFxn,
- ttl = options.ttl;
-
- return function check(body, generate, cb) {
-
- var hash = hashfxn(body);
-
- log('cache: checking `' + name + '` for hash `' + hash + '`...');
-
- db.get(hash, function (err, res) {
-
- if (err && err.name === 'NotFoundError') {
-
- log('cache: `' + name + '` did not have `' + hash + '`.');
- return generate(function (err, _res) {
- if (err) return cb(err);
-
- log(
- 'cache: saving hash `' + hash + '` in `' + name + '` ' + (
- (typeof ttl === 'number')
- ? 'with ttl ' + ttl +'...'
- : '...'
- )
- );
-
- if (ttl) {
- db.put(hash, JSON.stringify(_res), { ttl: ttl }, finish);
- }
- else {
- db.put(hash, JSON.stringify(_res), finish);
- }
-
- function finish(err) {
- log('saved hash `' + hash + '` in `' + name + '`.');
- cb(err, _res);
- }
- });
- }
-
- cb(err, JSON.parse(res));
- });
- };
+Cache.prototype.check = function check(body, generate, cb) {
+ var hash = this.hashfxn(body),
+ db = this.db,
+ name = this.name,
+ ttl = this.ttl;
+
+ log('cache: checking `' + name + '` for hash `' + hash + '`...');
+
+ db.get(hash, function (err, res) {
+
+ if (err && err.name === 'NotFoundError') {
+
+ log('cache: `' + name + '` did not have `' + hash + '`.');
+ return generate(function (err, _res) {
+ if (err) return cb(err);
+
+ log(
+ 'cache: saving hash `' + hash + '` in `' + name + '` ' + (
+ (typeof ttl === 'number')
+ ? 'with ttl ' + ttl +'...'
+ : '...'
+ )
+ );
+
+ if (ttl) {
+ db.put(hash, JSON.stringify(_res), { ttl: ttl }, finish);
+ }
+ else {
+ db.put(hash, JSON.stringify(_res), finish);
+ }
+
+ function finish(err) {
+ log('saved hash `' + hash + '` in `' + name + '`.');
+ cb(err, _res);
+ }
+ });
+ }
+
+ cb(err, JSON.parse(res));
+ });
};
var SECONDS = 1000,
@@ -63,21 +63,22 @@ var SECONDS = 1000,
var c = module.exports = function (location) {
- var cache = new Cache(location),
- bundles, multibundles, aliases;
+ var db = sublevel(ttl(level(location)));
+
+ var bundles, multibundles, aliases;
- bundles = cache.open('bundles', {
+ bundles = new Cache('bundles', db, {
ttl: 30 * DAYS
});
- aliases = cache.open('aliases', {
+ aliases = cull(new Cache('aliases', db, {
hashfxn: function (o) {
return o.module + '@' + o.semver;
},
ttl: 1 * DAYS
- });
+ }));
- multibundles = cache.open('multibundles', {
+ multibundles = new Cache('multibundles', db, {
hashfxn: function (o) {
if (typeof o === 'string' && o.length === 24) {
log('cache: Input for `multibundles` appears to be an md5 hash already');
@@ -88,8 +89,6 @@ var c = module.exports = function (location) {
ttl: 30 * DAYS
});
- cull(cache);
-
return {
bundles: bundles,
multibundles: multibundles,
@@ -99,6 +98,7 @@ var c = module.exports = function (location) {
};
c.defaultHashFxn = defaultHashFxn;
+c.Cache = Cache;
function defaultHashFxn(o) {
return crypto
View
4 bundler/index.js
@@ -29,7 +29,7 @@ module.exports = function bundler(opts) {
return checkBundles(null, process.version);
}
- c.aliases({ module: module, semver: semver }, function resolve(cb) {
+ c.aliases.check({ module: module, semver: semver }, function resolve(cb) {
registry.resolve(module, semver, function (err, v) {
if (err) return callback(err);
@@ -42,7 +42,7 @@ module.exports = function bundler(opts) {
pkg.version = version;
- c.bundles(pkg, function (cb) {
+ c.bundles.check(pkg, function (cb) {
return build(pkg, cb);
}, callback);
}
View
2  bundler/npm-cull.js
@@ -2,7 +2,7 @@ var NpmPublishStream = require('npm-publish-stream'),
log = require('minilog')('cull');
module.exports = function (cache) {
- var aliases = cache.db.sublevel('aliases'),
+ var aliases = cache.db,
pubStream = new NpmPublishStream;
pubStream.on('data', function (data) {
View
2  multiple.js
@@ -42,7 +42,7 @@ function create(bundle) {
return res.end(stringifyError.goodbye);
}
- cache.multibundles(cache.defaultHashFxn(opts), function multibundle(cb) {
+ cache.multibundles.check(cache.defaultHashFxn(opts), function multibundle(cb) {
var keys = Object.keys(deps),
count = keys.length,
modules = {},
Please sign in to comment.
Something went wrong with that request. Please try again.