Leefrost.HttpClient.Cache 1.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Leefrost.HttpClient.Cache --version 1.0.0
NuGet\Install-Package Leefrost.HttpClient.Cache -Version 1.0.0
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="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Leefrost.HttpClient.Cache --version 1.0.0
#r "nuget: Leefrost.HttpClient.Cache, 1.0.0"
#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=1.0.0

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

HttpClient.Cache

A lightweight in-memory cache for HttpClient.

The Purpose

Working with high-load systems or with a system where it is important to have a good response time - the cache is a first citizen. This package contains lightweight, self-written, in-memory cache implementation to catch and store responses based on their status code. The configuration is pretty flexible and gives opportynity to set pair between cache time and response code.

How to install

dotnet add package Leefrost.HttpClient.Cache

Plans and TODOs:

  • in-memory caching support
  • distributed caching support

How to use

The code below caches 3 top-kind responses (OK, BadRequest, and InternalServerError) for a different time - 60/10/5 seconds. HttpClient will do 5 requests to the https://randomuser.me/api/ and do cache the responses for us.

The cache report will show us 1 miss (initial request) and 4 hits (so the response time will be 0)

const string url = "https://randomuser.me/api/";

//Setting 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)}
};

//Calling the API and cache the responses
var requestHandler = new HttpClientHandler();
var cacheHandler = new InMemoryCacheHandler(requestHandler, cacheExpiration);
using (var httpClient = new HttpClient(cacheHandler))
{
    for (int i = 1; i <= 5; ++i)
    {
        Console.Write($"Attempt {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));
    }
    Console.WriteLine();
}

//Checking cache stats
var stats = cacheHandler.StatsProvider.GetReport();
Console.WriteLine($"Cache stats - total requests: {stats.Total.TotalRequests}");
Console.WriteLine($"--> Hits: {stats.Total.CacheHit} [{stats.Total.TotalHitsPercent}]");
Console.WriteLine($"--> Misses: {stats.Total.CacheMiss} [{stats.Total.TotalMissPercent}]");
Console.ReadLine();

Console will show next output:

Attempt 1: https://randomuser.me/api/ --> OK Done in: 681 ms
Attempt 2: https://randomuser.me/api/ --> OK Done in: 75 ms
Attempt 3: https://randomuser.me/api/ --> OK Done in: 0 ms
Attempt 4: https://randomuser.me/api/ --> OK Done in: 0 ms
Attempt 5: https://randomuser.me/api/ --> OK Done in: 0 ms

Cache stats - total requests: 5
--> Hits: 4 [0,8]
--> Misses: 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 150 4/20/2023
0.1.4 150 4/4/2023
0.1.3 165 3/31/2023