forked from VahidN/EFCoreSecondLevelCacheInterceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EFServiceProvider.cs
395 lines (366 loc) · 18.2 KB
/
EFServiceProvider.cs
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
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using CacheManager.Core;
using Newtonsoft.Json;
using EasyCaching.InMemory;
using CacheManager.Redis;
namespace EFCoreSecondLevelCacheInterceptor.Tests
{
public enum TestCacheProvider
{
BuiltInInMemory,
CacheManagerCoreInMemory,
CacheManagerCoreRedis,
EasyCachingCoreInMemory,
EasyCachingCoreRedis,
EasyCachingCoreHybrid
}
public class SpecialTypesConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(TimeSpan) || objectType == typeof(TimeSpan?)
|| objectType == typeof(DateTime) || objectType == typeof(DateTime?)
|| objectType == typeof(DateTimeOffset) || objectType == typeof(DateTimeOffset?);
}
public override bool CanRead => true;
public override bool CanWrite => true;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => reader.Value;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
writer.WritePropertyName("$type"); // Deserializer helper
writer.WriteValue(value.GetType().FullName);
writer.WritePropertyName("$value");
writer.WriteValue(value);
writer.WriteEndObject();
}
}
public static class EFServiceProvider
{
private static readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
public static IEFCacheServiceProvider GetCacheServiceProvider(TestCacheProvider provider)
{
var services = new ServiceCollection();
services.AddOptions();
services.AddLogging(cfg => cfg.AddConsole().AddDebug());
switch (provider)
{
case TestCacheProvider.BuiltInInMemory:
services.AddEFSecondLevelCache(options => options.UseMemoryCacheProvider());
break;
case TestCacheProvider.CacheManagerCoreInMemory:
services.AddEFSecondLevelCache(options => options.UseCacheManagerCoreProvider());
addCacheManagerCoreInMemory(services);
break;
case TestCacheProvider.CacheManagerCoreRedis:
services.AddEFSecondLevelCache(options => options.UseCacheManagerCoreProvider());
addCacheManagerCoreRedis(services);
break;
case TestCacheProvider.EasyCachingCoreInMemory:
const string providerName1 = "InMemory1";
services.AddEFSecondLevelCache(options => options.UseEasyCachingCoreProvider(providerName1));
addEasyCachingCoreInMemory(services, providerName1);
break;
case TestCacheProvider.EasyCachingCoreRedis:
const string providerName2 = "Redis1";
services.AddEFSecondLevelCache(options => options.UseEasyCachingCoreProvider(providerName2));
addEasyCachingCoreRedis(services, providerName2);
break;
case TestCacheProvider.EasyCachingCoreHybrid:
const string providerName3 = "Hybrid1";
services.AddEFSecondLevelCache(options => options.UseEasyCachingCoreProvider(providerName3, isHybridCache: true));
addEasyCachingCoreHybrid(services, providerName3);
break;
}
var serviceProvider = services.BuildServiceProvider();
var cacheProvider = serviceProvider.GetRequiredService<IEFCacheServiceProvider>();
cacheProvider.ClearAllCachedEntries();
return cacheProvider;
}
public static T GetRequiredService<T>()
{
var services = new ServiceCollection();
services.AddOptions();
services.AddLogging(cfg => cfg.AddConsole().AddDebug());
services.AddEFSecondLevelCache(options => options.UseMemoryCacheProvider());
var serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetRequiredService<T>();
}
public static IServiceProvider GetConfiguredContextServiceProvider(
TestCacheProvider cacheProvider,
LogLevel logLevel,
bool cacheAllQueries)
{
var services = new ServiceCollection();
services.AddOptions();
var basePath = Directory.GetCurrentDirectory();
Console.WriteLine($"Using `{basePath}` as the ContentRootPath");
var configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
services.AddSingleton(_ => configuration);
var loggerProvider = new DebugLoggerProvider();
services.AddLogging(cfg => cfg.AddConsole().AddDebug().AddProvider(loggerProvider).SetMinimumLevel(logLevel));
services.AddEFSecondLevelCache(options =>
{
switch (cacheProvider)
{
case TestCacheProvider.BuiltInInMemory:
options.UseMemoryCacheProvider();
break;
case TestCacheProvider.CacheManagerCoreInMemory:
options.UseCacheManagerCoreProvider();
addCacheManagerCoreInMemory(services);
break;
case TestCacheProvider.CacheManagerCoreRedis:
options.UseCacheManagerCoreProvider();
addCacheManagerCoreRedis(services);
break;
case TestCacheProvider.EasyCachingCoreInMemory:
const string providerName1 = "InMemory-1";
options.UseEasyCachingCoreProvider(providerName1);
addEasyCachingCoreInMemory(services, providerName1);
break;
case TestCacheProvider.EasyCachingCoreRedis:
const string providerName2 = "Redis-1";
options.UseEasyCachingCoreProvider(providerName2);
addEasyCachingCoreRedis(services, providerName2);
break;
case TestCacheProvider.EasyCachingCoreHybrid:
const string providerName3 = "Hybrid1";
options.UseEasyCachingCoreProvider(providerName3, isHybridCache: true);
addEasyCachingCoreHybrid(services, providerName3);
break;
}
if (cacheAllQueries)
{
options.CacheAllQueries(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30));
}
});
services.AddConfiguredMsSqlDbContext(GetConnectionString(basePath, configuration));
return services.BuildServiceProvider();
}
private static void addEasyCachingCoreHybrid(ServiceCollection services, string hybridProviderName)
{
const string redisProvider = "redis";
const string memoryProvider = "memory";
// More info: https://easycaching.readthedocs.io/en/latest/HybridCachingProvider/#how-to-use
services.AddEasyCaching(option =>
{
option.UseRedis(config =>
{
config.DBConfig.AllowAdmin = true;
config.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint("127.0.0.1", 6379));
}, redisProvider)
.UseInMemory(config =>
{
config.DBConfig = new InMemoryCachingOptions
{
// scan time, default value is 60s
ExpirationScanFrequency = 60,
// total count of cache items, default value is 10000
SizeLimit = 100,
// enable deep clone when reading object from cache or not, default value is true.
EnableReadDeepClone = false,
// enable deep clone when writing object to cache or not, default value is false.
EnableWriteDeepClone = false,
};
// the max random second will be added to cache's expiration, default value is 120
config.MaxRdSecond = 120;
// whether enable logging, default is false
config.EnableLogging = false;
// mutex key's alive time(ms), default is 5000
config.LockMs = 5000;
// when mutex key alive, it will sleep some time, default is 300
config.SleepMs = 300;
}, memoryProvider)
.UseHybrid(config =>
{
config.TopicName = "topic";
config.EnableLogging = true;
config.LocalCacheProviderName = memoryProvider;
config.DistributedCacheProviderName = redisProvider;
},
hybridProviderName)
.WithRedisBus(busConf =>
{
busConf.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint("127.0.0.1", 6379));
// busConf.Database = 2;
busConf.AllowAdmin = true;
});
});
}
private static void addEasyCachingCoreRedis(ServiceCollection services, string providerName)
{
// It needs <PackageReference Include="EasyCaching.Redis" Version="0.8.8" />
// More info: https://easycaching.readthedocs.io/en/latest/Redis/
services.AddEasyCaching(option =>
{
option.UseRedis(config =>
{
config.DBConfig.AllowAdmin = true;
config.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint("127.0.0.1", 6379));
}, providerName);
});
}
private static void addEasyCachingCoreInMemory(ServiceCollection services, string providerName)
{
// It needs <PackageReference Include="EasyCaching.InMemory" Version="0.8.8" />
// More info: https://easycaching.readthedocs.io/en/latest/In-Memory/
services.AddEasyCaching(options =>
{
// use memory cache with your own configuration
options.UseInMemory(config =>
{
config.DBConfig = new InMemoryCachingOptions
{
// scan time, default value is 60s
ExpirationScanFrequency = 60,
// total count of cache items, default value is 10000
SizeLimit = 100,
// enable deep clone when reading object from cache or not, default value is true.
EnableReadDeepClone = false,
// enable deep clone when writing object to cache or not, default value is false.
EnableWriteDeepClone = false,
};
// the max random second will be added to cache's expiration, default value is 120
config.MaxRdSecond = 120;
// whether enable logging, default is false
config.EnableLogging = false;
// mutex key's alive time(ms), default is 5000
config.LockMs = 5000;
// when mutex key alive, it will sleep some time, default is 300
config.SleepMs = 300;
}, providerName);
});
}
private static void addCacheManagerCoreRedis(ServiceCollection services)
{
var jss = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto,
Converters = { new SpecialTypesConverter() }
};
const string redisConfigurationKey = "redis";
services.AddSingleton(typeof(ICacheManagerConfiguration),
new CacheManager.Core.ConfigurationBuilder()
.WithJsonSerializer(serializationSettings: jss, deserializationSettings: jss)
.WithUpdateMode(CacheUpdateMode.Up)
.WithRedisConfiguration(redisConfigurationKey, config =>
{
config.WithAllowAdmin()
.WithDatabase(0)
.WithEndpoint("localhost", 6379)
// Enables keyspace notifications to react on eviction/expiration of items.
// Make sure that all servers are configured correctly and 'notify-keyspace-events' is at least set to 'Exe', otherwise CacheManager will not retrieve any events.
// See https://redis.io/topics/notifications#configuration for configuration details.
.EnableKeyspaceEvents();
})
.WithMaxRetries(100)
.WithRetryTimeout(50)
.WithRedisCacheHandle(redisConfigurationKey)
.DisablePerformanceCounters()
.DisableStatistics()
.Build());
services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));
}
private static void addCacheManagerCoreInMemory(ServiceCollection services)
{
services.AddSingleton(typeof(ICacheManagerConfiguration),
new CacheManager.Core.ConfigurationBuilder()
.WithJsonSerializer()
.WithMicrosoftMemoryCacheHandle(instanceName: "MemoryCache1")
.DisablePerformanceCounters()
.DisableStatistics()
.Build());
services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));
}
public static string GetConnectionString(string basePath, IConfigurationRoot configuration)
{
var testsFolder = basePath.Split(new[] { "\\Tests\\" }, StringSplitOptions.RemoveEmptyEntries)[0];
var contentRootPath = Path.Combine(testsFolder, "Tests", "EFCoreSecondLevelCacheInterceptor.AspNetCoreSample");
var connectionString = configuration["ConnectionStrings:ApplicationDbContextConnection"];
if (connectionString.Contains("%CONTENTROOTPATH%"))
{
connectionString = connectionString.Replace("%CONTENTROOTPATH%", contentRootPath);
}
Console.WriteLine($"Using {connectionString}");
return connectionString;
}
public static void RunInContext(
TestCacheProvider cacheProvider,
LogLevel logLevel,
bool cacheAllQueries,
params Action<ApplicationDbContext, DebugLoggerProvider>[] actions)
{
_semaphoreSlim.Wait();
try
{
var serviceProvider = GetConfiguredContextServiceProvider(cacheProvider, logLevel, cacheAllQueries);
var cacheServiceProvider = serviceProvider.GetRequiredService<IEFCacheServiceProvider>();
cacheServiceProvider.ClearAllCachedEntries();
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
foreach (var action in actions)
{
using (var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>())
{
action(context, (DebugLoggerProvider)serviceProvider.GetRequiredService<ILoggerProvider>());
}
}
}
}
finally
{
_semaphoreSlim.Release();
}
}
public static async Task RunInContextAsync(
TestCacheProvider cacheProvider,
LogLevel logLevel,
bool cacheAllQueries,
params Func<ApplicationDbContext, DebugLoggerProvider, Task>[] actions)
{
await _semaphoreSlim.WaitAsync();
try
{
var serviceProvider = GetConfiguredContextServiceProvider(cacheProvider, logLevel, cacheAllQueries);
var cacheServiceProvider = serviceProvider.GetRequiredService<IEFCacheServiceProvider>();
cacheServiceProvider.ClearAllCachedEntries();
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
foreach (var action in actions)
{
using (var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>())
{
await action(context, (DebugLoggerProvider)serviceProvider.GetRequiredService<ILoggerProvider>());
}
}
}
}
finally
{
_semaphoreSlim.Release();
}
}
public static void ExecuteInParallel(Action test, int count = 40)
{
var tests = new Action[count];
for (var i = 0; i < count; i++)
{
tests[i] = test;
}
Parallel.Invoke(tests);
}
}
}