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
  • Loading branch information
iarna committed Jul 10, 2018
1 parent ab35a90 commit 4ebabd3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 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 = 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
17 changes: 6 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"release": "standard-version -s",
"test": "tap -J --nyc-arg=--all --coverage test"
},
"dependencies": {
"lru-cache": "^4.1.3"
},
"devDependencies": {
"standard": "^11.0.1",
"standard-version": "^4.3.0",
Expand Down

0 comments on commit 4ebabd3

Please sign in to comment.