Skip to content

Commit e518222

Browse files
iarnaisaacs
authored andcommitted
fix(cache): Switch to lru-cache to save ourselves from unlimited memory consumption
PR-URL: #38 Credit: @iarna Close: #38 Reviewed-by: @isaacs
1 parent 1942b17 commit e518222

File tree

3 files changed

+791
-1021
lines changed

3 files changed

+791
-1021
lines changed

index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var url = require('url')
33
var gitHosts = require('./git-host-info.js')
44
var GitHost = module.exports = require('./git-host.js')
5+
var LRU = require('lru-cache')
6+
var cache = new LRU({max: 1000})
57

68
var protocolToRepresentationMap = {
79
'git+ssh': 'sshurl',
@@ -23,17 +25,15 @@ var authProtocols = {
2325
'git+http:': true
2426
}
2527

26-
var cache = {}
27-
2828
module.exports.fromUrl = function (giturl, opts) {
2929
if (typeof giturl !== 'string') return
3030
var key = giturl + JSON.stringify(opts || {})
3131

32-
if (!(key in cache)) {
33-
cache[key] = fromUrl(giturl, opts)
32+
if (!cache.has(key)) {
33+
cache.set(key, fromUrl(giturl, opts))
3434
}
3535

36-
return cache[key]
36+
return cache.get(key)
3737
}
3838

3939
function fromUrl (giturl, opts) {

0 commit comments

Comments
 (0)