mirror of
https://github.com/microsoft/PowerToys
synced 2024-11-22 16:46:57 +00:00
[PTRun]Lock mechanism added to GetLocalizedName (#27550)
* [PTRun] Lock mechanism added to GetLocalizedName. * [PTRun] Dictionary replaced with ConcurrentDictionary instead of using lock. * [PTRun] Ignoring TryAdd function result.
This commit is contained in:
parent
a71411d931
commit
eb91e8977e
@ -2,7 +2,7 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using Wox.Plugin.Common.Interfaces;
|
||||
using Wox.Plugin.Common.Win32;
|
||||
@ -15,7 +15,7 @@ namespace Wox.Plugin.Common
|
||||
public class ShellLocalization
|
||||
{
|
||||
// Cache for already localized names. This makes localization of already localized string faster.
|
||||
private Dictionary<string, string> _localizationCache = new Dictionary<string, string>();
|
||||
private ConcurrentDictionary<string, string> _localizationCache = new ConcurrentDictionary<string, string>();
|
||||
|
||||
/// <summary>
|
||||
/// Returns the localized name of a shell item.
|
||||
@ -24,8 +24,10 @@ namespace Wox.Plugin.Common
|
||||
/// <returns>The localized name as string or <see cref="string.Empty"/>.</returns>
|
||||
public string GetLocalizedName(string path)
|
||||
{
|
||||
string lowerInvariantPath = path.ToLowerInvariant();
|
||||
|
||||
// Checking cache if path is already localized
|
||||
if (_localizationCache.TryGetValue(path.ToLowerInvariant(), out string value))
|
||||
if (_localizationCache.TryGetValue(lowerInvariantPath, out string value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
@ -39,12 +41,7 @@ namespace Wox.Plugin.Common
|
||||
|
||||
shellItem.GetDisplayName(SIGDN.NORMALDISPLAY, out string filename);
|
||||
|
||||
if (!_localizationCache.ContainsKey(path.ToLowerInvariant()))
|
||||
{
|
||||
// The if condition is required to not get timing problems when called from an parallel execution.
|
||||
// Without the check we will get "key exists" exceptions.
|
||||
_localizationCache.Add(path.ToLowerInvariant(), filename);
|
||||
}
|
||||
_ = _localizationCache.TryAdd(lowerInvariantPath, filename);
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user