PERF: Fixing 16889 IText pooling

--HG--
branch : perf
This commit is contained in:
Suha Can
2010-12-01 15:33:04 -08:00
parent 733de4a166
commit 2a0c34bcab
2 changed files with 15 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
namespace Orchard.Localization { namespace Orchard.Localization {
public interface IText { public interface IText : ISingletonDependency {
LocalizedString Get(string textHint, params object[] args); LocalizedString Get(string textHint, params object[] args);
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Autofac; using Autofac;
using Autofac.Core; using Autofac.Core;
@@ -6,6 +7,11 @@ using Module = Autofac.Module;
namespace Orchard.Localization { namespace Orchard.Localization {
public class LocalizationModule : Module { public class LocalizationModule : Module {
private readonly IDictionary<string, Localizer> _localizerCache;
public LocalizationModule() {
_localizerCache = new Dictionary<string, Localizer>();
}
protected override void Load(ContainerBuilder builder) { protected override void Load(ContainerBuilder builder) {
builder.RegisterType<Text>().As<IText>().InstancePerDependency(); builder.RegisterType<Text>().As<IText>().InstancePerDependency();
@@ -19,8 +25,14 @@ namespace Orchard.Localization {
var scope = registration.Activator.LimitType.FullName; var scope = registration.Activator.LimitType.FullName;
registration.Activated += (sender, e) => { registration.Activated += (sender, e) => {
var localizer = LocalizationUtilities.Resolve(e.Context, scope); if (_localizerCache.ContainsKey(scope)) {
userProperty.SetValue(e.Instance, localizer, null); userProperty.SetValue(e.Instance, _localizerCache[scope], null);
}
else {
var localizer = LocalizationUtilities.Resolve(e.Context, scope);
_localizerCache.Add(scope, localizer);
userProperty.SetValue(e.Instance, localizer, null);
}
}; };
} }
} }