Repository navigation
Expand file tree
/
Copy pathLocalizedEntityService.cs
More file actions
450 lines (378 loc) · 16.6 KB
/
Copy pathLocalizedEntityService.cs
File metadata and controls
450 lines (378 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using SmartStore.Core;
using SmartStore.Core.Caching;
using SmartStore.Core.Configuration;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Common;
using SmartStore.Core.Domain.Localization;
namespace SmartStore.Services.Localization
{
/// <summary>
/// Provides information about localizable entities
/// </summary>
public partial class LocalizedEntityService : ScopedServiceBase, ILocalizedEntityService
{
/// <summary>
/// 0 = segment (keygroup.key.idrange), 1 = language id
/// </summary>
const string LOCALIZEDPROPERTY_SEGMENT_KEY = "localizedproperty:{0}-lang-{1}";
const string LOCALIZEDPROPERTY_SEGMENT_PATTERN = "localizedproperty:{0}*";
const string LOCALIZEDPROPERTY_ALLSEGMENTS_PATTERN = "localizedproperty:*";
private readonly IRepository<LocalizedProperty> _localizedPropertyRepository;
private readonly ICacheManager _cacheManager;
private readonly PerformanceSettings _performanceSettings;
private readonly IDictionary<string, LocalizedPropertyCollection> _prefetchedCollections;
private static int _lastCacheSegmentSize = -1;
public LocalizedEntityService(
ICacheManager cacheManager,
IRepository<LocalizedProperty> localizedPropertyRepository,
PerformanceSettings performanceSettings)
{
_cacheManager = cacheManager;
_localizedPropertyRepository = localizedPropertyRepository;
_performanceSettings = performanceSettings;
_prefetchedCollections = new Dictionary<string, LocalizedPropertyCollection>(StringComparer.OrdinalIgnoreCase);
ValidateCacheState();
}
private void ValidateCacheState()
{
// Ensure that after a segment size change the cache segments are invalidated.
var size = _performanceSettings.CacheSegmentSize;
var changed = _lastCacheSegmentSize == -1;
if (size <= 0)
{
_performanceSettings.CacheSegmentSize = size = 1;
}
if (_lastCacheSegmentSize > 0 && _lastCacheSegmentSize != size)
{
OnClearCache();
changed = true;
}
if (changed)
{
Interlocked.Exchange(ref _lastCacheSegmentSize, size);
}
}
protected override void OnClearCache()
{
_cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_ALLSEGMENTS_PATTERN);
}
protected virtual IDictionary<int, string> GetCacheSegment(string localeKeyGroup, string localeKey, int entityId, int languageId)
{
Guard.NotEmpty(localeKeyGroup, nameof(localeKeyGroup));
Guard.NotEmpty(localeKey, nameof(localeKey));
var segmentKey = GetSegmentKeyPart(localeKeyGroup, localeKey, entityId, out var minEntityId, out var maxEntityId);
var cacheKey = BuildCacheSegmentKey(segmentKey, languageId);
// TODO: (MC) skip caching product.fulldescription (?), OR
// ...additionally segment by entity id ranges.
return _cacheManager.Get(cacheKey, () =>
{
var properties = _localizedPropertyRepository.TableUntracked
.Where(x => x.EntityId >= minEntityId && x.EntityId <= maxEntityId && x.LocaleKey == localeKey && x.LocaleKeyGroup == localeKeyGroup && x.LanguageId == languageId)
.ToList();
var dict = new Dictionary<int, string>(properties.Count);
foreach (var prop in properties)
{
dict[prop.EntityId] = prop.LocaleValue ?? string.Empty;
}
return dict;
});
}
/// <summary>
/// Clears the cached segment from the cache
/// </summary>
protected virtual void ClearCacheSegment(string localeKeyGroup, string localeKey, int entityId, int? languageId = null)
{
try
{
if (IsInScope)
return;
var segmentKey = GetSegmentKeyPart(localeKeyGroup, localeKey, entityId);
if (languageId.HasValue && languageId.Value > 0)
{
_cacheManager.Remove(BuildCacheSegmentKey(segmentKey, languageId.Value));
}
else
{
_cacheManager.RemoveByPattern(LOCALIZEDPROPERTY_SEGMENT_PATTERN.FormatInvariant(segmentKey));
}
}
catch { }
}
public virtual string GetLocalizedValue(int languageId, int entityId, string localeKeyGroup, string localeKey)
{
if (_prefetchedCollections.TryGetValue(localeKeyGroup, out var collection))
{
var cachedItem = collection.Find(languageId, entityId, localeKey);
if (cachedItem != null)
{
return cachedItem.LocaleValue;
}
}
if (IsInScope)
{
return GetLocalizedValueUncached(languageId, entityId, localeKeyGroup, localeKey);
}
if (languageId <= 0)
return string.Empty;
var props = GetCacheSegment(localeKeyGroup, localeKey, entityId, languageId);
if (!props.TryGetValue(entityId, out var val))
{
return string.Empty;
}
return val;
}
protected string GetLocalizedValueUncached(int languageId, int entityId, string localeKeyGroup, string localeKey)
{
if (languageId <= 0)
return string.Empty;
var query = from lp in _localizedPropertyRepository.TableUntracked
where
lp.EntityId == entityId &&
lp.LocaleKey == localeKey &&
lp.LocaleKeyGroup == localeKeyGroup &&
lp.LanguageId == languageId
select lp.LocaleValue;
return query.FirstOrDefault().EmptyNull();
}
public virtual IList<LocalizedProperty> GetLocalizedProperties(int entityId, string localeKeyGroup)
{
if (localeKeyGroup.IsEmpty())
return new List<LocalizedProperty>();
var query = from x in _localizedPropertyRepository.Table
orderby x.Id
where x.EntityId == entityId && x.LocaleKeyGroup == localeKeyGroup
select x;
var props = query.ToList();
return props;
}
public virtual void PrefetchLocalizedProperties(string localeKeyGroup, int languageId, int[] entityIds, bool isRange = false, bool isSorted = false)
{
if (languageId == 0)
return;
var collection = GetLocalizedPropertyCollectionInternal(localeKeyGroup, languageId, entityIds, isRange, isSorted);
if (_prefetchedCollections.TryGetValue(localeKeyGroup, out var existing))
{
collection.MergeWith(existing);
}
else
{
_prefetchedCollections[localeKeyGroup] = collection;
}
}
public virtual LocalizedPropertyCollection GetLocalizedPropertyCollection(string localeKeyGroup, int[] entityIds, bool isRange = false, bool isSorted = false)
{
return GetLocalizedPropertyCollectionInternal(localeKeyGroup, 0, entityIds, isRange, isSorted);
}
protected virtual LocalizedPropertyCollection GetLocalizedPropertyCollectionInternal(string localeKeyGroup, int languageId, int[] entityIds, bool isRange = false, bool isSorted = false)
{
Guard.NotEmpty(localeKeyGroup, nameof(localeKeyGroup));
using (new DbContextScope(proxyCreation: false, lazyLoading: false))
{
var query = from x in _localizedPropertyRepository.TableUntracked
where x.LocaleKeyGroup == localeKeyGroup
select x;
var splitEntityIds = false;
var requestedSet = entityIds;
if (entityIds != null && entityIds.Length > 0)
{
if (isRange)
{
if (!isSorted)
{
Array.Sort(entityIds);
}
var min = entityIds[0];
var max = entityIds[entityIds.Length - 1];
if (entityIds.Length == 2 && max > min + 1)
{
// Only min & max were passed, create the range sequence.
requestedSet = Enumerable.Range(min, max - min + 1).ToArray();
}
query = query.Where(x => x.EntityId >= min && x.EntityId <= max);
}
else
{
requestedSet = entityIds;
if (entityIds.Length > 5000)
{
splitEntityIds = true;
}
else
{
query = query.Where(x => entityIds.Contains(x.EntityId));
}
}
}
if (languageId > 0)
{
query = query.Where(x => x.LanguageId == languageId);
}
if (splitEntityIds)
{
var items = new List<LocalizedProperty>();
foreach (var chunk in entityIds.Slice(5000))
{
items.AddRange(query.Where(x => chunk.Contains(x.EntityId)).ToList());
}
return new LocalizedPropertyCollection(localeKeyGroup, requestedSet, items);
}
else
{
return new LocalizedPropertyCollection(localeKeyGroup, requestedSet, query.ToList());
}
}
}
protected virtual LocalizedProperty GetLocalizedProperty(int languageId, int entityId, string localeKeyGroup, string localeKey)
{
var query = from lp in _localizedPropertyRepository.Table
where
lp.EntityId == entityId &&
lp.LocaleKey == localeKey &&
lp.LocaleKeyGroup == localeKeyGroup &&
lp.LanguageId == languageId
select lp;
return query.FirstOrDefault();
}
public virtual void InsertLocalizedProperty(LocalizedProperty property)
{
Guard.NotNull(property, nameof(property));
// db
_localizedPropertyRepository.Insert(property);
HasChanges = true;
// cache
ClearCacheSegment(property.LocaleKeyGroup, property.LocaleKey, property.EntityId, property.LanguageId);
}
public virtual void UpdateLocalizedProperty(LocalizedProperty property)
{
Guard.NotNull(property, nameof(property));
// db
_localizedPropertyRepository.Update(property);
HasChanges = true;
// cache
ClearCacheSegment(property.LocaleKeyGroup, property.LocaleKey, property.EntityId, property.LanguageId);
}
public virtual void DeleteLocalizedProperty(LocalizedProperty property)
{
Guard.NotNull(property, nameof(property));
// cache
ClearCacheSegment(property.LocaleKeyGroup, property.LocaleKey, property.EntityId, property.LanguageId);
// db
_localizedPropertyRepository.Delete(property);
HasChanges = true;
}
public virtual LocalizedProperty GetLocalizedPropertyById(int localizedPropertyId)
{
if (localizedPropertyId == 0)
return null;
var localizedProperty = _localizedPropertyRepository.GetById(localizedPropertyId);
return localizedProperty;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void SaveLocalizedValue<T>(
T entity,
Expression<Func<T, string>> keySelector,
string value,
int languageId) where T : BaseEntity, ILocalizedEntity
{
SaveLocalizedValue(entity, entity.Id, entity.GetEntityName(), keySelector, value, languageId);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void SaveLocalizedValue<T, TPropType>(
T entity,
Expression<Func<T, TPropType>> keySelector,
TPropType value,
int languageId) where T : BaseEntity, ILocalizedEntity
{
SaveLocalizedValue(entity, entity.Id, entity.GetEntityName(), keySelector, value, languageId);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void SaveLocalizedSetting<TSetting, TPropType>(
TSetting settings,
Expression<Func<TSetting, TPropType>> keySelector,
TPropType value,
int languageId,
int storeId = 0) where TSetting : class, ISettings
{
// INFO: unfortunately we have to misuse the "EntityId" prop and store StoreId instead.
SaveLocalizedValue(settings, storeId, typeof(TSetting).Name, keySelector, value, languageId);
}
protected virtual void SaveLocalizedValue<T, TPropType>(
T obj,
int id, // T is BaseEntity = EntityId, T is ISetting = StoreId
string keyGroup,
Expression<Func<T, TPropType>> keySelector,
TPropType value,
int languageId) where T : class
{
Guard.NotNull(obj, nameof(obj));
Guard.NotEmpty(keyGroup, nameof(keyGroup));
Guard.NotZero(languageId, nameof(languageId));
var member = keySelector.Body as MemberExpression;
if (member == null)
{
throw new ArgumentException($"Expression '{keySelector}' refers to a method, not a property.");
}
var propInfo = member.Member as PropertyInfo;
if (propInfo == null)
{
throw new ArgumentException($"Expression '{keySelector}' refers to a field, not a property.");
}
var key = propInfo.Name;
var valueStr = value.Convert<string>();
var prop = GetLocalizedProperty(languageId, id, keyGroup, key);
if (prop != null)
{
if (string.IsNullOrEmpty(valueStr))
{
// Delete
DeleteLocalizedProperty(prop);
}
else
{
// Update
if (prop.LocaleValue != valueStr)
{
prop.LocaleValue = valueStr;
UpdateLocalizedProperty(prop);
}
}
}
else
{
if (!string.IsNullOrEmpty(valueStr))
{
// insert
prop = new LocalizedProperty
{
EntityId = id,
LanguageId = languageId,
LocaleKey = key,
LocaleKeyGroup = keyGroup,
LocaleValue = valueStr
};
InsertLocalizedProperty(prop);
}
}
}
private string BuildCacheSegmentKey(string segment, int languageId)
{
return String.Format(LOCALIZEDPROPERTY_SEGMENT_KEY, segment, languageId);
}
private string GetSegmentKeyPart(string localeKeyGroup, string localeKey, int entityId)
{
return GetSegmentKeyPart(localeKeyGroup, localeKey, entityId, out _, out _);
}
private string GetSegmentKeyPart(string localeKeyGroup, string localeKey, int entityId, out int minId, out int maxId)
{
(minId, maxId) = entityId.GetRange(_performanceSettings.CacheSegmentSize);
return (localeKeyGroup + "." + localeKey + "." + minId.ToString()).ToLowerInvariant();
}
}
}