Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3

- run: dotnet pack --configuration Release --output ${{ env.NuGetDirectory }}
- run: dotnet pack .\src\HttpClient.Cache\HttpClient.Cache.csproj --configuration Release --output ${{ env.NuGetDirectory }}

- uses: actions/upload-artifact@v3
with:
Expand Down
44 changes: 34 additions & 10 deletions src/HttpClient.Cache/HttpClient.Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@
</PropertyGroup>

<PropertyGroup>
<Authors>Sergii Lischuk</Authors>
<Description>A simple and easy cache for HttpClient</Description>
<PackageProjectUrl>https://codestory.me</PackageProjectUrl>
<PackageTags>cache, httpclient, library</PackageTags>

<GenerateDocumentationFile>True</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<EnablePackageValidation>true</EnablePackageValidation>

<!-- Optional: Detect breaking changes from a previous version -->
<!-- <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> -->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GenerateDocumentationFile>True</GenerateDocumentationFile>

<!-- If all members are not documented, you can disable the compiler warnings -->
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>

<PropertyGroup>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>Sergii Lischuk</Authors>
<Description>A simple and easy cache for HttpClient</Description>

<!-- PackageProjectUrl is different from the repository URL. It can be a documentation
website or a website explaining the project -->
<PackageProjectUrl>https://codestory.me</PackageProjectUrl>

<!-- A list of tags to help the search engine to understand the content of the package -->
<PackageTags>cache, httpclient, library</PackageTags>
</PropertyGroup>

<PropertyGroup>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -35,11 +51,19 @@
<None Include="icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<PropertyGroup>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="" />
<None Include="README.md" Pack="true" PackagePath=""/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MinVer" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
68 changes: 68 additions & 0 deletions src/HttpClient.Cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# HttpClient.Cache

A caching wrapper around HttpClient to cache responses

### The Purpose

Working with some high load systems or with system where is important to have good response time
cache is a first one citizen.

### Install

//TODO Nuget deployment

```shell
foo@bar:~$ Install-Package HttpClient.Cache
```

### Examples

```csharp
const string url = "http://worldclockapi.com/api/json/utc/now";

//Set the cache time for each required status
var cacheExpiration = new Dictionary<HttpStatusCode, TimeSpan>
{
{HttpStatusCode.OK, TimeSpan.FromSeconds(60)},
{HttpStatusCode.BadRequest, TimeSpan.FromSeconds(10)},
{HttpStatusCode.InternalServerError, TimeSpan.FromSeconds(5)}
};

//Client calls API and caches it
//Report will show 1 Miss (initial) and 4 Hits.
var innerHandler = new HttpClientHandler();
var cacheHandler = new InMemoryCacheHandler(innerHandler, cacheExpiration);
using (var httpClient = new System.Net.Http.HttpClient(cacheHandler))
{
for (int i = 1; i <= 5; ++i)
{
Console.Write($"Try: {i}: {url} ");

var stopwatch = Stopwatch.StartNew();
var result = await httpClient.GetAsync(url);
Console.Write($" --> {result.StatusCode} ");
stopwatch.Stop();

Console.WriteLine($"Done in: {stopwatch.ElapsedMilliseconds} ms");
await Task.Delay(TimeSpan.FromSeconds(1));
}
}

var stats = cacheHandler.StatsProvider.GetReport();
Console.WriteLine($"Cache stats - total requests: {stats.Total.TotalRequests}");
Console.WriteLine($"--> Hit: {stats.Total.CacheHit} [{stats.Total.TotalHitsPercent}]");
Console.WriteLine($"--> Miss: {stats.Total.CacheMiss} [{stats.Total.TotalMissPercent}]");
Console.ReadLine();
```
Will generate next output:
```console
Try: 1: http://worldclockapi.com/api/json/utc/now --> OK Done in: 450 ms
Try: 2: http://worldclockapi.com/api/json/utc/now --> OK Done in: 57 ms
Try: 3: http://worldclockapi.com/api/json/utc/now --> OK Done in: 0 ms
Try: 4: http://worldclockapi.com/api/json/utc/now --> OK Done in: 0 ms
Try: 5: http://worldclockapi.com/api/json/utc/now --> OK Done in: 0 ms
Cache stats - total requests: 5
--> Hit: 4 [0,8]
--> Miss: 1 [0,2]

```
File renamed without changes