77using SmartStore . Core . Caching ;
88using SmartStore . Core . Data ;
99using SmartStore . Core . Domain . Localization ;
10+ using SmartStore . Core . Infrastructure ;
1011
1112namespace 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