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

Implement Http.Cache #13

Merged
merged 1 commit into from
Dec 5, 2023
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
11 changes: 11 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ jobs:
- name: Pack Polly
run: dotnet pack ./src/*/*/Polly.csproj --no-restore -o ~/nuget -c Release

# Cache

- name: Restore Cache
run: dotnet restore ./src/*/*/Cache.csproj

- name: Build Cache
run: dotnet build ./src/*/*/Cache.csproj --no-restore -c Release

- name: Pack Cache
run: dotnet pack ./src/*/*/Cache.csproj --no-restore -o ~/nuget -c Release

# Push

- name: Push Packages
Expand Down
17 changes: 12 additions & 5 deletions Infra.Http.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9715E6CD-CD62-4214-804A-9E0501D03797}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "polly", "polly", "{D2992286-27A9-4D09-AFC6-392D3F26CF5F}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "handler", "handler", "{D2992286-27A9-4D09-AFC6-392D3F26CF5F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Polly", "src\polly\Polly\Polly.csproj", "{9E202E25-C63F-442A-8B7D-E073DFF00B12}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Polly", "src\handler\Polly\Polly.csproj", "{9E202E25-C63F-442A-8B7D-E073DFF00B12}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sockets-handler-provider", "sockets-handler-provider", "{1495381A-D19A-477B-8804-BB07DCF73EF3}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "provider", "provider", "{1495381A-D19A-477B-8804-BB07DCF73EF3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketsHandlerProvider", "src\sockets-handler-provider\SocketsHandlerProvider\SocketsHandlerProvider.csproj", "{5B77A1F5-ACD3-45C5-97DA-9FA0869DBC34}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketsHandlerProvider", "src\provider\SocketsHandlerProvider\SocketsHandlerProvider.csproj", "{5B77A1F5-ACD3-45C5-97DA-9FA0869DBC34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketsHandlerProvider.Abstractions", "src\sockets-handler-provider\SocketsHandlerProvider.Abstractions\SocketsHandlerProvider.Abstractions.csproj", "{8A66DDCE-7726-4009-BBF8-DD55F04F95D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketsHandlerProvider.Abstractions", "src\provider\SocketsHandlerProvider.Abstractions\SocketsHandlerProvider.Abstractions.csproj", "{8A66DDCE-7726-4009-BBF8-DD55F04F95D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cache", "src\handler\Cache\Cache.csproj", "{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +35,10 @@ Global
{8A66DDCE-7726-4009-BBF8-DD55F04F95D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A66DDCE-7726-4009-BBF8-DD55F04F95D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A66DDCE-7726-4009-BBF8-DD55F04F95D4}.Release|Any CPU.Build.0 = Release|Any CPU
{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -43,6 +49,7 @@ Global
{1495381A-D19A-477B-8804-BB07DCF73EF3} = {9715E6CD-CD62-4214-804A-9E0501D03797}
{5B77A1F5-ACD3-45C5-97DA-9FA0869DBC34} = {1495381A-D19A-477B-8804-BB07DCF73EF3}
{8A66DDCE-7726-4009-BBF8-DD55F04F95D4} = {1495381A-D19A-477B-8804-BB07DCF73EF3}
{CDB1ADA8-75C7-4862-9E34-5CEA847EBAE4} = {D2992286-27A9-4D09-AFC6-392D3F26CF5F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6D3D18F6-8DAA-4284-9F3C-6FC817E6FF44}
Expand Down
20 changes: 20 additions & 0 deletions src/handler/Cache/Cache.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<InvariantGlobalization>true</InvariantGlobalization>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>GarageGroup.Infra</RootNamespace>
<AssemblyName>GarageGroup.Infra.Http.Cache</AssemblyName>
<Version>0.0.1</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HttpClient.Caching" Version="1.3.7" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="PrimeFuncPack.Dependency" Version="2.1.0" />
</ItemGroup>

</Project>
75 changes: 75 additions & 0 deletions src/handler/Cache/HttpCacheDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Caching.InMemory;
using Microsoft.Extensions.Configuration;
using PrimeFuncPack;

namespace GarageGroup.Infra;

public static class HttpCacheDependency
{
private const string DefaultSectionName = "HttpCache";

public static Dependency<HttpMessageHandler> UseInMemoryCache(
this Dependency<HttpMessageHandler> dependency,
Func<IServiceProvider, HttpCacheOption> optionResolver)
{
ArgumentNullException.ThrowIfNull(dependency);
ArgumentNullException.ThrowIfNull(optionResolver);

return dependency.With(optionResolver).Fold<HttpMessageHandler>(CreateInMemoryCacheHandler);

static InMemoryCacheHandler CreateInMemoryCacheHandler(HttpMessageHandler innerHandler, HttpCacheOption option)
{
ArgumentNullException.ThrowIfNull(innerHandler);
ArgumentNullException.ThrowIfNull(option);

return InnerCreateInMemoryCacheHandler(innerHandler, option);
}
}

public static Dependency<HttpMessageHandler> UseInMemoryCache(
this Dependency<HttpMessageHandler> dependency,
string cacheSectionName = DefaultSectionName)
{
ArgumentNullException.ThrowIfNull(dependency);

return dependency.Map<HttpMessageHandler>(ResolveHandler);

InMemoryCacheHandler ResolveHandler(IServiceProvider serviceProvider, HttpMessageHandler innerHandler)
{
ArgumentNullException.ThrowIfNull(serviceProvider);
ArgumentNullException.ThrowIfNull(innerHandler);

var configuration = serviceProvider.GetServiceOrThrow<IConfiguration>().GetSection(cacheSectionName ?? string.Empty);
return InnerCreateInMemoryCacheHandler(innerHandler, configuration.GetHttpCacheOption());
}
}

private static HttpCacheOption GetHttpCacheOption(this IConfiguration configuration)
{
var section = configuration.GetSection(nameof(HttpCacheOption.ExpirationPerHttpResponseCode));
var expirationPerHttpResponseCode = new Dictionary<HttpStatusCode, TimeSpan>();

foreach (var codeSection in section.GetChildren())
{
var value = codeSection.Value;
if (string.IsNullOrEmpty(value))
{
continue;
}

var statusCode = Enum.Parse<HttpStatusCode>(codeSection.Key, ignoreCase: true);
expirationPerHttpResponseCode[statusCode] = TimeSpan.Parse(value);
}

return new(
expirationPerHttpResponseCode: new(expirationPerHttpResponseCode));
}

private static InMemoryCacheHandler InnerCreateInMemoryCacheHandler(HttpMessageHandler innerHandler, HttpCacheOption option)
=>
new(innerHandler, option.ExpirationPerHttpResponseCode);
}
26 changes: 26 additions & 0 deletions src/handler/Cache/HttpCacheOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Net;

namespace GarageGroup.Infra;

public sealed record class HttpCacheOption
{
private static readonly ReadOnlyDictionary<HttpStatusCode, TimeSpan> DefaultExpirationPerHttpResponseCode;

static HttpCacheOption()
=>
DefaultExpirationPerHttpResponseCode = new(
new Dictionary<HttpStatusCode, TimeSpan>
{
[HttpStatusCode.OK] = TimeSpan.FromMinutes(60)
});

public HttpCacheOption([AllowNull] ReadOnlyDictionary<HttpStatusCode, TimeSpan> expirationPerHttpResponseCode)
=>
ExpirationPerHttpResponseCode = expirationPerHttpResponseCode?.Count > 0 ? expirationPerHttpResponseCode : DefaultExpirationPerHttpResponseCode;

public ReadOnlyDictionary<HttpStatusCode, TimeSpan> ExpirationPerHttpResponseCode { get; }
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public static Dependency<SocketsHttpHandler> UseSocketsHttpHandler(
public static Dependency<SocketsHttpHandler> UseStandardSocketsHttpHandler()
=>
InnerUseSocketsHttpHandler(
sp => sp.GetSocketsHttpHandlerConfigurationFromEnvironment(string.Empty));
static sp => sp.GetSocketsHttpHandlerConfigurationFromEnvironment(string.Empty));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>GarageGroup.Infra</RootNamespace>
<AssemblyName>GarageGroup.Infra.Http.SocketsHandlerProvider</AssemblyName>
<Version>2.5.0</Version>
<Version>2.5.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down