Skip to content

Commit a89932c

Browse files
committed
Optimized Cache
1 parent a107ab4 commit a89932c

File tree

3 files changed

+8
-33
lines changed

3 files changed

+8
-33
lines changed

Localization.AspNetCore.EntityFramework.Test/Controllers/HomeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public HomeController(ILogger<HomeController> logger,
2424

2525
public IActionResult Index()
2626
{
27-
var z = _localizer["Test3"];
27+
var z = _localizer["Test5"];
2828
return View();
2929
}
3030

Localization.AspNetCore.EntityFramework/Factories/LocalizerFactory.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Localization.AspNetCore.EntityFramework.Entities;
77
using Localization.AspNetCore.EntityFramework.Enums;
88
using Localization.AspNetCore.EntityFramework.Extensions;
9-
using Localization.AspNetCore.EntityFramework.Models;
109
using Localization.AspNetCore.EntityFramework.Settings;
1110
using Microsoft.AspNetCore.Builder;
1211
using Microsoft.EntityFrameworkCore;
@@ -22,7 +21,7 @@ internal class LocalizerFactory<T> : IStringLocalizerFactory, ILocalizationManag
2221
private readonly LocalizerOptions _localizerSettings;
2322
private readonly RequestLocalizationOptions _requestLocalizationSettings;
2423
private readonly IServiceProvider _serviceProvider;
25-
private List<LocalizationModel> _cache = new List<LocalizationModel>();
24+
private List<LocalizationResource> _cache = new List<LocalizationResource>();
2625

2726
public LocalizerFactory(IServiceProvider serviceProvider,
2827
IOptions<LocalizerOptions> localizerOptions,
@@ -47,15 +46,8 @@ public void ResetCache()
4746
using (var scope = _serviceProvider.GetScopedService(out T context))
4847
{
4948
_cache = context.Set<LocalizationResource>()
50-
.SelectMany(l => l.Translations)
51-
.Select(t => new LocalizationModel
52-
{
53-
Id = t.Resource.Id,
54-
ResourceId = t.ResourceId,
55-
ResourceKey = t.Resource.ResourceKey,
56-
Language = t.Language,
57-
Value = t.Value
58-
})
49+
.Include(r=>r.Translations)
50+
.AsNoTracking()
5951
.ToList();
6052
}
6153
}
@@ -80,7 +72,8 @@ public LocalizedString GetResource(string resourceKey)
8072
throw new ArgumentException(nameof(resourceKey));
8173

8274
var values = _cache
83-
.Where(t => t.ResourceKey == resourceKey
75+
.SelectMany(r=>r.Translations)
76+
.Where(t => t.Resource.ResourceKey == resourceKey
8477
&& t.Language == CurrentLanguage)
8578
.Select(p => p.Value)
8679
.ToList();
@@ -141,16 +134,9 @@ private void UpdateCache(string resourceKey)
141134
using (var scope = _serviceProvider.GetScopedService(out T context))
142135
{
143136
var values = context.Set<LocalizationResource>()
137+
.AsNoTracking()
138+
.Include(r=>r.Translations)
144139
.Where(r => r.ResourceKey == resourceKey)
145-
.SelectMany(l => l.Translations)
146-
.Select(t => new LocalizationModel
147-
{
148-
Id = t.Resource.Id,
149-
ResourceId = t.ResourceId,
150-
ResourceKey = t.Resource.ResourceKey,
151-
Language = t.Language,
152-
Value = t.Value
153-
})
154140
.ToList();
155141

156142
_cache.Where(m => m.ResourceKey == resourceKey)

Localization.AspNetCore.EntityFramework/Models/LocalizationModel.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)