Baubit.Caching.DI 2026.4.1

dotnet add package Baubit.Caching.DI --version 2026.4.1
                    
NuGet\Install-Package Baubit.Caching.DI -Version 2026.4.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="Baubit.Caching.DI" Version="2026.4.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Baubit.Caching.DI" Version="2026.4.1" />
                    
Directory.Packages.props
<PackageReference Include="Baubit.Caching.DI" />
                    
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 Baubit.Caching.DI --version 2026.4.1
                    
#r "nuget: Baubit.Caching.DI, 2026.4.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 Baubit.Caching.DI@2026.4.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=Baubit.Caching.DI&version=2026.4.1
                    
Install as a Cake Addin
#tool nuget:?package=Baubit.Caching.DI&version=2026.4.1
                    
Install as a Cake Tool

Baubit.Caching.DI

CircleCI codecov<br/> NuGet NuGet <br/> .NET Standard 2.0 License: MIT<br/> Known Vulnerabilities

Dependency injection modules for Baubit.Caching. Registers IOrderedCache<TId, TValue> in your DI container with configurable L1/L2 caching and service lifetimes.

Important: When using configuration-based module loading with concrete (non-generic) modules, you MUST call YourModuleRegistry.Register() before UseConfiguredServiceProviderFactory(). See Configuration-Based Loading for details.

Installation

dotnet add package Baubit.Caching.DI

Quick Start

Programmatic Module Loading

Load caching modules programmatically using IComponent. This is the recommended approach for generic cache modules.

using Baubit.Caching.DI;
using Baubit.DI;
using Baubit.DI.Extensions;
using Microsoft.Extensions.DependencyInjection;

public class AppComponent : Component
{
    protected override Result<ComponentBuilder> Build(ComponentBuilder builder)
    {
        return builder.WithModule<InMemory.Guid7.Module<string>, InMemory.Configuration>(config =>
        {
            config.IncludeL1Caching = true;
            config.L1MinCap = 128;
            config.L1MaxCap = 8192;
            config.CacheLifetime = ServiceLifetime.Singleton;
        }, config => new InMemory.Guid7.Module<string>(config));
    }
}

await Host.CreateEmptyApplicationBuilder(new HostApplicationBuilderSettings())
          .UseConfiguredServiceProviderFactory(componentsFactory: () => [new AppComponent()])
          .Build()
          .RunAsync();

Configuration-Based Loading

Create a concrete module by extending one of the generic modules and decorating it with [BaubitModule].

Step 1: Create a Concrete Module
using Baubit.Caching.DI;
using Baubit.DI;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;

namespace MyApp.Caching
{
    [BaubitModule("string-cache")]
    public class StringCacheModule : InMemory.Guid7.Module<string>
    {
        public StringCacheModule(IConfiguration configuration) : base(configuration) { }
        
        public StringCacheModule(InMemory.Configuration configuration, List<IModule> nestedModules = null) 
            : base(configuration, nestedModules) { }
    }
}
Step 2: Create Module Registry
using Baubit.DI;

namespace MyApp.Caching
{
    [GeneratedModuleRegistry]
    internal static partial class CachingModuleRegistry
    {
        // Register() method will be generated automatically
    }
}
Step 3: Register and Load

CRITICAL: You MUST call Register() on your module registry before any module loading operations. Forgetting this step will cause your modules to not be found and can lead to frustrating runtime errors.

using MyApp.Caching;

// MUST be called before UseConfiguredServiceProviderFactory()
CachingModuleRegistry.Register();

await Host.CreateApplicationBuilder()
          .UseConfiguredServiceProviderFactory()
          .Build()
          .RunAsync();
Step 4: Configure in appsettings.json
{
  "modules": [
    {
      "key": "string-cache",
      "configuration": {
        "includeL1Caching": true,
        "l1MinCap": 128,
        "l1MaxCap": 8192,
        "cacheLifetime": "Singleton"
      }
    }
  ]
}

Hybrid Loading

Combine with other modules from appsettings.json:

using MyApp.Caching;

// MUST call Register() first
CachingModuleRegistry.Register();

await Host.CreateApplicationBuilder()
          .UseConfiguredServiceProviderFactory(componentsFactory: () => [new AppComponent()])
          .Build()
          .RunAsync();

Keyed Service Registration

Register multiple cache instances with different keys for different use cases.

using Baubit.Caching.DI;
using Baubit.DI;
using Microsoft.Extensions.DependencyInjection;

public class AppComponent : Component
{
    protected override Result<ComponentBuilder> Build(ComponentBuilder builder)
    {
        return builder.WithModule<InMemory.Module<long, string>, InMemory.Configuration>(config =>
                      {
                          config.RegistrationKey = "user-cache";
                          config.CacheLifetime = ServiceLifetime.Singleton;
                      }, config => new InMemory.Module<long, string>(config))
                      .WithModule<InMemory.Module<long, string>, InMemory.Configuration>(config =>
                      {
                          config.RegistrationKey = "product-cache";
                          config.CacheLifetime = ServiceLifetime.Singleton;
                      }, config => new InMemory.Module<long, string>(config));
    }
}

// Resolve keyed services
var userCache = serviceProvider.GetKeyedService<IOrderedCache<long, string>>("user-cache");
var productCache = serviceProvider.GetKeyedService<IOrderedCache<long, string>>("product-cache");

Features

  • L1/L2 Caching: Optional bounded L1 layer with unbounded L2 storage
  • Configurable Lifetimes: Singleton, Transient, or Scoped registration
  • Keyed Service Registration: Register multiple cache instances with unique keys
  • Type-Safe Configuration: Strongly-typed configuration via IComponent
  • Sequential ID Generation: Long and GUID v7 ID generators included

