This is the default application cache implementation, included in the core package, and is used if no other cache module is registered explicitly. The In-memory cache module caches objects in memory of the local server instance. The in-memory cache is suited for almost all application types, even in load-balanced scenarios where a distributed service bus will take care of cache invalidation. Only for very large load-balanced applications with many nodes, an out-of-process cache (such as Redis or Memcached) is recommended to reduce data store requests.

Installation

The In-memory cache module is already part of the Redakt core and does not need to be installed separately.

Service registration

The in-memory cache module will be registered by default if no other application cache module has been registered. If you want to explicitly register In-memory cache services in order to pass a configuration delegate, you can do so in the ConfigureServices() method of your project's Startup.cs file by adding an AddInMemoryCache() call to the Redakt builder instance. The order in which services are added to the Redakt builder is not important.

public void ConfigureServices(IServiceCollection services)
{
    // ... framework and other services here

    var builder = services.AddRedakt();
    builder.AddInMemoryCache();  // Optionally pass Action<InMemoryCacheOptions> parameter.
    // ... other Redakt services
}

Configuration settings

The In-memory cache is configured through the appsettings.json file. Any configuration that is not included in the appsettings.json file will be set to its default values. Additionally, an Action<InMemoryCacheOptions> configuration delegate may be passed to the AddInMemoryCache() call. Configuration set through this delegate takes priority overrides values in the appsettings.json file.

{
    "Redakt": {
        "ApplicationCache": {
            "InMemory": {
                "SlidingExpirationSeconds": 300,
                "AbsoluteExpirationSeconds": 3600,
                "CacheInvalidationDelayMilliseconds": 0
            }
        }
    }
}

Sliding Expiration

Sets the sliding cache expiration in seconds. A cache item will be purged from the cache if it hasn't been requested for this amount of time. Setting a lower value will reduce memory usage but may result in a higher database load. Defaults to 300 (5 minutes) if not set.

Absolute Expiration

Sets the absolute cache expiration in seconds. After this time a cache item will always be purged from the cache, no matter how often it is requested. Defaults to 3600 (1 hour) if not set.

Cache Invalidation Delay

Sets the delay in milliseconds before a cache item is purged from the cache after an invalidation event. This is useful when using an eventually consistent data store, where an immediate subsequent request from the data store may retrieve and re-cache a stale object.