Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 8bba2b6

Browse files
committed
Switch to ConcurrentDictionary for the api client cache
1 parent c571592 commit 8bba2b6

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/GitHub.Api/SimpleApiClientFactory.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitHub.Primitives;
66
using GitHub.Services;
77
using Octokit;
8+
using System.Collections.Concurrent;
89

910
namespace GitHub.Api
1011
{
@@ -16,7 +17,7 @@ public class SimpleApiClientFactory : ISimpleApiClientFactory
1617
readonly Lazy<IEnterpriseProbeTask> lazyEnterpriseProbe;
1718
readonly Lazy<IWikiProbe> lazyWikiProbe;
1819

19-
static readonly Dictionary<UriString, ISimpleApiClient> cache = new Dictionary<UriString, ISimpleApiClient>();
20+
static readonly ConcurrentDictionary<UriString, ISimpleApiClient> cache = new ConcurrentDictionary<UriString, ISimpleApiClient>();
2021

2122
[ImportingConstructor]
2223
public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
@@ -28,25 +29,16 @@ public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enter
2829

2930
public ISimpleApiClient Create(UriString repositoryUrl)
3031
{
31-
lock (cache)
32-
{
33-
if (!cache.ContainsKey(repositoryUrl))
34-
{
35-
var hostAddress = HostAddress.Create(repositoryUrl);
36-
var apiBaseUri = hostAddress.ApiUri;
37-
cache.Add(repositoryUrl, new SimpleApiClient(hostAddress, repositoryUrl, new GitHubClient(productHeader, new SimpleCredentialStore(hostAddress), apiBaseUri), lazyEnterpriseProbe, lazyWikiProbe));
38-
}
39-
return cache[repositoryUrl];
40-
}
32+
var hostAddress = HostAddress.Create(repositoryUrl);
33+
return cache.GetOrAdd(repositoryUrl, new SimpleApiClient(hostAddress, repositoryUrl,
34+
new GitHubClient(productHeader, new SimpleCredentialStore(hostAddress), hostAddress.ApiUri),
35+
lazyEnterpriseProbe, lazyWikiProbe));
4136
}
4237

4338
public void ClearFromCache(ISimpleApiClient client)
4439
{
45-
lock (cache)
46-
{
47-
if (cache.ContainsKey(client.OriginalUrl))
48-
cache.Remove(client.OriginalUrl);
49-
}
40+
ISimpleApiClient c;
41+
cache.TryRemove(client.OriginalUrl, out c);
5042
}
5143
}
5244
}

0 commit comments

Comments
 (0)