Skip to content

Instantly share code, notes, and snippets.

@mikeal
Created July 23, 2013 22:35
Show Gist options
  • Save mikeal/6066773 to your computer and use it in GitHub Desktop.
Save mikeal/6066773 to your computer and use it in GitHub Desktop.
LRU cache for the npm registry.
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