5
5
using GitHub . Primitives ;
6
6
using GitHub . Services ;
7
7
using Octokit ;
8
+ using System . Collections . Concurrent ;
8
9
9
10
namespace GitHub . Api
10
11
{
@@ -16,7 +17,7 @@ public class SimpleApiClientFactory : ISimpleApiClientFactory
16
17
readonly Lazy < IEnterpriseProbeTask > lazyEnterpriseProbe ;
17
18
readonly Lazy < IWikiProbe > lazyWikiProbe ;
18
19
19
- static readonly Dictionary < UriString , ISimpleApiClient > cache = new Dictionary < UriString , ISimpleApiClient > ( ) ;
20
+ static readonly ConcurrentDictionary < UriString , ISimpleApiClient > cache = new ConcurrentDictionary < UriString , ISimpleApiClient > ( ) ;
20
21
21
22
[ ImportingConstructor ]
22
23
public SimpleApiClientFactory ( IProgram program , Lazy < IEnterpriseProbeTask > enterpriseProbe , Lazy < IWikiProbe > wikiProbe )
@@ -28,25 +29,16 @@ public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enter
28
29
29
30
public ISimpleApiClient Create ( UriString repositoryUrl )
30
31
{
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 ) ) ;
41
36
}
42
37
43
38
public void ClearFromCache ( ISimpleApiClient client )
44
39
{
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 ) ;
50
42
}
51
43
}
52
44
}
0 commit comments