Skip to content

Commit

Permalink
perf(CacheManager): remove lock improve performance (#5154)
Browse files Browse the repository at this point in the history
* doc: 移除注释

* refactor: 移除锁提高性能

Co-Authored-By: Alex chow <[email protected]>
  • Loading branch information
ArgoZhang and densen2014 authored Jan 19, 2025
1 parent d0dd670 commit 951c1c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void OnChange(BootstrapBlazorOptions op)
/// </summary>
/// <param name="typeInfo"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
protected override string GetResourcePrefix(TypeInfo typeInfo)
{
var typeName = typeInfo.FullName;
Expand Down
33 changes: 12 additions & 21 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
public static IEnumerable<LocalizedString>? GetAllStringsByTypeName(Assembly assembly, string typeName)
=> GetJsonStringByTypeName(GetJsonLocalizationOption(), assembly, typeName, CultureInfo.CurrentUICulture.Name);

#if NET9_0_OR_GREATER
private static readonly Lock _locker = new();
#else
private static readonly object _locker = new();
#endif

/// <summary>
/// 通过指定程序集获取所有本地化信息键值集合
/// </summary>
Expand All @@ -240,27 +234,24 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
Instance.Cache.Remove(key);
}

lock (_locker)
localizedItems = Instance.GetOrCreate(key, _ =>
{
localizedItems = Instance.GetOrCreate(key, _ =>
var sections = option.GetJsonStringFromAssembly(assembly, cultureName);
var items = sections.SelectMany(section => section.GetChildren().Select(kv =>
{
var sections = option.GetJsonStringFromAssembly(assembly, cultureName);
var items = sections.SelectMany(section => section.GetChildren().Select(kv =>
var value = kv.Value;
if (value == null && option.UseKeyWhenValueIsNull == true)
{
var value = kv.Value;
if (value == null && option.UseKeyWhenValueIsNull == true)
{
value = kv.Key;
}
return new LocalizedString(kv.Key, value ?? "", false, section.Key);
}));
value = kv.Key;
}
return new LocalizedString(kv.Key, value ?? "", false, section.Key);
}));
#if NET8_0_OR_GREATER
return items.ToFrozenSet();
return items.ToFrozenSet();
#else
return items.ToHashSet();
return items.ToHashSet();
#endif
});
}
});
return localizedItems.Where(item => item.SearchedLocation!.Equals(typeName, StringComparison.OrdinalIgnoreCase));
}

Expand Down

0 comments on commit 951c1c6

Please sign in to comment.