From 96462b9528da1b01d5f1759f28e49f2fb7cb2d7d Mon Sep 17 00:00:00 2001 From: damianedwards Date: Mon, 7 Dec 2015 18:15:20 -0800 Subject: [PATCH] Make the ResourceManagerStringLocalizerFactory cache localizer instances - #156 --- .../ResourceManagerStringLocalizer.cs | 7 ++--- .../ResourceManagerStringLocalizerFactory.cs | 30 +++++++++++-------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs index 41009e64ce..be93329178 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs @@ -3,7 +3,6 @@ using System; using System.Collections; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; using System.Reflection; @@ -18,9 +17,7 @@ namespace Microsoft.Extensions.Localization /// public class ResourceManagerStringLocalizer : IStringLocalizer { - private readonly ConcurrentDictionary _missingManifestCache = - new ConcurrentDictionary(); - + private readonly Dictionary _missingManifestCache = new Dictionary(); private readonly IResourceNamesCache _resourceNamesCache; private readonly ResourceManager _resourceManager; private readonly AssemblyWrapper _resourceAssemblyWrapper; @@ -188,7 +185,7 @@ namespace Microsoft.Extensions.Localization } catch (MissingManifestResourceException) { - _missingManifestCache.TryAdd(cacheKey, null); + _missingManifestCache.Add(cacheKey, null); return null; } } diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs index a82f22bd56..4e3d721bd0 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs @@ -2,11 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Concurrent; using System.IO; using System.Reflection; using System.Resources; -using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.OptionsModel; +using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.Extensions.Localization { @@ -16,7 +17,8 @@ namespace Microsoft.Extensions.Localization public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory { private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache(); - + private readonly ConcurrentDictionary _localizerCache = + new ConcurrentDictionary(); private readonly IApplicationEnvironment _applicationEnvironment; private readonly string _resourcesRelativePath; @@ -67,11 +69,13 @@ namespace Microsoft.Extensions.Localization var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + typeInfo.FullName; - return new ResourceManagerStringLocalizer( - new ResourceManager(baseName, assembly), - assembly, - baseName, - _resourceNamesCache); + return _localizerCache.GetOrAdd(baseName, _ => + new ResourceManagerStringLocalizer( + new ResourceManager(baseName, assembly), + assembly, + baseName, + _resourceNamesCache) + ); } /// @@ -91,11 +95,13 @@ namespace Microsoft.Extensions.Localization var assembly = Assembly.Load(new AssemblyName(rootPath)); baseName = rootPath + "." + _resourcesRelativePath + baseName; - return new ResourceManagerStringLocalizer( - new ResourceManager(baseName, assembly), - assembly, - baseName, - _resourceNamesCache); + return _localizerCache.GetOrAdd(baseName, _ => + new ResourceManagerStringLocalizer( + new ResourceManager(baseName, assembly), + assembly, + baseName, + _resourceNamesCache) + ); } } } \ No newline at end of file