HttpClient.Cache 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package HttpClient.Cache --version 0.0.1
                    
NuGet\Install-Package HttpClient.Cache -Version 0.0.1
                    
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.Cache" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpClient.Cache" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="HttpClient.Cache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HttpClient.Cache --version 0.0.1
                    
#r "nuget: HttpClient.Cache, 0.0.1"
                    
#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.
#:package HttpClient.Cache@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HttpClient.Cache&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=HttpClient.Cache&version=0.0.1
                    
Install as a Cake Tool

HttpClient.Cache

HTTP/1.1 compliant caching layer for the .NET HttpClient. A prerelease package is availble on nuget HttpClient.Cache

It supports file based caching of responses based on the HTTP/1.1 caching headers specified in RFC7234.

Features

  • Pluggable caching for HttpClient
  • Customizable cache key computation (supports both shared and private caching)
  • Extensible cache entry and cache handler interfaces (defaults to file based caching)
  • Easy integration with dependency injection

Getting Started

Installation

Add the package reference to your project:

dotnet add package HttpClient.Cache

Usage

  1. Register the cache handler and related services on your HttpClient.
  2. Use the HttpClient as usual.

Note that all uses of the cache goes via HttpClient, so caching can easily be obtained using e.g generated clients such as Refit clients.

Example
// Register the caching client on a HttpClient in your Program.cs
services.AddHttpClient("cachedClient")
        .AddResponseCache(); // Defaults to using the shared, file based cache.

// Use the client as normally
var client = httpClientFactory.CreateClient("cachedClient");

var firstResponse = await client.GetAsync("https://example.com/"); // Returns a "non-private" Cache-Control header
var secondResponse = await client.GetAsync("https://example.com/");
Assert.Equal(CacheType.None, firstResponse.GetCacheType()); // This response was not obtained from cache.
Assert.Equal(CacheType.Shared, secondResponse.GetCacheType()); // This response was obtained from the shared (not private) cache.

Note that the usage here is different compared to e.g. Replicant.

Custom Caches

It is possible to use a custom cache for a client. Consider the following example:


var cache = new FileCache("/some/cache/directory");
services.AddHttpClient("cachedClient")
        .AddResponseCache(cache);

FileCache implements IHttpCache and this is an extension point if one wants to use a different, non-file based cache implementation.

FileCache Configuration

The FileCache class implements persistent caching by storing HTTP responses as files on disk. Below are the key properties of FileCache that can be used to for configuration:

  • DefaultInitialExpiration
    The default duration for which a cached entry remains valid after it is first seen if no explicit expiration is set in the response.

  • DefaultRefreshExpiration
    The default duration for which a cached entry remains valid when it is seen again if no explicit expiration is set in the response.

  • MaxEntries
    The maximum number of cache entries allowed in the cache directory. If the number of cached files exceeds this value, the cache will evict older or least-used entries to maintain the limit.

Key Components

  • CacheHandler: HTTP message handler that manages caching logic. This handler can be registered on a HttpClient as a message handler layer.
  • IHttpCache: Interface for cache storage implementations. The default file based implementation is FileCache.

Alternatives

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open issues or submit pull requests for improvements and bug fixes.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
0.1.4 114 9/10/2025
0.1.3 180 8/27/2025
0.1.2 155 6/24/2025
0.1.1 149 6/24/2025
0.1.0 149 6/16/2025
0.0.5 143 6/16/2025
0.0.4 140 6/16/2025
0.0.3 124 6/14/2025
0.0.2 122 6/14/2025
0.0.1 222 6/13/2025