-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c2b1e6
commit ad5f764
Showing
19 changed files
with
779 additions
and
803 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/MyCSharp.HttpUserAgentParser.AspNetCore/IHttpUserAgentParserAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright © myCSharp.de - all rights reserved | ||
|
||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace MyCSharp.HttpUserAgentParser.AspNetCore; | ||
|
||
/// <summary> | ||
/// User Agent parser accessor | ||
/// </summary> | ||
public interface IHttpUserAgentParserAccessor | ||
{ | ||
/// <summary> | ||
/// User agent value | ||
/// </summary> | ||
string? GetHttpContextUserAgent(HttpContext httpContext); | ||
|
||
/// <summary> | ||
/// Returns current <see cref="HttpUserAgentInformation"/> | ||
/// </summary> | ||
HttpUserAgentInformation? Get(HttpContext httpContext); | ||
} |
81 changes: 37 additions & 44 deletions
81
src/MyCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,58 @@ | ||
// Copyright © myCSharp.de - all rights reserved | ||
|
||
using System; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using MyCSharp.HttpUserAgentParser.Providers; | ||
|
||
namespace MyCSharp.HttpUserAgentParser.MemoryCache | ||
namespace MyCSharp.HttpUserAgentParser.MemoryCache; | ||
|
||
/// <inheritdoc/> | ||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProvider"/>. | ||
/// </summary> | ||
/// <param name="options">The options used to set expiration and size limit</param> | ||
public class HttpUserAgentParserMemoryCachedProvider( | ||
HttpUserAgentParserMemoryCachedProviderOptions options) : IHttpUserAgentParserProvider | ||
{ | ||
private readonly Microsoft.Extensions.Caching.Memory.MemoryCache _memoryCache = new(options.CacheOptions); | ||
private readonly HttpUserAgentParserMemoryCachedProviderOptions _options = options; | ||
|
||
/// <inheritdoc/> | ||
public class HttpUserAgentParserMemoryCachedProvider : IHttpUserAgentParserProvider | ||
public HttpUserAgentInformation Parse(string userAgent) | ||
{ | ||
private readonly Microsoft.Extensions.Caching.Memory.MemoryCache _memoryCache; | ||
private readonly HttpUserAgentParserMemoryCachedProviderOptions _options; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProvider"/>. | ||
/// </summary> | ||
/// <param name="options">The options used to set expiration and size limit</param> | ||
public HttpUserAgentParserMemoryCachedProvider(HttpUserAgentParserMemoryCachedProviderOptions options) | ||
{ | ||
_memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(options.CacheOptions); | ||
_options = options; | ||
} | ||
CacheKey key = this.GetKey(userAgent); | ||
|
||
/// <inheritdoc/> | ||
public HttpUserAgentInformation Parse(string userAgent) | ||
return _memoryCache.GetOrCreate(key, static entry => | ||
{ | ||
CacheKey key = this.GetKey(userAgent); | ||
|
||
return _memoryCache.GetOrCreate(key, static entry => | ||
{ | ||
CacheKey key = (entry.Key as CacheKey)!; | ||
entry.SlidingExpiration = key.Options.CacheEntryOptions.SlidingExpiration; | ||
entry.SetSize(1); | ||
CacheKey key = (entry.Key as CacheKey)!; | ||
entry.SlidingExpiration = key.Options.CacheEntryOptions.SlidingExpiration; | ||
entry.SetSize(1); | ||
|
||
return HttpUserAgentParser.Parse(key.UserAgent); | ||
}); | ||
} | ||
return HttpUserAgentParser.Parse(key.UserAgent); | ||
}); | ||
} | ||
|
||
[ThreadStatic] | ||
private static CacheKey? s_tKey; | ||
[ThreadStatic] | ||
private static CacheKey? s_tKey; | ||
|
||
private CacheKey GetKey(string userAgent) | ||
{ | ||
CacheKey key = s_tKey ??= new CacheKey(); | ||
private CacheKey GetKey(string userAgent) | ||
{ | ||
CacheKey key = s_tKey ??= new CacheKey(); | ||
|
||
key.UserAgent = userAgent; | ||
key.Options = _options; | ||
key.UserAgent = userAgent; | ||
key.Options = _options; | ||
|
||
return key; | ||
} | ||
return key; | ||
} | ||
|
||
private class CacheKey : IEquatable<CacheKey> // required for IMemoryCache | ||
{ | ||
public string UserAgent { get; set; } = null!; | ||
private class CacheKey : IEquatable<CacheKey> // required for IMemoryCache | ||
{ | ||
public string UserAgent { get; set; } = null!; | ||
|
||
public HttpUserAgentParserMemoryCachedProviderOptions Options { get; set; } = null!; | ||
public HttpUserAgentParserMemoryCachedProviderOptions Options { get; set; } = null!; | ||
|
||
public bool Equals(CacheKey? other) => this.UserAgent == other?.UserAgent; | ||
public override bool Equals(object? obj) => this.Equals(obj as CacheKey); | ||
public bool Equals(CacheKey? other) => this.UserAgent == other?.UserAgent; | ||
public override bool Equals(object? obj) => this.Equals(obj as CacheKey); | ||
|
||
public override int GetHashCode() => this.UserAgent.GetHashCode(); | ||
} | ||
public override int GetHashCode() => this.UserAgent.GetHashCode(); | ||
} | ||
} |
90 changes: 45 additions & 45 deletions
90
...yCSharp.HttpUserAgentParser.MemoryCache/HttpUserAgentParserMemoryCachedProviderOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,56 @@ | ||
// Copyright © myCSharp.de - all rights reserved | ||
|
||
using System; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace MyCSharp.HttpUserAgentParser.MemoryCache | ||
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> | ||
public class HttpUserAgentParserMemoryCachedProviderOptions | ||
{ | ||
/// <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> | ||
/// Cache options | ||
/// </summary> | ||
public MemoryCacheOptions CacheOptions { get; } | ||
|
||
/// <summary> | ||
/// Cache entry options | ||
/// </summary> | ||
public class HttpUserAgentParserMemoryCachedProviderOptions | ||
public MemoryCacheEntryOptions CacheEntryOptions { get; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProviderOptions"/> | ||
/// </summary> | ||
public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions cacheOptions) | ||
: this(cacheOptions, null) { } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProviderOptions"/> | ||
/// </summary> | ||
public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheEntryOptions cacheEntryOptions) | ||
: this(null, cacheEntryOptions) { } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProviderOptions"/> | ||
/// </summary> | ||
public HttpUserAgentParserMemoryCachedProviderOptions( | ||
MemoryCacheOptions? cacheOptions = null, MemoryCacheEntryOptions? cacheEntryOptions = null) | ||
{ | ||
/// <summary> | ||
/// Cache options | ||
/// </summary> | ||
public MemoryCacheOptions CacheOptions { get; } | ||
|
||
/// <summary> | ||
/// Cache entry options | ||
/// </summary> | ||
public MemoryCacheEntryOptions CacheEntryOptions { get; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProviderOptions"/> | ||
/// </summary> | ||
public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions cacheOptions) | ||
: this(cacheOptions, null) { } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProviderOptions"/> | ||
/// </summary> | ||
public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheEntryOptions cacheEntryOptions) | ||
: this(null, cacheEntryOptions) { } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="HttpUserAgentParserMemoryCachedProviderOptions"/> | ||
/// </summary> | ||
public HttpUserAgentParserMemoryCachedProviderOptions(MemoryCacheOptions? cacheOptions = null, MemoryCacheEntryOptions? cacheEntryOptions = null) | ||
this.CacheEntryOptions = cacheEntryOptions ?? new MemoryCacheEntryOptions | ||
{ | ||
// defaults | ||
SlidingExpiration = TimeSpan.FromDays(1) | ||
}; | ||
|
||
this.CacheOptions = cacheOptions ?? new MemoryCacheOptions | ||
{ | ||
this.CacheEntryOptions = cacheEntryOptions ?? new MemoryCacheEntryOptions | ||
{ | ||
// defaults | ||
SlidingExpiration = TimeSpan.FromDays(1) | ||
}; | ||
this.CacheOptions = cacheOptions ?? new MemoryCacheOptions | ||
{ | ||
// defaults | ||
SizeLimit = 256 | ||
}; | ||
} | ||
// defaults | ||
SizeLimit = 256 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.