-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceCollectionExtensions.cs
31 lines (26 loc) · 1.25 KB
/
ServiceCollectionExtensions.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
#if NETCOREAPP2_1
using Agero.Core.Checker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Threading;
namespace Agero.Core.ApiCache
{
/// <summary>Extensions for <see cref="IServiceCollection"/></summary>
public static class ServiceCollectionExtensions
{
private static int _executionCounter;
/// <summary>Adds "API Cache" services to specified <see cref="IServiceCollection"/></summary>
/// <typeparam name="TCacheManager">Cache manager type</typeparam>
/// <param name="services">ASP .NET Core dependency injection container</param>
/// <param name="cacheManager">API cache manager</param>
public static void AddApiCache<TCacheManager>(this IServiceCollection services, TCacheManager cacheManager)
where TCacheManager : BaseCacheManager
{
Check.ArgumentIsNull(services, nameof(services));
Check.ArgumentIsNull(cacheManager, nameof(cacheManager));
Check.Assert(Interlocked.Increment(ref _executionCounter) == 1, $"'{nameof(AddApiCache)}' method can be used only once.");
services.AddTransient<IHostedService>(_ => new ClearCacheBackgroundService(cacheManager));
}
}
}
#endif