From 29ec1aa4ad789675e4fe3bb6588c2bb85e19646b Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 26 Mar 2021 18:14:32 +0800 Subject: [PATCH] Add ConcurrentDictionary GetOrAdd extension methods --- .../Generic/AbpDictionaryExtensions.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpDictionaryExtensions.cs b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpDictionaryExtensions.cs index 2d2ae9d6dc..148b34fa90 100644 --- a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpDictionaryExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpDictionaryExtensions.cs @@ -67,7 +67,7 @@ namespace System.Collections.Generic { return dictionary.TryGetValue(key, out var obj) ? obj : default; } - + /// /// Gets a value from the dictionary with given key. Returns default value if can not find. /// @@ -100,7 +100,7 @@ namespace System.Collections.Generic return dictionary[key] = factory(key); } - + /// /// Gets a value from the dictionary with given key. Returns default value if can not find. /// @@ -114,5 +114,19 @@ namespace System.Collections.Generic { return dictionary.GetOrAdd(key, k => factory()); } + + /// + /// Gets a value from the concurrent dictionary with given key. Returns default value if can not find. + /// + /// Concurrent dictionary to check and get + /// Key to find the value + /// A factory method used to create the value if not found in the dictionary + /// Type of the key + /// Type of the value + /// Value if found, default if can not found. + public static TValue GetOrAdd(this ConcurrentDictionary dictionary, TKey key, Func factory) + { + return dictionary.GetOrAdd(key, k => factory()); + } } -} \ No newline at end of file +}