Skip to content

Commit

Permalink
fix(cache): Switch to lru-cache to save ourselves from unlimited memo…
Browse files Browse the repository at this point in the history
…ry consumption

PR-URL: #38
Credit: @iarna
Close: #38
Reviewed-by: @isaacs
  • Loading branch information
iarna authored and isaacs committed Aug 4, 2019
1 parent 1942b17 commit e518222
Show file tree
Hide file tree
Showing 3 changed files with 791 additions and 1,021 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var url = require('url')
var gitHosts = require('./git-host-info.js')
var GitHost = module.exports = require('./git-host.js')
var LRU = require('lru-cache')
var cache = new LRU({max: 1000})

var protocolToRepresentationMap = {
'git+ssh': 'sshurl',
Expand All @@ -23,17 +25,15 @@ var authProtocols = {
'git+http:': true
}

var cache = {}

module.exports.fromUrl = function (giturl, opts) {
if (typeof giturl !== 'string') return
var key = giturl + JSON.stringify(opts || {})

if (!(key in cache)) {
cache[key] = fromUrl(giturl, opts)
if (!cache.has(key)) {
cache.set(key, fromUrl(giturl, opts))
}

return cache[key]
return cache.get(key)
}

function fromUrl (giturl, opts) {
Expand Down
Loading

0 comments on commit e518222

Please # to comment.