diff --git a/README.md b/README.md index 712f02e..5bf0a21 100644 --- a/README.md +++ b/README.md @@ -80,11 +80,13 @@ public void ConfigureServices(IServiceCollection services) // limit the total entries in the MemoryCache // each unique user agent string counts as one entry - options.CacheOptions.SizeLimit = 1024; // default is 256 + options.CacheOptions.SizeLimit = 1024; // default is null (= no limit) }); } ``` +> `AddHttpUserAgentMemoryCachedParser` registers `HttpUserAgentParserMemoryCachedProvider` as singleton which contains an isolated `MemoryCache` object. + ### ASP.NET Core For ASP.NET Core applications, an accessor pattern (`IHttpUserAgentParserAccessor`) implementation can be registered additionally that independently retrieves the user agent based on the `HttpContextAccessor`. This requires the package [MyCSharp.HttpUserAgentParser.AspNetCore](https://www.nuget.org/packages/MyCSharp.HttpUserAgentParser.AspNetCore) diff --git a/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs b/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs index a8f8f1d..9ff4fda 100644 --- a/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs +++ b/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs @@ -15,11 +15,10 @@ public class HttpUserAgentParserMemoryCachedProvider : IHttpUserAgentParserProvi /// /// Creates a new instance of . /// - /// The memory cache instance to use /// The options used to set expiration and size limit - public HttpUserAgentParserMemoryCachedProvider(IMemoryCache memoryCache, HttpUserAgentParserMemoryCachedProviderOptions options) + public HttpUserAgentParserMemoryCachedProvider(HttpUserAgentParserMemoryCachedProviderOptions options) { - _memoryCache = memoryCache; + _memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(options.CacheOptions); _options = options; } diff --git a/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs b/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs index 6299576..3bf5aaa 100644 --- a/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs +++ b/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs @@ -8,7 +8,6 @@ namespace MyCSharp.HttpUserAgentParser.MemoryCache /// /// Provider options for /// - /// Default of is 256. /// Default of is 1 day /// /// @@ -46,11 +45,7 @@ public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions? cacheO // defaults SlidingExpiration = TimeSpan.FromDays(1) }; - this.CacheOptions = cacheOptions ?? new MemoryCacheOptions - { - // defaults - SizeLimit = 256 - }; + this.CacheOptions = cacheOptions ?? new MemoryCacheOptions(); } } } diff --git a/tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderTests.cs b/tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderTests.cs index 53d5c58..8f461b6 100644 --- a/tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderTests.cs +++ b/tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderTests.cs @@ -1,7 +1,6 @@ // Copyright © myCSharp 2020-2021, all rights reserved using FluentAssertions; -using Microsoft.Extensions.Caching.Memory; using Xunit; namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests @@ -12,9 +11,8 @@ public class HttpUserAgentParserMemoryCachedProviderTests public void Parse() { HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new(); - IMemoryCache memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(cachedProviderOptions.CacheOptions); - HttpUserAgentParserMemoryCachedProvider provider = new(memoryCache, cachedProviderOptions); + HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions); // create first string userAgentOne =