Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isolated memorycache #21

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public class HttpUserAgentParserMemoryCachedProvider : IHttpUserAgentParserProvi
/// <summary>
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProvider"/>.
/// </summary>
/// <param name="memoryCache">The memory cache instance to use</param>
/// <param name="options">The options used to set expiration and size limit</param>
public HttpUserAgentParserMemoryCachedProvider(IMemoryCache memoryCache, HttpUserAgentParserMemoryCachedProviderOptions options)
public HttpUserAgentParserMemoryCachedProvider(HttpUserAgentParserMemoryCachedProviderOptions options)
{
_memoryCache = memoryCache;
_memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(options.CacheOptions);
_options = options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MyCSharp.HttpUserAgentParser.MemoryCache
/// <summary>
/// Provider options for <see cref="HttpUserAgentParserMemoryCachedProvider"/>
/// <remarks>
/// Default of <seealso cref="MemoryCacheOptions.SizeLimit"/> is 256.
/// Default of <seealso cref="MemoryCacheEntryOptions.SlidingExpiration"/> is 1 day
/// </remarks>
/// </summary>
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 =
Expand Down