Leefrost.HttpClient.Cache 0.1.3

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Leefrost.HttpClient.Cache --version 0.1.3
NuGet\Install-Package Leefrost.HttpClient.Cache -Version 0.1.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Leefrost.HttpClient.Cache" Version="0.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Leefrost.HttpClient.Cache --version 0.1.3
#r "nuget: Leefrost.HttpClient.Cache, 0.1.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Leefrost.HttpClient.Cache as a Cake Addin
#addin nuget:?package=Leefrost.HttpClient.Cache&version=0.1.3

// Install Leefrost.HttpClient.Cache as a Cake Tool
#tool nuget:?package=Leefrost.HttpClient.Cache&version=0.1.3

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

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

Examples

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:

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]

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 155 4/20/2023
0.1.4 153 4/4/2023
0.1.3 168 3/31/2023