Skip to content

Commit 82f2d66

Browse files
committed
Add distributed cache redis and sql server
1 parent d115c18 commit 82f2d66

File tree

14 files changed

+119
-28
lines changed

14 files changed

+119
-28
lines changed

Api/Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="9.0.8" />
9+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.8" />
810
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.8" />
911
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.8" />
1012
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.8" />

Api/Startup.Ioc.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System;
12
using Domain.Abstractions;
23
using Domain.Configuration;
34
using Domain.Services;
45
using Infrastructure.DistributedCaches;
56
using Infrastructure.HttpClients;
7+
using Microsoft.Extensions.Configuration;
68
using Microsoft.Extensions.DependencyInjection;
79

810
namespace Api;
@@ -19,12 +21,35 @@ private void ConfigureIoc(IServiceCollection services)
1921
.AddHttpClient<IDummyClient, DummyClient>()
2022
.AddHttpMessageHandler<DummyMessageHandler>();
2123

22-
services.AddDistributedMemoryCache();
23-
24+
services.Configure<Settings>(Configuration.GetSection(Settings.SectionName));
25+
2426
services.AddSingleton<IDistributedCacheProvider, DistributedCacheProvider>();
25-
26-
services.Configure<Settings>(Configuration.GetSection(nameof(Settings)));
27-
28-
services.Configure<DistributedCacheOptions>(Configuration.GetSection(nameof(DistributedCacheOptions)));
27+
28+
var settings = Configuration
29+
.GetSection(Settings.SectionName)
30+
.Get<Settings>();
31+
32+
switch (settings.CacheSettings.Source)
33+
{
34+
case nameof(CacheSource.Memory):
35+
services.AddDistributedMemoryCache();
36+
break;
37+
case nameof(CacheSource.Redis):
38+
services.AddStackExchangeRedisCache(options =>
39+
{
40+
options.Configuration = settings.CacheSettings.Redis.ConnectionString;
41+
});
42+
break;
43+
case nameof(CacheSource.SqlServer):
44+
services.AddDistributedSqlServerCache(options =>
45+
{
46+
options.ConnectionString = settings.CacheSettings.SqlServer.ConnectionString;
47+
options.SchemaName = settings.CacheSettings.SqlServer.SchemaName;
48+
options.TableName = settings.CacheSettings.SqlServer.TableName;
49+
});
50+
break;
51+
default:
52+
throw new NotSupportedException($"Distributed cache source '{settings.CacheSettings.Source}' is not supported.");
53+
}
2954
}
3055
}

Api/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void ConfigureServices(IServiceCollection services)
2121
{
2222
services.AddControllers(options =>
2323
{
24-
options.Filters.Add(typeof(ApiExceptionFilter));
24+
options.Filters.Add<ApiExceptionFilter>();
2525
});
2626

2727
ConfigureSwagger(services);

Api/appsettings.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,18 @@
4141
"https://twitter.com/",
4242
"https://www.snapchat.com/",
4343
"https://www.facebook.com/"
44-
]
45-
},
46-
"DistributedCacheOptions": {
47-
"ExpirationInMinutes": 2
44+
],
45+
"CacheSettings": {
46+
"ExpirationInMinutes": 2,
47+
"Source": "Memory",
48+
"Redis": {
49+
"ConnectionString": "localhost:6379"
50+
},
51+
"SqlServer": {
52+
"ConnectionString": "Server=localhost,1433;Database=CacheDatabase;User Id=sa;Password=@Pa5sw0rd!;TrustServerCertificate=True;",
53+
"SchemaName": "dbo",
54+
"TableName": "CacheTable"
55+
}
56+
}
4857
}
4958
}

CreateDatabase.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE DATABASE CacheDatabase;
2+
GO
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
ProjectSection(SolutionItems) = preProject
1414
.github\workflows\ci.yml = .github\workflows\ci.yml
1515
README.md = README.md
16+
CreateDatabase.sql = CreateDatabase.sql
1617
EndProjectSection
1718
EndProject
1819
Global
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Domain.Configuration;
2+
3+
public sealed record CacheSettings
4+
{
5+
public int ExpirationInMinutes { get; init; } = 2;
6+
public string Source { get; init; } = nameof(CacheSource.Memory);
7+
public RedisSettings Redis { get; init; }
8+
9+
public SqlServerSettings SqlServer { get; init; }
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Domain.Configuration;
2+
3+
public enum CacheSource
4+
{
5+
Memory,
6+
Redis,
7+
SqlServer
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Domain.Configuration;
2+
3+
public sealed record RedisSettings
4+
{
5+
public string ConnectionString { get; init; }
6+
}

Domain/Configuration/Settings.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Domain.Configuration;
44

5-
public class Settings
5+
public sealed record Settings
66
{
7-
public ICollection<string> BlacklistUrls { get; set; }
7+
public const string SectionName = "Settings";
8+
9+
public ICollection<string> BlacklistUrls { get; init; }
10+
11+
public CacheSettings CacheSettings { get; init; }
812
}

0 commit comments

Comments
 (0)