Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Commit 0072229

Browse files
committed
Cache fixes for ResourceManagerStringLocalizer culture walk:
- Make the cache static - Make the cache key include the assembly name & the resource name - #15
1 parent 1d6b63a commit 0072229

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/Microsoft.Framework.Localization/ResourceManagerStringLocalizer.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ public class ResourceManagerStringLocalizer : IStringLocalizer
2121
private readonly ConcurrentDictionary<string, object> _missingManifestCache =
2222
new ConcurrentDictionary<string, object>();
2323

24-
private readonly ConcurrentDictionary<string, IList<string>> _resourceNamesCache =
24+
private static readonly ConcurrentDictionary<string, IList<string>> _resourceNamesCache =
2525
new ConcurrentDictionary<string, IList<string>>();
26-
27-
26+
2827
/// <summary>
2928
/// Creates a new <see cref="ResourceManagerStringLocalizer"/>.
3029
/// </summary>
@@ -188,7 +187,9 @@ private IList<string> GetResourceNamesForCulture(CultureInfo culture)
188187
}
189188
resourceStreamName += ".resources";
190189

191-
var cultureResourceNames = _resourceNamesCache.GetOrAdd(resourceStreamName, key =>
190+
var cacheKey = $"assembly={ResourceAssembly.FullName};resourceStreamName={resourceStreamName}";
191+
192+
var cultureResourceNames = _resourceNamesCache.GetOrAdd(cacheKey, key =>
192193
{
193194
var names = new List<string>();
194195
using (var cultureResourceStream = ResourceAssembly.GetManifestResourceStream(key))

test/Microsoft.Framework.Localization.Test/ResourceManagerStringLocalizerTest.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ public void EnumeratorCachesCultureWalk()
2222
resourceAssembly.Setup(rm => rm.GetManifestResourceStream(It.IsAny<string>()))
2323
.Returns(() => MakeResourceStream());
2424
var baseName = "test";
25-
var localizer = new ResourceManagerStringLocalizer(
25+
var localizer1 = new ResourceManagerStringLocalizer(
26+
resourceManager.Object,
27+
resourceAssembly.Object,
28+
baseName);
29+
var localizer2 = new ResourceManagerStringLocalizer(
2630
resourceManager.Object,
2731
resourceAssembly.Object,
2832
baseName);
2933

3034
// Act
3135
for (int i = 0; i < 5; i++)
3236
{
33-
localizer.ToList();
37+
localizer1.ToList();
38+
localizer2.ToList();
3439
}
3540

3641
// Assert

0 commit comments

Comments
 (0)