Configuration

Property Type Default Description
IncludeL1Caching bool false Enable bounded L1 caching layer
L1MinCap int 128 Minimum capacity for L1 store
L1MaxCap int 8192 Maximum capacity for L1 store
CacheConfiguration Baubit.Caching.Configuration null Underlying cache configuration
CacheLifetime ServiceLifetime Singleton DI service lifetime (Singleton, Scoped, or Transient)
RegistrationKey string null Key for keyed service registration. When null, registered as non-keyed service

Available Modules

InMemory.Long.Module<TValue>

In-memory cache module with sequential long ID generation starting from 1.

InMemory.Guid7.Module<TValue>

In-memory cache module with monotonically increasing GUID v7 ID generation.

Keyed Service Registration

Register multiple cache instances with different keys:

public class AppComponent : Component
{
    protected override Result<ComponentBuilder> Build(ComponentBuilder builder)
    {
        return builder.WithModule<InMemory.Guid7.Module<string>, InMemory.Configuration>(config =>
                      {
                          config.RegistrationKey = "user-cache";
                          config.CacheLifetime = ServiceLifetime.Singleton;
                      }, config => new InMemory.Guid7.Module<string>(config))
                      .WithModule<InMemory.Guid7.Module<string>, InMemory.Configuration>(config =>
                      {
                          config.RegistrationKey = "product-cache";
                          config.CacheLifetime = ServiceLifetime.Singleton;
                      }, config => new InMemory.Guid7.Module<string>(config));
    }
}

// Resolve keyed services
var userCache = serviceProvider.GetKeyedService<IOrderedCache<Guid, string>>("user-cache");
var productCache = serviceProvider.GetKeyedService<IOrderedCache<Guid, string>>("product-cache");

Creating Custom Modules

Extend Module<TId, TValue, TConfiguration> to create custom cache modules with different storage backends or ID generation strategies.

Required Overrides

When creating a custom module, you must implement three abstract methods:

  • BuildL1DataStore(IServiceProvider): Creates the L1 (bounded) cache store
  • BuildL2DataStore(IServiceProvider): Creates the L2 (unbounded) cache store
  • BuildMetadata(IServiceProvider): Creates the metadata store for cache statistics

Optional Overrides

Two virtual methods are available for advanced customization:

  • BuildCacheEnumeratorCollectionFactory(IServiceProvider): Customize how enumerator collections are created (default: () => new CacheEnumeratorCollection<TId>())
  • BuildCacheEnumeratorFactory(IServiceProvider): Customize the async enumerator factory (default: new CacheAsyncEnumeratorFactory<TId, TValue>())

Example: Custom Module

using Baubit.Caching;
using Baubit.Caching.DI;
using Baubit.DI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;

namespace MyApp.Caching
{
    public class RedisConfiguration : Configuration
    {
        public string ConnectionString { get; set; }
        public int DatabaseNumber { get; set; } = 0;
    }

    public abstract class RedisModule<TId, TValue> : Module<TId, TValue, RedisConfiguration> 
        where TId : struct, IComparable<TId>, IEquatable<TId>
    {
        protected RedisModule(RedisConfiguration configuration, List<IModule> nestedModules = null) 
            : base(configuration, nestedModules) { }

        protected override IStore<TId, TValue> BuildL1DataStore(IServiceProvider serviceProvider)
        {
            // Implement Redis-backed L1 store
            throw new NotImplementedException();
        }

        protected override IStore<TId, TValue> BuildL2DataStore(IServiceProvider serviceProvider)
        {
            // Implement Redis-backed L2 store
            throw new NotImplementedException();
        }

        protected override IMetadata<TId> BuildMetadata(IServiceProvider serviceProvider)
        {
            // Implement Redis-backed metadata store
            throw new NotImplementedException();
        }

        // Optional: Override to customize cache enumerator collection factory
        protected override Func<CacheEnumeratorCollection<TId>> BuildCacheEnumeratorCollectionFactory(IServiceProvider serviceProvider)
        {
            // Default implementation returns () => new CacheEnumeratorCollection<TId>()
            return base.BuildCacheEnumeratorCollectionFactory(serviceProvider);
        }

        // Optional: Override to customize cache async enumerator factory
        protected override ICacheAsyncEnumeratorFactory<TId, TValue> BuildCacheEnumeratorFactory(IServiceProvider serviceProvider)
        {
            // Default implementation returns new CacheAsyncEnumeratorFactory<TId, TValue>()
            return base.BuildCacheEnumeratorFactory(serviceProvider);
        }
    }
}

Dependencies

License

MIT

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.  net9.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework 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 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 (1)

Showing the top 1 NuGet packages that depend on Baubit.Caching.DI:

Package Downloads
Baubit.Caching.LiteDB.DI

Dependency injection module for Baubit.Caching.LiteDB. Registers IOrderedCache with LiteDB-backed L2 storage in your DI container.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.4.1 155 1/25/2026
2026.2.2-prerelease 164 1/10/2026
2026.2.1-prerelease 95 1/8/2026
2026.1.2-prerelease 97 1/3/2026
2026.1.1 209 12/31/2025
2026.1.1-prerelease 99 12/31/2025
2025.51.3 128 12/21/2025
2025.51.2 281 12/19/2025
2025.51.1 265 12/19/2025
2025.49.3 265 12/4/2025
2025.49.2 683 12/3/2025
2025.1.1-prerelease 97 12/31/2025