Skip to content

Commit f8c5d8b

Browse files
committed
(Perf) implemented static cache for localized properties, which are all loaded on app startup now. To disable this feature, set 'LocalizationSettings.LoadAllLocalizedPropertiesOnStartup' to false (true by default)
1 parent bf90e32 commit f8c5d8b

4 files changed

Lines changed: 58 additions & 19 deletions

File tree

src/Libraries/SmartStore.Core/Domain/Localization/LocalizationSettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public LocalizationSettings()
99
UseImagesForLanguageSelection = true;
1010
DefaultLanguageRedirectBehaviour = DefaultLanguageRedirectBehaviour.StripSeoCode;
1111
InvalidLanguageRedirectBehaviour = InvalidLanguageRedirectBehaviour.ReturnHttp404;
12+
LoadAllLocalizedPropertiesOnStartup = true;
1213
}
1314

1415
/// <summary>
@@ -26,6 +27,11 @@ public LocalizationSettings()
2627
/// </summary>
2728
public bool LoadAllLocaleRecordsOnStartup { get; set; }
2829

30+
/// <summary>
31+
/// A value indicating whether to load all localized entity properties on application startup
32+
/// </summary>
33+
public bool LoadAllLocalizedPropertiesOnStartup { get; set; }
34+
2935
/// <summary>
3036
/// A value indicating whether the browser user lannguage should be detected
3137
/// </summary>

src/Libraries/SmartStore.Data/Migrations/201504171629262_V22Final.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void MigrateLocaleResources(LocaleResourcesBuilder builder)
9090
builder.AddOrUpdate("Admin.Common.UnknownError",
9191
"An unknown error has occurred.",
9292
"Es ist ein unbekannter Fehler aufgetreten.");
93-
93+
9494
builder.AddOrUpdate("Plugins.Feed.FreeShippingThreshold",
9595
"Free shipping threshold",
9696
"Kostenloser Versand ab",

src/Libraries/SmartStore.Data/Migrations/201506211043073_PaymentShippingRestrictions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public bool RollbackOnFailure
4444
public void Seed(SmartObjectContext context)
4545
{
4646
context.MigrateLocaleResources(MigrateLocaleResources);
47+
48+
context.MigrateSettings(x =>
49+
{
50+
x.Add("localizationsettings.loadalllocalizedpropertiesonstartup", true);
51+
});
4752
}
4853

4954
public void MigrateLocaleResources(LocaleResourcesBuilder builder)

src/Libraries/SmartStore.Services/Localization/LocalizedEntityService.cs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using SmartStore.Core.Caching;
88
using SmartStore.Core.Data;
99
using SmartStore.Core.Domain.Localization;
10+
using SmartStore.Core.Infrastructure;
1011

1112
namespace SmartStore.Services.Localization
1213
{
@@ -18,7 +19,7 @@ public partial class LocalizedEntityService : ILocalizedEntityService
1819
#region Constants
1920

2021
private const string LOCALIZEDPROPERTY_KEY = "SmartStore.localizedproperty.value-{0}-{1}-{2}-{3}";
21-
private const string LOCALIZEDPROPERTY_ENTITYID_KEY = "SmartStore.localizedproperty.entityid-{0}-{1}-{2}";
22+
private const string LOCALIZEDPROPERTY_ALL_KEY = "SmartStore.localizedproperty.all";
2223
private const string LOCALIZEDPROPERTY_PATTERN_KEY = "SmartStore.localizedproperty.";
2324

2425
#endregion
@@ -27,6 +28,7 @@ public partial class LocalizedEntityService : ILocalizedEntityService
2728

2829
private readonly IRepository<LocalizedProperty> _localizedPropertyRepository;
2930
private readonly ICacheManager _cacheManager;
31+
private readonly LocalizationSettings _localizationSettings;
3032

3133
#endregion
3234

@@ -37,11 +39,11 @@ public partial class LocalizedEntityService : ILocalizedEntityService
3739
/// </summary>
3840
/// <param name="cacheManager">Cache manager</param>
3941
/// <param name="localizedPropertyRepository">Localized property repository</param>
40-
public LocalizedEntityService(ICacheManager cacheManager,
41-
IRepository<LocalizedProperty> localizedPropertyRepository)
42+
public LocalizedEntityService(ICacheManager cacheManager, IRepository<LocalizedProperty> localizedPropertyRepository, LocalizationSettings localizationSettings)
4243
{
4344
this._cacheManager = cacheManager;
4445
this._localizedPropertyRepository = localizedPropertyRepository;
46+
this._localizationSettings = localizationSettings;
4547
}
4648

4749
#endregion
@@ -87,21 +89,42 @@ public virtual LocalizedProperty GetLocalizedPropertyById(int localizedPropertyI
8789
/// <returns>Found localized value</returns>
8890
public virtual string GetLocalizedValue(int languageId, int entityId, string localeKeyGroup, string localeKey)
8991
{
90-
string key = string.Format(LOCALIZEDPROPERTY_KEY, languageId, entityId, localeKeyGroup, localeKey);
91-
return _cacheManager.Get(key, () =>
92-
{
93-
var query = from lp in _localizedPropertyRepository.Table
94-
where lp.EntityId == entityId &&
95-
lp.LocaleKey == localeKey &&
96-
lp.LocaleKeyGroup == localeKeyGroup &&
97-
lp.LanguageId == languageId
98-
select lp.LocaleValue;
99-
var localeValue = query.FirstOrDefault();
100-
//little hack here. nulls aren't cacheable so set it to ""
101-
if (localeValue == null)
102-
localeValue = "";
103-
return localeValue;
104-
});
92+
string val = null;
93+
94+
if (_localizationSettings.LoadAllLocalizedPropertiesOnStartup)
95+
{
96+
var allValues = _cacheManager.Get(LOCALIZEDPROPERTY_ALL_KEY, () =>
97+
{
98+
var result = _localizedPropertyRepository.TableUntracked.ToDictionary(
99+
x => GenerateKey(x.LanguageId, x.LocaleKeyGroup, x.LocaleKey, x.EntityId),
100+
x => x.LocaleValue);
101+
return result;
102+
});
103+
104+
string key = GenerateKey(languageId, localeKeyGroup, localeKey, entityId);
105+
106+
if (!allValues.TryGetValue(key, out val))
107+
{
108+
return string.Empty;
109+
}
110+
}
111+
else
112+
{
113+
string cacheKey = string.Format(LOCALIZEDPROPERTY_KEY, languageId, entityId, localeKeyGroup, localeKey);
114+
val = _cacheManager.Get(cacheKey, () =>
115+
{
116+
var query = from lp in _localizedPropertyRepository.TableUntracked
117+
where lp.EntityId == entityId &&
118+
lp.LocaleKey == localeKey &&
119+
lp.LocaleKeyGroup == localeKeyGroup &&
120+
lp.LanguageId == languageId
121+
select lp.LocaleValue;
122+
123+
return query.FirstOrDefault().EmptyNull();
124+
});
125+
}
126+
127+
return val;
105128
}
106129

107130
/// <summary>
@@ -238,6 +261,11 @@ public virtual void SaveLocalizedValue<T, TPropType>(T entity,
238261
}
239262
}
240263

264+
private string GenerateKey(int languageId, string localeKeyGroup, string localeKey, int entityId)
265+
{
266+
return "{0}.{1}.{2}.{3}".FormatInvariant(languageId, localeKeyGroup, localeKey, entityId);
267+
}
268+
241269
#endregion
242270
}
243271
}

0 commit comments

Comments
 (0)