ActionCache 0.0.9

dotnet add package ActionCache --version 0.0.9                
NuGet\Install-Package ActionCache -Version 0.0.9                
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="ActionCache" Version="0.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ActionCache --version 0.0.9                
#r "nuget: ActionCache, 0.0.9"                
#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 ActionCache as a Cake Addin
#addin nuget:?package=ActionCache&version=0.0.9

// Install ActionCache as a Cake Tool
#tool nuget:?package=ActionCache&version=0.0.9                

ActionCache

NuGet Version NuGet Downloads

Register with MemoryCache

Use the AddActionCache extension method to register IMemoryCache as a cache store. The configuration for MemoryCacheOptions is exposed as a parameter to UseMemoryCache.

builder.Services.AddActionCache(options => 
{
    options.UseMemoryCache(...);
});

Register with Redis

Use the AddActionCache extension method to register RedisCache as a cache store. The configuration for RedisCacheOptions is exposed as a parameter to UseRedisCache.

builder.Services.AddActionCache(options => 
{
    options.UseRedisCache(...);
});

Register with SqlServer

Use the AddActionCache extension method to register SqlServerCache as a cache store. The configuration for SqlServerCacheOptions is exposed as a parameter to UseSqlServerCache.

builder.Services.AddActionCache(options => 
{
    options.UseSqlServerCache(...);
});

Register with Azure Cosmos

Use the AddActionCache extension method to register CosmosClient as a cache store. The configuration for AzureCosmosCacheOptions is exposed as a parameter to UseAzureCosmosCache.

builder.Services.AddActionCache(options => 
{
    options.UseAzureCosmosCache(options =>
    {
        options.DatabaseId = "MyDatabase";
        options.ConnectionString =
            configuration.GetValue<string>("CosmosDb:ConnectionString");
    });
});

[!NOTE] Both a DatabaseId and ConnectionString are required. The only requirement within Azure is to create an Azure Cosmos DB account and use that primary connection string in the above configuration. A database and container will be created automatically if they don't already exist.

Register Multiple Cache Stores

Two or more cache stores can be combined.

builder.Services.AddActionCache(options => 
{
    options.UseMemoryCache(...);
    options.UseRedisCache(...);
    options.UseSqlServerCache(...);
});

Basic Usage

Add an ActionCacheAttribute to any controller actions that should be cached. There is a mandatory parameter for the cache namespace which will prefix all entries with whatever is specified.

[HttpPost]
[Route("/")]
[ActionCache(Namespace = "MyNamespace")]
public IActionResult Post() 
{
}

Cache Key Creation

Both the route values and the action arguments are serialized then encoded to generate the cache key suffix. This suffix is appended to the string "ActionCache:{Namespace}".

[!NOTE] Any route data from the request, i.e. the area, controller and action names as well as parameters are also added to the key. This is to support automatic cache refreshing.

Cache Eviction

An ActionCacheEvictionAttribute can be applied to a controller action. A cache eviction occurs at the namespace level. One or more namespaces can be used separated by a comma. In the example below, both MyNamespace and MyOtherNamespace would have their entries evicted on a successful execution of the action.

[HttpDelete]
[Route("/")]
[ActionCacheEviction(Namespace = "MyNamespace, MyOtherNamespace")]
public IActionResult Delete()
{
}

Cache Refresh

An ActionCacheRefreshAttribute can be applied to a controller action. A cache refresh occurs at the namespace level. Any entries currently in the cache will be refetched by executing their corresponding controller action and repopulating the cache. This is done automatically because all of the route details are persisted into the cache key.

[HttpPut]
[Route("/")]
[ActionCacheRefresh(Namespace = "MyNamespace")]
public IActionResult Put()
{
}

Route Templates for Namespaces

A namespace, i.e. a cache key, can contain route template parameters. In the case below, cache namespaces will vary on the route parameter of id. This means that each unique Account will have it's own namespace where differing values of offset will be stored in their corresponding cache namespace.

[HttpGet]
[Route("{id}")]
[ActionCache(Namespace = "Account:{id}")]
public async Task<IActionResult> Get(Guid id, DateTime offset)
{
}

[!NOTE] This is beneficial because actions like evicting or refreshing cache entries can be done at the namespace level.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  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. 
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.0.9 70 2/14/2025
0.0.8 78 1/31/2025
0.0.7 119 12/13/2024
0.0.6 98 11/14/2024
0.0.5 106 10/27/2024
0.0.4 126 10/20/2024
0.0.3 91 10/14/2024
0.0.2 122 8/7/2024
0.0.1 111 7/14/2024