Skip to content

Added support for custom cache key provider #10

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

Merged
merged 15 commits into from
May 7, 2022
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
147 changes: 147 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
####################################################################
# Editor Configuration (Updated 2022-01-07)
#
# (c)2021 superdev GmbH
####################################################################

root = true

[*.cs]
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_labels = one_less_than_current
csharp_indent_switch_labels = true

csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = all
csharp_new_line_between_query_expression_clauses = true

csharp_prefer_braces = true:error
csharp_prefer_simple_default_expression = true:error
csharp_prefer_simple_using_statement = false

csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false

csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false

csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = do_not_ignore
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = none
csharp_space_between_square_brackets = false

# Expression-bodied members
csharp_style_expression_bodied_accessors = true:none
csharp_style_expression_bodied_constructors = false:none
csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_lambdas = true:none
csharp_style_expression_bodied_local_functions = false
csharp_style_expression_bodied_methods = false:none
csharp_style_expression_bodied_operators = false:none
csharp_style_expression_bodied_properties = true:silent

csharp_style_conditional_delegate_call = true:error
csharp_style_inlined_variable_declaration = true:error
csharp_style_pattern_matching_over_as_with_null_check = true:error
csharp_style_pattern_matching_over_is_with_cast_check = true:error
csharp_style_throw_expression = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:error
csharp_style_implicit_object_creation_when_type_is_apparent = false
csharp_style_prefer_switch_expression = false

dotnet_sort_system_directives_first = true
dotnet_style_coalesce_expression = true:error
dotnet_style_collection_initializer = true:error
dotnet_style_explicit_tuple_names = true:error
dotnet_style_null_propagation = true:error
dotnet_style_object_initializer = true:none
dotnet_style_predefined_type_for_locals_parameters_members = true:error
dotnet_style_predefined_type_for_member_access = true:error

dotnet_style_qualification_for_event = true:error
dotnet_style_qualification_for_field = true:error
dotnet_style_qualification_for_method = true:error
dotnet_style_qualification_for_property = true:error

end_of_line = crlf
indent_size = 4
indent_style = space
insert_final_newline = false
tab_width = 4

dotnet_naming_symbols.private_field_symbol.applicable_kinds = field
dotnet_naming_symbols.private_field_symbol.applicable_accessibilities = private
dotnet_naming_style.private_field_style.capitalization = camel_case
dotnet_naming_rule.private_fields_are_camel_case.severity = error
dotnet_naming_rule.private_fields_are_camel_case.symbols = private_field_symbol
dotnet_naming_rule.private_fields_are_camel_case.style = private_field_style

dotnet_naming_symbols.non_private_field_symbol.applicable_kinds = field
dotnet_naming_symbols.non_private_field_symbol.applicable_accessibilities = public,internal,friend,protected,protected_internal,protected_friend
dotnet_naming_style.non_private_field_style.capitalization = pascal_case
dotnet_naming_rule.non_private_fields_are_pascal_case.severity = error
dotnet_naming_rule.non_private_fields_are_pascal_case.symbols = non_private_field_symbol
dotnet_naming_rule.non_private_fields_are_pascal_case.style = non_private_field_style

dotnet_naming_symbols.parameter_symbol.applicable_kinds = parameter
dotnet_naming_style.parameter_style.capitalization = camel_case
dotnet_naming_rule.parameters_are_camel_case.severity = error
dotnet_naming_rule.parameters_are_camel_case.symbols = parameter_symbol
dotnet_naming_rule.parameters_are_camel_case.style = parameter_style

dotnet_naming_symbols.non_interface_type_symbol.applicable_kinds = class,struct,enum,delegate
dotnet_naming_style.non_interface_type_style.capitalization = pascal_case
dotnet_naming_rule.non_interface_types_are_pascal_case.severity = error
dotnet_naming_rule.non_interface_types_are_pascal_case.symbols = non_interface_type_symbol
dotnet_naming_rule.non_interface_types_are_pascal_case.style = non_interface_type_style

dotnet_naming_symbols.interface_type_symbol.applicable_kinds = interface
dotnet_naming_style.interface_type_style.capitalization = pascal_case
dotnet_naming_style.interface_type_style.required_prefix = I
dotnet_naming_rule.interface_types_must_be_prefixed_with_I.severity = error
dotnet_naming_rule.interface_types_must_be_prefixed_with_I.symbols = interface_type_symbol
dotnet_naming_rule.interface_types_must_be_prefixed_with_I.style = interface_type_style

dotnet_naming_symbols.member_symbol.applicable_kinds = method,property,event
dotnet_naming_style.member_style.capitalization = pascal_case
dotnet_naming_rule.members_are_pascal_case.severity = error
dotnet_naming_rule.members_are_pascal_case.symbols = member_symbol
dotnet_naming_rule.members_are_pascal_case.style = member_style

