CodeByDay.ApiCache 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CodeByDay.ApiCache --version 1.0.0
NuGet\Install-Package CodeByDay.ApiCache -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="CodeByDay.ApiCache" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeByDay.ApiCache --version 1.0.0
#r "nuget: CodeByDay.ApiCache, 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 CodeByDay.ApiCache as a Cake Addin
#addin nuget:?package=CodeByDay.ApiCache&version=1.0.0

// Install CodeByDay.ApiCache as a Cake Tool
#tool nuget:?package=CodeByDay.ApiCache&version=1.0.0

Code By Day: API Cache

Adds a ServerCacheAttribute to intelligently cache responses on the server side.

Release Notes

1.0.0 First release

Usage Guide

Add the ServerCacheAttribute to your controller:

[ServerCache(Duration = 3600)]
public class MyController : Controller

And that's it! Your controller will now cache all requests for 3600 seconds (one hour)!

Controller Configuration

To configure the entire controller, modify the values the ServerCacheAttribute. All the attributes mentioned in the Method Configuration section are baked-in to the ServerCacheAttribute.

Implied Invalidations

If a controller is POSTed to, it will (by default) invalidate its cache. If more than the controller's cache needs to be invalidated, you can pass in the types for the other controllers to the ServerCacheAttribute constructor:

[ServerCache(typeof(MyOtherController), Duration = 3600)]
public class MyController : Controller

Method Configuration

If an individual method needs customization, you can append a DurationConfigurationAttribute, StatusCodeConfigurationAttribute, VerbConfigurationAttribute, or a DoNotCacheAttribute.

DurationConfigurationAttribute

The DurationConfigurationAttribute allows you to change how long a method is cached for.

StatusCodeConfigurationAttribute

The StatusCodeConfigurationAttribute allows you to specify the HTTP status codes which should be cached.

VerbConfigurationAttribute

The VerbConfigurationAttribute allows you to change the action on each verb: cache, don't cache, or invalidate existing cache. For example, if your endpoint is a POST, but should be cached, you can append the following attribute to the method:

[VerbConfigurationAttribute(Post = Cacheability.Cache)]

IEtagProvider

By default, ETags are generated by the time at which the response was generated. This can cause issues if your server's memory is cleared: the user will have a valid cached response, but the server will not know since that ETag is no longer in memory. You can add your own IEtagProvider attribute to override the ETag and use a more stable response.

IMPORTANT: If you create your own IEtagProvider attribute, you MUST put it before the ServerCacheAttribute:

[MyEtagProvider]
[ServerCache(Duration = 3600)]
public class MyController : Controller

An IEtagProvider attribute is also legal on a method. A method's attributes will be preferred over the controller's.

IKeyProvider

In order to avoid any possibility of cache collisions, the default IKeyProvider stores the query string and entire body as a string. This is how it identifies that two requests are the same. If your API generally receives requests with big bodies, you may consider creating your own IKeyProvider attribute to override this behavior and, perhaps, store a Hash of the body instead. You may also want to override the default behavior if you know two requests are equivalent without their bodies bing the same. An example would be { Name: "Joe", Age: 30 } and { Age: 30, Name: "Joe" }; the default handler will consider these two different requests, but a custom handler could notice that these are the same request and return the same key.

IMPORTANT: If you create your own IKeyProvider attribute, you MUST put it before the ServerCacheAttribute:

[MyKeyProvider]
[ServerCache(Duration = 3600)]
public class MyController : Controller

An IKeyProvider attribute is also legal on a method. A method's attributes will be preferred over the controller's.

IModifiedProvider

By default, the Last-Modified header is set to the time at which the response was generated. This is correct most of the time. However, if you have static files, you can create your own IModifiedProvider to read the write time on those static files and send that back instead.

IMPORTANT: If you create your own IModifiedProvider attribute, you MUST put it before the ServerCacheAttribute:

[MyModifiedProvider]
[ServerCache(Duration = 3600)]
public class MyController : Controller

An IModifiedProvider attribute is also legal on a method. A method's attributes will be preferred over the controller's.

Repo

Branches

The branches in this repo relate directly to the release they are building up to. Once a release has been made, the branch is no longer commited to. This effectively means there is no "master" branch: only releases.

Versioning

This project uses Semantic Versioning 2.0.0

I recommend editing your .csproj to force this Semantic Versioning. This example requires a version equal to or above 1.0.0 and disallows 2.0.0 or above.

<PackageReference Include="CodeByDay.ApiCache" Version="[1.0.0,2.0.0)" />
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.3.1 1,548 11/6/2020
1.3.0 1,429 10/22/2020
1.2.0 1,772 2/25/2020
1.1.7 1,715 12/6/2019
1.1.6 1,640 9/11/2019
1.1.5 1,730 9/8/2019
1.1.2 1,766 7/12/2019
1.1.1 1,768 7/10/2019
1.1.0 1,753 5/17/2019
1.0.0 1,914 4/14/2019