Class CacheContextServicesExtension
Extension methods to register the caches described in the Caching configuration section.
public static class CacheContextServicesExtension
- Inheritance
-
CacheContextServicesExtension
- Inherited Members
Methods
AddCacheContext(IServiceCollection, IConfiguration, string)
Registers the ICacheContext and the options of every cache declared in the configuration section. For the cache at position n of the Caches array,
the options are read from {sectionName}:Caches:n:Settings and registered according to its Kind (Memory, Redis, RedisSentinel, Sql or Dapr, case-insensitive; other kinds are ignored).
The ICache implementations themselves are provided by the Arc4u.Caching.* packages and registered with their own extension
(AddMemoryCacheKind(), AddRedisCacheKinds(), AddSqlCacheKind(), AddDaprCacheKind()); an IObjectSerialization must also be registered.
public static void AddCacheContext(this IServiceCollection services, IConfiguration configuration, string sectionName = "Caching")
Parameters
servicesIServiceCollectionThe service collection.
configurationIConfigurationThe application configuration.
sectionNamestringThe name of the configuration section. Default is
Caching.
Examples
With a configuration such as:
{
"Caching": {
"Default": "Volatile",
"Caches": [
{ "Name": "Volatile", "Kind": "Memory", "IsAutoStart": true, "Settings": { "SizeLimitInMB": 100 } }
]
}
}
the caches are registered in Program.cs with:
builder.Services.AddCacheContext(builder.Configuration);
builder.Services.AddMemoryCacheKind();
builder.Services.AddTransient<IObjectSerialization, JsonSerialization>();
Exceptions
- ArgumentNullException
configurationis null.- InvalidOperationException
The section does not exist or cannot be bound.
GetCacheContext(IServiceProvider)
Gets the registered ICacheContext.
public static ICacheContext? GetCacheContext(this IServiceProvider container)
Parameters
containerIServiceProviderThe service provider.
Returns
- ICacheContext
The ICacheContext, or null when it is not registered.