Skip to content

Commit a0b6e68

Browse files
committed
added cache provider moick
1 parent 3332bd1 commit a0b6e68

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

Localization.AspNetCore.EntityFramework/Extensions/ServiceCollectionExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Localization.AspNetCore.EntityFramework.Factories;
33
using Localization.AspNetCore.EntityFramework.Managers;
4+
using Localization.AspNetCore.EntityFramework.Providers;
45
using Localization.AspNetCore.EntityFramework.Settings;
56
using Microsoft.EntityFrameworkCore;
67
using Microsoft.Extensions.DependencyInjection;
@@ -20,6 +21,13 @@ public static IServiceCollection AddLocalization<T>(this IServiceCollection serv
2021

2122
services.AddOptions();
2223

24+
services.AddMemoryCache();
25+
26+
services.TryAdd(new ServiceDescriptor(
27+
typeof(CacheProvider),
28+
typeof(CacheProvider),
29+
ServiceLifetime.Singleton));
30+
2331
services.TryAdd(new ServiceDescriptor(
2432
typeof(LocalizationManager<>),
2533
typeof(LocalizationManager<>),

Localization.AspNetCore.EntityFramework/Managers/LocalizationManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Localization.AspNetCore.EntityFramework.Entities;
66
using Localization.AspNetCore.EntityFramework.Enums;
77
using Localization.AspNetCore.EntityFramework.Extensions;
8+
using Localization.AspNetCore.EntityFramework.Providers;
89
using Localization.AspNetCore.EntityFramework.Settings;
910
using Microsoft.AspNetCore.Builder;
1011
using Microsoft.EntityFrameworkCore;
@@ -19,13 +20,16 @@ internal class LocalizationManager<T> : ILocalizationManager
1920
private readonly LocalizerOptions _localizerSettings;
2021
private readonly RequestLocalizationOptions _requestLocalizationSettings;
2122
private readonly IServiceProvider _serviceProvider;
23+
private readonly CacheProvider _cacheProvider;
2224
private List<LocalizationResource> _cache = new List<LocalizationResource>();
2325

2426
public LocalizationManager(IServiceProvider serviceProvider,
27+
CacheProvider cacheProvider,
2528
IOptions<LocalizerOptions> localizerOptions,
2629
IOptions<RequestLocalizationOptions> requestLocalizationOptions)
2730
{
2831
_serviceProvider = serviceProvider;
32+
_cacheProvider = cacheProvider;
2933
_localizerSettings = localizerOptions == null
3034
? throw new ArgumentNullException(nameof(localizerOptions))
3135
: localizerOptions.Value;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Threading;
3+
using Localization.AspNetCore.EntityFramework.Settings;
4+
using Microsoft.Extensions.Caching.Memory;
5+
using Microsoft.Extensions.Options;
6+
using Microsoft.Extensions.Primitives;
7+
8+
namespace Localization.AspNetCore.EntityFramework.Providers
9+
{
10+
internal class CacheProvider
11+
{
12+
private static CancellationTokenSource _resetCacheToken = new CancellationTokenSource();
13+
private readonly IMemoryCache _innerCache;
14+
private readonly LocalizerOptions _localizerSettings;
15+
16+
public CacheProvider(IMemoryCache cache,
17+
IOptions<LocalizerOptions> localizerOptions)
18+
{
19+
_localizerSettings = localizerOptions == null
20+
? throw new ArgumentNullException(nameof(localizerOptions))
21+
: localizerOptions.Value;
22+
_innerCache = cache;
23+
}
24+
25+
public T Set<T>(object key, T value)
26+
{
27+
/* some other code removed for brevity */
28+
var options = new MemoryCacheEntryOptions()
29+
.SetPriority(CacheItemPriority.Normal)
30+
.SetSlidingExpiration(_localizerSettings.CacheExpirationOffset);
31+
//.SetAbsoluteExpiration(typeExpiration);
32+
33+
options.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token));
34+
35+
_innerCache.Set(key, value, options);
36+
37+
return value;
38+
}
39+
40+
41+
public void Reset()
42+
{
43+
if (_resetCacheToken != null && !_resetCacheToken.IsCancellationRequested && _resetCacheToken.Token.CanBeCanceled)
44+
{
45+
_resetCacheToken.Cancel();
46+
_resetCacheToken.Dispose();
47+
}
48+
49+
_resetCacheToken = new CancellationTokenSource();
50+
}
51+
}
52+
}

Localization.AspNetCore.EntityFramework/Settings/LocalizerOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Localization.AspNetCore.EntityFramework.Enums;
23

34
namespace Localization.AspNetCore.EntityFramework.Settings
@@ -7,5 +8,6 @@ public class LocalizerOptions
78
public FallBackBehaviorEnum FallBackBehavior { get; set; } = FallBackBehaviorEnum.DefaultCulture;
89
public NamingConventionEnum NamingConvention { get; set; } = NamingConventionEnum.TypeName;
910
public bool CreateMissingTranslationsIfNotFound { get; set; } = true;
11+
public TimeSpan CacheExpirationOffset { get; set; } = new TimeSpan(1,0,0);
1012
}
1113
}

0 commit comments

Comments
 (0)