HttpClient.Caching 1.3.7

.NET Standard 1.2 .NET Framework 4.6.2
dotnet add package HttpClient.Caching --version 1.3.7
NuGet\Install-Package HttpClient.Caching -Version 1.3.7
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="HttpClient.Caching" Version="1.3.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpClient.Caching --version 1.3.7
#r "nuget: HttpClient.Caching, 1.3.7"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install HttpClient.Caching as a Cake Addin
#addin nuget:?package=HttpClient.Caching&version=1.3.7

// Install HttpClient.Caching as a Cake Tool
#tool nuget:?package=HttpClient.Caching&version=1.3.7

HttpClient.Caching

Version Downloads

Download and Install HttpClient.Caching

This library is available on NuGet: https://www.nuget.org/packages/HttpClient.Caching/ Use the following command to install HttpClient.Caching using NuGet package manager console:

PM> Install-Package HttpClient.Caching

You can use this library in any .Net project which is compatible to .Net Framework 4.5+ and .Net Standard 1.2+ (e.g. Xamarin Android, iOS, Universal Windows Platform, etc.)

The Purpose of HTTP Caching

HTTP Caching affects both involved communication peers, the client and the server. On the server-side, caching is appropriate for improving throughput (scalability). HTTP caching doesn't make a single HTTP call faster but it can lead to better response performance in high-load scenarios. On the client-side, caching is used to avoid unnecessarily repetitiv HTTP calls. This leads to less waiting time on the client-side since cache reads have naturally a much better response performance than HTTP calls over relatively slow network links.

API Usage

Using MemoryCache

Declare IMemoryCache in your API service, either by creating an instance manually or by injecting IMemoryCache into your API service class.

private readonly IMemoryCache memoryCache = new MemoryCache();

Following example show how IMemoryCache can be used to store an HTTP GET result in memory for a given time span (cacheExpirection):

public async Task<TResult> GetAsync<TResult>(string uri, TimeSpan? cacheExpiration = null)
{
    var stopwatch = new Stopwatch();
    stopwatch.Start();

    TResult result;
    var caching = cacheExpiration.HasValue;
    if (caching && this.memoryCache.TryGetValue(uri, out result))
    {
        stopwatch.Stop();
        this.tracer.Debug($"{nameof(this.GetAsync)} for Uri '{uri}' finished in {stopwatch.Elapsed.ToSecondsString()} (caching=true)");
        return result;
    }

    var httpResponseMessage = await this.HandleRequest(() => this.httpClient.GetAsync(uri));
    var jsonResponse = await this.HandleResponse(httpResponseMessage);
    result = await Task.Run(() => JsonConvert.DeserializeObject<TResult>(jsonResponse, this.serializerSettings));

    if (caching)
    {
        this.memoryCache.Set(uri, result, cacheExpiration.Value);
    }
    else
    {
        this.memoryCache.Remove(uri);
    }

    stopwatch.Stop();
    this.tracer.Debug($"{nameof(this.GetAsync)} for Uri '{uri}' finished in {stopwatch.Elapsed.ToSecondsString()}");
    return result;
}
Using InMemoryCacheHandler

HttpClient allows to inject a custom http handler. In the follwing example, we inject an HttpClientHandler which is nested into an InMemoryCacheHandler where the InMemoryCacheHandler is responsible for maintaining and reading the cache.

static void Main(string[] args)
{
    const string url = "http://worldclockapi.com/api/json/utc/now";

    var httpClientHandler = new HttpClientHandler();
    var cacheExpirationPerHttpResponseCode = CacheExpirationProvider.CreateSimple(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(5));
    var handler = new InMemoryCacheHandler(httpClientHandler, cacheExpirationPerHttpResponseCode);
    using (var client = new HttpClient(handler))
    {
        // HttpClient calls the same API endpoint five times:
        // - The first attempt is called against the real API endpoint since no cache is available
        // - Attempts 2 to 5 can be read from cache
        for (var i = 1; i <= 5; i++)
        {
            Console.Write($"Attempt {i}: HTTP GET {url}...");
            var stopwatch = Stopwatch.StartNew();
            var result = client.GetAsync(url).GetAwaiter().GetResult();

            // Do something useful with the returned content...
            var content = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            Console.WriteLine($" completed in {stopwatch.ElapsedMilliseconds}ms");

            // Artificial wait time...
            Thread.Sleep(1000);
        }
    }

    Console.WriteLine();

    StatsResult stats = handler.StatsProvider.GetStatistics();
    Console.WriteLine($"TotalRequests: {stats.Total.TotalRequests}");
    Console.WriteLine($"-> CacheHit: {stats.Total.CacheHit}");
    Console.WriteLine($"-> CacheMiss: {stats.Total.CacheMiss}");
    Console.ReadKey();
}

