2121using Microsoft . Windows . PowerShell . ScriptAnalyzer . Generic ;
2222using System . Management . Automation . Runspaces ;
2323using System . Collections ;
24+ using System . Collections . Concurrent ;
2425
2526namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
2627{
@@ -38,7 +39,7 @@ public class Helper
3839 private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
3940 private Dictionary < string , Dictionary < string , object > > ruleArguments ;
4041 private PSVersionTable psVersionTable ;
41- private Dictionary < string , CommandInfo > commandInfoCache ;
42+ private ConcurrentDictionary < string , CommandInfo > commandInfoCache ;
4243 private RunspacePool runspacePool ;
4344
4445 #endregion
@@ -148,7 +149,7 @@ public void Initialize()
148149 KeywordBlockDictionary = new Dictionary < String , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
149150 VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
150151 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 ) ;
152153 runspacePool = RunspaceFactory . CreateRunspacePool ( InitialSessionState . CreateDefault2 ( ) ) ;
153154 runspacePool . Open ( ) ;
154155
@@ -740,17 +741,14 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
740741 cmdletName = name ;
741742 }
742743
743- lock ( getCommandLock )
744+ if ( commandInfoCache . ContainsKey ( cmdletName ) )
744745 {
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 ] ;
753747 }
748+
749+ var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
750+ commandInfoCache . AddOrUpdate ( cmdletName , ( key ) => commandInfo , ( key , value ) => commandInfo ) ;
751+ return commandInfo ;
754752 }
755753
756754 /// <summary>
0 commit comments