Skip to content

Commit 951c1c6

Browse files
perf(CacheManager): remove lock improve performance (#5154)
* doc: 移除注释 * refactor: 移除锁提高性能 Co-Authored-By: Alex chow <[email protected]>
1 parent d0dd670 commit 951c1c6

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/BootstrapBlazor/Localization/Json/JsonStringLocalizerFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ void OnChange(BootstrapBlazorOptions op)
7373
/// </summary>
7474
/// <param name="typeInfo"></param>
7575
/// <returns></returns>
76-
/// <exception cref="InvalidOperationException"></exception>
7776
protected override string GetResourcePrefix(TypeInfo typeInfo)
7877
{
7978
var typeName = typeInfo.FullName;

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,6 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
210210
public static IEnumerable<LocalizedString>? GetAllStringsByTypeName(Assembly assembly, string typeName)
211211
=> GetJsonStringByTypeName(GetJsonLocalizationOption(), assembly, typeName, CultureInfo.CurrentUICulture.Name);
212212

213-
#if NET9_0_OR_GREATER
214-
private static readonly Lock _locker = new();
215-
#else
216-
private static readonly object _locker = new();
217-
#endif
218-
219213
/// <summary>
220214
/// 通过指定程序集获取所有本地化信息键值集合
221215
/// </summary>
@@ -240,27 +234,24 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
240234
Instance.Cache.Remove(key);
241235
}
242236

243-
lock (_locker)
237+
localizedItems = Instance.GetOrCreate(key, _ =>
244238
{
245-
localizedItems = Instance.GetOrCreate(key, _ =>
239+
var sections = option.GetJsonStringFromAssembly(assembly, cultureName);
240+
var items = sections.SelectMany(section => section.GetChildren().Select(kv =>
246241
{
247-
var sections = option.GetJsonStringFromAssembly(assembly, cultureName);
248-
var items = sections.SelectMany(section => section.GetChildren().Select(kv =>
242+
var value = kv.Value;
243+
if (value == null && option.UseKeyWhenValueIsNull == true)
249244
{
250-
var value = kv.Value;
251-
if (value == null && option.UseKeyWhenValueIsNull == true)
252-
{
253-
value = kv.Key;
254-
}
255-
return new LocalizedString(kv.Key, value ?? "", false, section.Key);
256-
}));
245+
value = kv.Key;
246+
}
247+
return new LocalizedString(kv.Key, value ?? "", false, section.Key);
248+
}));
257249
#if NET8_0_OR_GREATER
258-
return items.ToFrozenSet();
250+
return items.ToFrozenSet();
259251
#else
260-
return items.ToHashSet();
252+
return items.ToHashSet();
261253
#endif
262-
});
263-
}
254+
});
264255
return localizedItems.Where(item => item.SearchedLocation!.Equals(typeName, StringComparison.OrdinalIgnoreCase));
265256
}
266257

0 commit comments

Comments
 (0)