-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHttpUserAgentParserMemoryCacheServiceCollectionExtensions.cs
More file actions
46 lines (41 loc) · 2 KB
/
HttpUserAgentParserMemoryCacheServiceCollectionExtensions.cs
File metadata and controls
46 lines (41 loc) · 2 KB
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
// Copyright © https://myCSharp.de - all rights reserved
using Microsoft.Extensions.DependencyInjection;
using MyCSharp.HttpUserAgentParser.DependencyInjection;
using MyCSharp.HttpUserAgentParser.Providers;
namespace MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection;
/// <summary>
/// Extension methods for registering <see cref="HttpUserAgentParserMemoryCachedProvider"/> with dependency injection.
/// </summary>
public static class HttpUserAgentParserMemoryCacheServiceCollectionExtensions
{
/// <summary>
/// Registers <see cref="HttpUserAgentParserMemoryCachedProvider"/> as a singleton implementation of <see cref="IHttpUserAgentParserProvider"/>.
/// </summary>
/// <param name="services">The service collection to add the services to.</param>
/// <param name="options">Optional action to configure the cache options.</param>
/// <returns>Options for further configuration.</returns>
/// <remarks>
/// <para>Default configuration: 256 entries maximum, 1 day sliding expiration.</para>
/// <para>Use the <paramref name="options"/> parameter to customize cache behavior.</para>
/// </remarks>
/// <example>
/// <code>
/// IServiceCollection services = new ServiceCollection();
/// services.AddHttpUserAgentMemoryCachedParser(opts =>
/// {
/// opts.CacheOptions.SizeLimit = 512;
/// opts.CacheEntryOptions.SlidingExpiration = TimeSpan.FromHours(6);
/// });
/// </code>
/// </example>
public static HttpUserAgentParserDependencyInjectionOptions AddHttpUserAgentMemoryCachedParser(
this IServiceCollection services, Action<HttpUserAgentParserMemoryCachedProviderOptions>? options = null)
{
HttpUserAgentParserMemoryCachedProviderOptions providerOptions = new();
options?.Invoke(providerOptions);
// register options
services.AddSingleton(providerOptions);
// register cache provider
return services.AddHttpUserAgentParser<HttpUserAgentParserMemoryCachedProvider>();
}
}