Created
July 23, 2013 22:35
-
-
Save mikeal/6066773 to your computer and use it in GitHub Desktop.
LRU cache for the npm registry.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lru = require('lru-cache') | |
, couchwatch = require('couchwatch') | |
, request = require('request') | |
; | |
function RegistryCache () { | |
this.cache = lru() | |
this.watch = couchwatch('http://isaacs.iriscouch.com/registry', -1) | |
this.watch.on('row', function (change) { | |
this.cache.del(change.id) | |
}) | |
} | |
RegistryCache.prototype.get = function (key, cb) { | |
if (this.cache.has(key)) return cb(null, this.cache.get(key)) | |
var self = this | |
request.get('http://isaacs.iriscouch.com/registry/'+key, {json:true}, function (e, resp, doc) { | |
if (e) return cb(e) | |
if (resp.statusCode !== 200) return cb(new Error('not found')) | |
self.cache.set(doc._id, doc) | |
cb(null, doc) | |
}) | |
} | |
module.exports = function () {return new RegistryCache()} | |
// ;(new RegistryCache()).get('request', function (e, doc) { | |
// console.log(e, doc) | |
// }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment