1+ using System ;
12using Domain . Abstractions ;
23using Domain . Configuration ;
34using Domain . Services ;
45using Infrastructure . DistributedCaches ;
56using Infrastructure . HttpClients ;
7+ using Microsoft . Extensions . Configuration ;
68using Microsoft . Extensions . DependencyInjection ;
79
810namespace 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}
0 commit comments