From 28448789cd1df9915df66afc67d1f92b1f48017e Mon Sep 17 00:00:00 2001 From: Benjamin Abt Date: Tue, 18 May 2021 16:27:47 +0200 Subject: [PATCH] add isolated memorycache --- README.md | 4 +++- .../HttpUserAgentParserMemoryCachedProvider.cs | 5 ++--- .../HttpUserAgentParserMemoryCachedProviderOptions.cs | 7 +------ .../HttpUserAgentParserMemoryCachedProviderTests.cs | 4 +--- 4 files changed, 7 insertions(+), 13 deletions(-) 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 =