-
Notifications
You must be signed in to change notification settings - Fork 140
/
Extensions.cs
36 lines (30 loc) · 1.09 KB
/
Extensions.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
using Ardalis.GuardClauses;
using BuildingBlocks.Abstractions.Caching;
using BuildingBlocks.Core.Caching;
using BuildingBlocks.Core.Extensions;
using Microsoft.Extensions.Configuration;
namespace BuildingBlocks.Caching.InMemory;
public static class Extensions
{
public static IServiceCollection AddCustomInMemoryCache(
this IServiceCollection services,
IConfiguration config,
Action<InMemoryCacheOptions>? configureOptions = null)
{
Guard.Against.Null(services, nameof(services));
var options = config.GetOptions<InMemoryCacheOptions>(nameof(InMemoryCacheOptions));
if (configureOptions is { })
{
services.Configure(configureOptions);
}
else
{
services.AddOptions<InMemoryCacheOptions>().Bind(config.GetSection(nameof(InMemoryCacheOptions)))
.ValidateDataAnnotations();
}
services.AddMemoryCache();
services.AddTransient<ICacheManager, CacheManager>();
services.AddSingleton<ICacheProvider, InMemoryCacheProvider>();
return services;
}
}