Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache): Switch to lru-cache to save ourselves from unlimited memory consumption #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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