Console output:

Attempt 1: HTTP GET http://worldclockapi.com/api/json/utc/now... completed in 625ms
Attempt 2: HTTP GET http://worldclockapi.com/api/json/utc/now... completed in 48ms
Attempt 3: HTTP GET http://worldclockapi.com/api/json/utc/now... completed in 1ms
Attempt 4: HTTP GET http://worldclockapi.com/api/json/utc/now... completed in 1ms
Attempt 5: HTTP GET http://worldclockapi.com/api/json/utc/now... completed in 1ms

TotalRequests: 5
-> CacheHit: 4
-> CacheMiss: 1

Cache keys

By default, requests will be cached by using a key which is composed with http method and url (only HEAD and GET http methods are supported). If this default behavior isn't enough you can implement your own ICacheKeyProvider wich provides cache key starting from HttpRequestMessage.

The following example show how use a cache provider of type MethodUriHeadersCacheKeysProvider. This cache key provider is already implemented and evaluates http method, specified headers and url to compose a cache key. with InMemoryCacheHandler.

static void Main(string[] args)
{
    const string url = "http://worldclockapi.com/api/json/utc/now";

    var httpClientHandler = new HttpClientHandler();
    var cacheExpirationPerHttpResponseCode = CacheExpirationProvider.CreateSimple(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(5));
    // this is a CacheKeyProvider which evaluates http method, specified headers and url to compose a key
    var cacheKeyProvider = new MethodUriHeadersCacheKeysProvider(new string[] { "FIRST-HEADER", "SECOND-HEADER" });
    var handler = new InMemoryCacheHandler(
        innerHandler: httpClientHandler,
        cacheExpirationPerHttpResponseCode: cacheExpirationPerHttpResponseCode,
        cacheKeysProvider: cacheKeyProvider
    );
    
    using (var client = new HttpClient(handler))
    {
        // HttpClient calls the same API endpoint five times:
        // - The first attempt is called against the real API endpoint since no cache is available
        // - Attempts 2 to 5 can be read from cache
        for (var i = 1; i <= 5; i++)
        {
            Console.Write($"Attempt {i}: HTTP GET {url}...");
            var stopwatch = Stopwatch.StartNew();
            var result = client.GetAsync(url).GetAwaiter().GetResult();

            // Do something useful with the returned content...
            var content = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            Console.WriteLine($" completed in {stopwatch.ElapsedMilliseconds}ms");

            // Artificial wait time...
            Thread.Sleep(1000);
        }
    }

    Console.WriteLine();

    StatsResult stats = handler.StatsProvider.GetStatistics();
    Console.WriteLine($"TotalRequests: {stats.Total.TotalRequests}");
    Console.WriteLine($"-> CacheHit: {stats.Total.CacheHit}");
    Console.WriteLine($"-> CacheMiss: {stats.Total.CacheMiss}");
    Console.ReadKey();
}

How-to: HTTP Caching for RESTful & Hypermedia APIs

License

This project is Copyright © 2022 Thomas Galliker. Free for non-commercial use. For commercial use please contact the author.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.2 netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Windows Phone wpa81
Windows Store netcore451
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on HttpClient.Caching:

Package Downloads
Terradue.Stars The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Stars is a set of services for working with Spatio Temporal Catalog such as STAC but not only

Neutron.Web.ClienteAPI

Biblioteca para utilizar a API do Software Neutron.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on HttpClient.Caching:

Repository Stars
DirtyRacer1337/Jellyfin.Plugin.PhoenixAdult
Jellyfin/Emby Metadata Provider for videos from multiple adult sites
ThePornDatabase/Jellyfin.Plugin.ThePornDB
Jellyfin/Emby Metadata Provider
Version Downloads Last updated
1.3.7 10,353 5/7/2022
1.3.5-pre 72 5/7/2022
1.3.3-pre 72 5/7/2022
1.2.6 37,944 8/29/2021
1.2.4-pre 182 8/29/2021
1.2.3 216 8/25/2021
1.2.1-pre 143 8/25/2021
1.1.21112.1 4,212 4/22/2021
1.1.20328.3-pre 238 11/23/2020
1.1.20328.1-pre 199 11/23/2020
1.0.19286.1-pre 2,702 10/13/2019
1.0.18336.1-pre 1,443 12/2/2018
1.0.18327.2-pre 484 11/23/2018
1.0.18326.3 6,577 11/22/2018
1.0.18326.1-pre 480 11/22/2018
1.0.18324.1-pre 471 11/20/2018
1.0.18323.7 587 11/19/2018

1.3.x
- Add ICacheKeysProvider which allows to specify custom cache keys

1.0.0
- Initial release