From d41a5996b195ec4755070ffe65875a29d0833070 Mon Sep 17 00:00:00 2001 From: Benjamin Abt Date: Tue, 18 May 2021 16:15:28 +0200 Subject: [PATCH 1/3] add isolated memry cache --- .../HttpUserAgentParserMemoryCachedProvider.cs | 5 ++--- .../HttpUserAgentParserMemoryCachedProviderTests.cs | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) 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/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 = From 5290b5a6883d4d3c6d800e2d200b9ab408066baa Mon Sep 17 00:00:00 2001 From: Benjamin Abt Date: Tue, 18 May 2021 16:18:46 +0200 Subject: [PATCH 2/3] add isolated memorycache --- README.md | 6 +++-- ...rAgentParserMemoryCachedProviderOptions.cs | 23 +------------------ 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 712f02e..9d0a4e8 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ The package [MyCSharp.HttpUserAgentParser.MemoryCache](https://www.nuget.org/pac ```csharp public void ConfigureServices(IServiceCollection services) { - services.AddHttpUserAgentMemoryCachedParser(); + services.AddHttpUserAgentMemoryCachedParser(); // or use options @@ -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` registeres `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/HttpUserAgentParserMemoryCachedProviderOptions.cs b/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs index 6299576..3a6e4b1 100644 --- a/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs +++ b/src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs @@ -8,37 +8,20 @@ namespace MyCSharp.HttpUserAgentParser.MemoryCache /// /// Provider options for /// - /// Default of is 256. /// Default of is 1 day /// /// public class HttpUserAgentParserMemoryCachedProviderOptions { - /// - /// Cache options - /// public MemoryCacheOptions CacheOptions { get; } - - /// - /// Cache entry options - /// public MemoryCacheEntryOptions CacheEntryOptions { get; } - /// - /// Creates a new instance of - /// public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions cacheOptions) : this(cacheOptions, null) { } - /// - /// Creates a new instance of - /// public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheEntryOptions cacheEntryOptions) : this(null, cacheEntryOptions) { } - /// - /// Creates a new instance of - /// public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions? cacheOptions = null, MemoryCacheEntryOptions? cacheEntryOptions = null) { this.CacheEntryOptions = cacheEntryOptions ?? new MemoryCacheEntryOptions @@ -46,11 +29,7 @@ public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions? cacheO // defaults SlidingExpiration = TimeSpan.FromDays(1) }; - this.CacheOptions = cacheOptions ?? new MemoryCacheOptions - { - // defaults - SizeLimit = 256 - }; + this.CacheOptions = cacheOptions ?? new MemoryCacheOptions(); } } } From 1be51cc188f107cd034f759186afba36100037a3 Mon Sep 17 00:00:00 2001 From: Benjamin Abt Date: Tue, 18 May 2021 16:20:02 +0200 Subject: [PATCH 3/3] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d0a4e8..24aa552 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ public void ConfigureServices(IServiceCollection services) } ``` -> `AddHttpUserAgentMemoryCachedParser` registeres `HttpUserAgentParserMemoryCachedProvider` as singleton which contains an isolated `MemoryCache` object. +> `AddHttpUserAgentMemoryCachedParser` registers `HttpUserAgentParserMemoryCachedProvider` as singleton which contains an isolated `MemoryCache` object. ### ASP.NET Core