dotnet_naming_rule.static_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.static_fields_should_be_pascal_case.symbols = static_fields
dotnet_naming_rule.static_fields_should_be_pascal_case.style = static_field_style
dotnet_naming_symbols.static_fields.applicable_kinds = field
dotnet_naming_symbols.static_fields.applicable_accessibilities = *
dotnet_naming_symbols.static_fields.required_modifiers = static
dotnet_naming_style.static_field_style.capitalization = pascal_case

# CS4014: Because this call is not awaited, execution of the current method continues before the call is completed
dotnet_diagnostic.CS4014.severity = error

# IDE0051: Remove unused private members
dotnet_diagnostic.IDE0051.severity = warning
2 changes: 2 additions & 0 deletions HttpClient.Caching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
build.yml = build.yml
README.md = README.md
.gitignore = .gitignore
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAppSample", "Samples\ConsoleAppSample\ConsoleAppSample.csproj", "{592B2324-79AA-4973-8CE5-F65BD503641F}"
Expand Down
17 changes: 17 additions & 0 deletions HttpClient.Caching/Abstractions/ICacheKeysProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Net.Http;

namespace Microsoft.Extensions.Caching.Abstractions
{
/// <summary>
/// Provides keys to store or retrieve data in the cache
/// </summary>
public interface ICacheKeysProvider
{
/// <summary>
/// Return the key for the request message <paramref name="request"/>
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
string GetKey(HttpRequestMessage request);
}
}
6 changes: 3 additions & 3 deletions HttpClient.Caching/HttpClient.Caching.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard1.2;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net452;netstandard1.2;netstandard2.0;netstandard2.1</TargetFrameworks>
<Authors>Thomas Galliker</Authors>
<Company>superdev GmbH</Company>
<Description>HttpClient.Caching adds http response caching to HttpClient.</Description>
<Copyright>Copyright 2021</Copyright>
<Copyright>Copyright 2022</Copyright>
<PackageProjectUrl>https://github.com/thomasgalliker/HttpClient.Caching</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/thomasgalliker/HttpClient.Caching/master/logo.png</PackageIconUrl>
<RepositoryUrl>https://github.com/thomasgalliker/HttpClient.Caching</RepositoryUrl>
Expand All @@ -21,7 +21,7 @@
<PackageReference Include="Newtonsoft.Json" Version="[11.0.2,)" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down
36 changes: 36 additions & 0 deletions HttpClient.Caching/InMemory/DefaultCacheKeysProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Net.Http;
using System.Text;
using Microsoft.Extensions.Caching.Abstractions;

namespace Microsoft.Extensions.Caching.InMemory
{
/// <summary>
/// Provides keys to store or retrieve data in the cache in the default way (http method + http request Uri)
/// </summary>
public class DefaultCacheKeysProvider : ICacheKeysProvider
{
/// <summary>
/// Return the key for the request message <paramref name="request"/> by composing a string
/// with <see cref="HttpRequestMessage.Method"/> and <see cref="HttpRequestMessage.RequestUri"/>
/// </summary>
/// <param name="request"></param>
/// <returns>
/// An example of return value: "MET_GET;URI_https://www.google.it"
/// </returns>
public string GetKey(HttpRequestMessage request)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

var sb = new StringBuilder();

sb.AppendFormat("MET_{0};", request.Method);
sb.AppendFormat("URI_{0};", request.RequestUri);

return sb.ToString();
}
}
}
36 changes: 31 additions & 5 deletions HttpClient.Caching/InMemory/InMemoryCacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class InMemoryCacheHandler : DelegatingHandler
private readonly IDictionary<HttpStatusCode, TimeSpan> cacheExpirationPerHttpResponseCode;
private readonly IMemoryCache responseCache;

/// <summary>
/// Cache key provider being used
/// </summary>
public ICacheKeysProvider CacheKeysProvider { get; }


/// <summary>
/// Create a new InMemoryCacheHandler.
/// </summary>
Expand All @@ -31,8 +37,20 @@ public class InMemoryCacheHandler : DelegatingHandler
/// An <see cref="IStatsProvider" /> that records statistic information about the caching
/// behavior.
/// </param>
public InMemoryCacheHandler(HttpMessageHandler innerHandler = null, IDictionary<HttpStatusCode, TimeSpan> cacheExpirationPerHttpResponseCode = null, IStatsProvider statsProvider = null)
: this(innerHandler, cacheExpirationPerHttpResponseCode, statsProvider, new MemoryCache(new MemoryCacheOptions()))
/// <param name="cacheKeysProvider">
/// An <see cref="ICacheKeysProvider"/> that provides keys to retrieve and store items in the cache
/// </param>
public InMemoryCacheHandler(HttpMessageHandler innerHandler = null,
IDictionary<HttpStatusCode, TimeSpan> cacheExpirationPerHttpResponseCode = null,
IStatsProvider statsProvider = null,
ICacheKeysProvider cacheKeysProvider = null)
: this(
innerHandler,
cacheExpirationPerHttpResponseCode,
statsProvider,
new MemoryCache(new MemoryCacheOptions()),
cacheKeysProvider
)
{
}

