forked from VahidN/EFCoreSecondLevelCacheInterceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
91 lines (80 loc) · 3.6 KB
/
Startup.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
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer.Entities;
using System;
namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
public class Startup
{
private readonly string _contentRootPath;
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
_contentRootPath = env.ContentRootPath;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30))
.DisableLogging(false)
//.CacheAllQueries(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30)
/*.CacheQueriesContainingTypes(
CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30),
typeof(Post), typeof(Product), typeof(User)
)*/
.CacheQueriesContainingTableNames(
CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30),
"posts", "products", "users"
)
.SkipCachingCommands(commandText =>
// How to skip caching specific commands
commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase))
// Don't cache null values. Remove this optional setting if it's not necessary.
.SkipCachingResults(result =>
result.Value == null || (result.Value is EFTableRows rows && rows.RowsCount == 0))
.SkipCacheInvalidationCommands(commandText =>
// How to skip invalidating the related cache entries of this query
commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase));
});
var connectionString = Configuration["ConnectionStrings:ApplicationDbContextConnection"];
if (connectionString.Contains("%CONTENTROOTPATH%"))
{
connectionString = connectionString.Replace("%CONTENTROOTPATH%", _contentRootPath);
}
services.AddConfiguredMsSqlDbContext(connectionString);
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
IServiceScopeFactory scopeFactory)
{
scopeFactory.Initialize();
scopeFactory.SeedData();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}