55using GitHub . Primitives ;
66using GitHub . Services ;
77using Octokit ;
8+ using System . Collections . Concurrent ;
89
910namespace 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