21
21
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
22
22
using System . Management . Automation . Runspaces ;
23
23
using System . Collections ;
24
+ using System . Collections . Concurrent ;
24
25
25
26
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
26
27
{
@@ -38,7 +39,7 @@ public class Helper
38
39
private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
39
40
private Dictionary < string , Dictionary < string , object > > ruleArguments ;
40
41
private PSVersionTable psVersionTable ;
41
- private Dictionary < string , CommandInfo > commandInfoCache ;
42
+ private ConcurrentDictionary < string , CommandInfo > commandInfoCache ;
42
43
private RunspacePool runspacePool ;
43
44
44
45
#endregion
@@ -148,7 +149,7 @@ public void Initialize()
148
149
KeywordBlockDictionary = new Dictionary < String , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
149
150
VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
150
151
ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
151
- commandInfoCache = new Dictionary < string , CommandInfo > ( StringComparer . OrdinalIgnoreCase ) ;
152
+ commandInfoCache = new ConcurrentDictionary < string , CommandInfo > ( StringComparer . OrdinalIgnoreCase ) ;
152
153
runspacePool = RunspaceFactory . CreateRunspacePool ( InitialSessionState . CreateDefault2 ( ) ) ;
153
154
runspacePool . Open ( ) ;
154
155
@@ -740,17 +741,14 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
740
741
cmdletName = name ;
741
742
}
742
743
743
- lock ( getCommandLock )
744
+ if ( commandInfoCache . ContainsKey ( cmdletName ) )
744
745
{
745
- if ( commandInfoCache . ContainsKey ( cmdletName ) )
746
- {
747
- return commandInfoCache [ cmdletName ] ;
748
- }
749
-
750
- var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
751
- commandInfoCache . Add ( cmdletName , commandInfo ) ;
752
- return commandInfo ;
746
+ return commandInfoCache [ cmdletName ] ;
753
747
}
748
+
749
+ var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
750
+ commandInfoCache . AddOrUpdate ( cmdletName , ( key ) => commandInfo , ( key , value ) => commandInfo ) ;
751
+ return commandInfo ;
754
752
}
755
753
756
754
/// <summary>
0 commit comments