Expand All @@ -49,12 +67,19 @@ public InMemoryCacheHandler(HttpMessageHandler innerHandler = null, IDictionary<
/// behavior.
/// </param>
/// <param name="cache">The cache to be used.</param>
internal InMemoryCacheHandler(HttpMessageHandler innerHandler, IDictionary<HttpStatusCode, TimeSpan> cacheExpirationPerHttpResponseCode, IStatsProvider statsProvider, IMemoryCache cache)
/// <param name="cacheKeysProvider">The <see cref="ICacheKeysProvider"/> cache keys provider to use</param>
internal InMemoryCacheHandler(
HttpMessageHandler innerHandler,
IDictionary<HttpStatusCode, TimeSpan> cacheExpirationPerHttpResponseCode,
IStatsProvider statsProvider,
IMemoryCache cache,
ICacheKeysProvider cacheKeysProvider)
: base(innerHandler ?? new HttpClientHandler())
{
this.StatsProvider = statsProvider ?? new StatsProvider(nameof(InMemoryCacheHandler));
this.cacheExpirationPerHttpResponseCode = cacheExpirationPerHttpResponseCode ?? new Dictionary<HttpStatusCode, TimeSpan>();
this.responseCache = cache ?? new MemoryCache(new MemoryCacheOptions());
this.CacheKeysProvider = cacheKeysProvider ?? new DefaultCacheKeysProvider();
}

/// <summary>
Expand All @@ -67,7 +92,8 @@ public void InvalidateCache(Uri uri, HttpMethod method = null)
var methods = method != null ? new[] { method } : new[] { HttpMethod.Get, HttpMethod.Head };
foreach (var m in methods)
{
var key = m + uri.ToString();
var request = new HttpRequestMessage(m, uri);
var key = CacheKeysProvider.GetKey(request);
this.responseCache.Remove(key);
}
}
Expand All @@ -78,7 +104,7 @@ public void InvalidateCache(Uri uri, HttpMethod method = null)
/// <returns>The HttpResponseMessage from cache, or a newly invoked one.</returns>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var key = request.Method + request.RequestUri.ToString();
var key = this.CacheKeysProvider.GetKey(request);
// gets the data from cache, and returns the data if it's a cache hit
if (request.Method == HttpMethod.Get || request.Method == HttpMethod.Head)
{
Expand Down
85 changes: 85 additions & 0 deletions HttpClient.Caching/InMemory/MethodUriHeadersCacheKeysProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using Microsoft.Extensions.Caching.Abstractions;

namespace Microsoft.Extensions.Caching.InMemory
{
/// <summary>
/// Provides keys to store or retrieve data in the cache by using http method, specific headers and Uri
/// </summary>
public class MethodUriHeadersCacheKeysProvider : ICacheKeysProvider
{
private readonly string[] headersName;

/// <summary>
/// Initialize the cache key provider passing the headers name that will be used to compose <paramref name="headersName"/>
/// </summary>
/// <param name="headersName"></param>
public MethodUriHeadersCacheKeysProvider(string[] headersName)
{
if (headersName != null)
{
this.headersName = headersName.OrderBy(i => i).ToArray();
}
}

/// <summary>
/// Return the key for the request message <paramref name="request"/> by composing a string
/// with <see cref="HttpRequestMessage.Method"/>, <see cref="HttpRequestMessage.Headers"/> and <see cref="HttpRequestMessage.RequestUri"/>
/// </summary>
/// <param name="request"></param>
/// <returns>
/// An example of return value: "MET_GET;HEA_X-KID_389dfhuif;URI_https://www.google.it?par1=65&par2=20;"
/// </returns>
public string GetKey(HttpRequestMessage request)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

var sb = new StringBuilder();

sb.AppendFormat("MET_{0};", request.Method);
if (this.headersName != null)
{
foreach (var headerName in this.headersName)
{
if (request.Headers.Contains(headerName))
{
sb.AppendFormat("HEA_{0}_{1};", headerName, this.GetHeaderValue(request, headerName));
}
}
}

sb.AppendFormat("URI_{0};", request.RequestUri);

return sb.ToString();
}

private string GetHeaderValue(HttpRequestMessage request, string headerName)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

if (string.IsNullOrEmpty(headerName))
{
throw new ArgumentException($"'{nameof(headerName)}' cannot be null or empty.", nameof(headerName));
}

var orderedHeaderValues = string.Empty;

var headerValues = request.Headers.GetValues(headerName);
if (headerValues != null)
{
orderedHeaderValues = string.Join(",", headerValues.OrderBy(i => i));
}

return orderedHeaderValues;
}
}
}
Loading