Skip to content

Commit 73d8310

Browse files
authored
add isolated memorycache (#21)
1 parent 44d7ed9 commit 73d8310

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ public void ConfigureServices(IServiceCollection services)
8080
8181
// limit the total entries in the MemoryCache
8282
// each unique user agent string counts as one entry
83-
options.CacheOptions.SizeLimit = 1024; // default is 256
83+
options.CacheOptions.SizeLimit = 1024; // default is null (= no limit)
8484
});
8585
}
8686
```
8787

88+
> `AddHttpUserAgentMemoryCachedParser` registers `HttpUserAgentParserMemoryCachedProvider` as singleton which contains an isolated `MemoryCache` object.
89+
8890
### ASP.NET Core
8991

9092
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)

src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ public class HttpUserAgentParserMemoryCachedProvider : IHttpUserAgentParserProvi
1515
/// <summary>
1616
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProvider"/>.
1717
/// </summary>
18-
/// <param name="memoryCache">The memory cache instance to use</param>
1918
/// <param name="options">The options used to set expiration and size limit</param>
20-
public HttpUserAgentParserMemoryCachedProvider(IMemoryCache memoryCache, HttpUserAgentParserMemoryCachedProviderOptions options)
19+
public HttpUserAgentParserMemoryCachedProvider(HttpUserAgentParserMemoryCachedProviderOptions options)
2120
{
22-
_memoryCache = memoryCache;
21+
_memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(options.CacheOptions);
2322
_options = options;
2423
}
2524

src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace MyCSharp.HttpUserAgentParser.MemoryCache
88
/// <summary>
99
/// Provider options for <see cref="HttpUserAgentParserMemoryCachedProvider"/>
1010
/// <remarks>
11-
/// Default of <seealso cref="MemoryCacheOptions.SizeLimit"/> is 256.
1211
/// Default of <seealso cref="MemoryCacheEntryOptions.SlidingExpiration"/> is 1 day
1312
/// </remarks>
1413
/// </summary>
@@ -46,11 +45,7 @@ public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions? cacheO
4645
// defaults
4746
SlidingExpiration = TimeSpan.FromDays(1)
4847
};
49-
this.CacheOptions = cacheOptions ?? new MemoryCacheOptions
50-
{
51-
// defaults
52-
SizeLimit = 256
53-
};
48+
this.CacheOptions = cacheOptions ?? new MemoryCacheOptions();
5449
}
5550
}
5651
}

tests/MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests/HttpUserAgentParserMemoryCachedProviderTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright © myCSharp 2020-2021, all rights reserved
22

33
using FluentAssertions;
4-
using Microsoft.Extensions.Caching.Memory;
54
using Xunit;
65

76
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests
@@ -12,9 +11,8 @@ public class HttpUserAgentParserMemoryCachedProviderTests
1211
public void Parse()
1312
{
1413
HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new();
15-
IMemoryCache memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(cachedProviderOptions.CacheOptions);
1614

17-
HttpUserAgentParserMemoryCachedProvider provider = new(memoryCache, cachedProviderOptions);
15+
HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions);
1816

1917
// create first
2018
string userAgentOne =

0 commit comments

Comments
 (0)