IMEX.CORE 2.0.6

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

πŸ₯ IMEX.Core

.NET .NET .NET License NuGet Build Status

IMEX.Core library for API systems – High-performance Hybrid Cache, SqlSugar ORM, unified Fluent API (AddImexCore, CacheConfigurationBuilder, SqlSugarOptionsBuilder). Configure Cache/SqlSugar/Monitoring/RateLimit via builders; Fluent API evaluation: complete for Cache, SqlSugar, and Middleware.

πŸ“‹ Table of Contents

πŸš€ Introduction

IMEX.Core is a core library for APIs: high-performance Hybrid Cache, SqlSugar ORM, and unified Fluent API to configure Cache, SqlSugar, Monitoring, RateLimit, and Middleware from Program.cs.

🎯 Goals

  • Complete Fluent API: ImexCoreOptions (AddImexCore), CacheConfigurationBuilder, SqlSugarOptionsBuilder; UseRateLimiting(configure), UseMonitoring(configure), UseImexCore(app, configure), ConfigureMonitoring
  • High performance: Cache 199ns L1 hit, 40M ops/sec; optimized database access and memory usage
  • Easy to use: Configure via builders (Use* / Configure* / On*); documentation and Fluent API evaluation in docs/FLUENT-API-EVALUATION.md
  • Production-ready: ISmartCache registered by default, monitoring, rate limiting, memory pressure management
  • Large-Scale Support: 5K–20K concurrent users with Redis Cluster

✨ Key Features

πŸ”§ Fluent API (Unified Configuration)

  • AddImexCore(options β‡’ ...): Single entry point – UseCache, UseCachePreset, ConfigureCache, UseSqlSugar() / UseSqlSugar(configure), UseRateLimiting(configure), UseMonitoring(configure), UseCacheWarming, UseMemoryPressure
  • CacheConfigurationBuilder: UseMemoryCache/Hybrid/Redis, WithPreset, ConfigureL1, ConfigureHybrid, ConfigureMonitoring, WithRateLimiting(configure); ISmartCache registered for Memory/Hybrid
  • SqlSugarOptionsBuilder: Use* / Configure* / On* – NOLOCK, slow query, callbacks, MoreSettings (SQL Server, PostgreSQL, SQLite, Oracle)
  • UseImexCore(app, configure): Middleware options (SetApplicationRunningState). Full evaluation: docs/FLUENT-API-EVALUATION.md

πŸ—„οΈ Database & ORM

  • SqlSugar ORM: Multi-database, read/write splitting; configure via SqlSugarOptionsBuilder
  • Migration System: Automatic migration and seeding
  • Multi-tenant: Multi-tenant architecture support
  • Connection Pooling: Optimized database connections

πŸš€ Caching System (Enterprise-Grade)

Core Features
  • Multi-tier Cache: L1 (Memory) + L2 (Redis) + Hybrid with optimized architecture
  • Smart Compression: Auto-compress large data with LZ4 and MessagePack
  • Adaptive TTL: Auto-adjust cache duration with jittering
  • Lock-free Operations: Thread-safe, high-performance with object pooling
  • Cache Invalidation: RemoveByPrefix, tag-based removal, pattern matching
  • Background Refresh: Stale-While-Revalidate before expiry
Large-Scale Features (NEW)
  • Redis Cluster/Sentinel: Redis Cluster and Sentinel mode with automatic failover
  • Consistent Hash Ring: Jump consistent hashing for key distribution with virtual nodes
  • Cache Warming Service: Pre-populate cache on startup with priority-based warming
  • Adaptive Eviction Policies: LRU, LFU, SizeBased, TTL, Random, Adaptive, Composite
  • Memory Pressure Management: Auto-evict cache under memory pressure
  • Batch Operations: GetManyAsync, SetManyAsync, RemoveManyAsync with pipelining
  • Multi-Tenant Isolation: Auto-inject tenant ID into cache keys
  • Distributed Tracing: OpenTelemetry integration with Prometheus export
  • Circuit Breaker: Enhanced circuit breaker with adaptive configuration
Performance Features
  • Object Pooling: 90% reduction in memory allocations
  • Zero-Allocation Operations: Span-based string and memory operations
  • CreateKey zero-alloc: FusionStyleOptimizations.CreateKeyThreeParts / CreateKeyTwoParts (string.Create) for EnhancedMemoryCache, CachePerformanceOptimizer, ResilientRedisClusterProvider β€” single allocation per key
  • Decision Metrics: ThreadLocalMetricsBuffer.GetDecisionMetrics() returns HitRatio, ErrorRate, P50Ms, P95Ms, P99Ms for TTL, warming, skip L2, circuit. Call RecordError() on operation failure to update error rate
  • Single-Flight Pattern: Prevents duplicate expensive computations
  • Fail-Safe Cache: Stale data fallback on failure
  • Health Monitoring: Real-time health checks and metrics
  • CacheGetOptionsPool (optional): CacheGetOptionsPool.Rent() / Return(options) when profiling shows options allocation as bottleneck. ValueTask path: use GetIfCachedAsync for L1-only read
World-class mode (FusionCache-style)
  • L1 store-by-reference: EnableL1Compression = false (default) β€” L1 stores object reference, no compression β†’ very fast Get/Set for large objects (100KB+), minimal allocation. Set EnableL1Compression = true when saving RAM (adaptive compression by memory pressure)
  • Per-call SkipDistributedLocker: CacheGetOptions.SkipDistributedLocker = true β€” skip distributed lock for that call to reduce latency/contention (like FusionCache SetSkipDistributedLocker)
  • SetMemoryEfficient: Always compress (use when memory savings are required)

πŸ” Security & Authentication

  • Multi-encryption: RSA, SM2, SM3, SM4
  • JWT Authentication: Stateless authentication
  • Role-based Access Control: Bitwise permission system
  • Data Encryption: End-to-end encryption
  • Audit Logging: Comprehensive security logging

πŸ“Š Logging & Monitoring

  • Structured Logging: Serilog with Elasticsearch and custom sinks
  • Performance Monitoring: Real-time metrics with OpenTelemetry and Prometheus
  • Cache Analytics: Hit/miss ratios, response times, memory usage tracking
  • Error Tracking: Comprehensive error handling with circuit breaker metrics
  • Health Checks: Auto health monitoring for cache and database connections

πŸ› οΈ Utilities & Helpers

  • File Operations: Secure file handling with encryption
  • Data Conversion: Number to text, encoding/decoding with performance optimization
  • Validation: Input validation helpers with async support
  • HTTP Utilities: Request/response helpers with retry policies
  • ID Generation: Snowflake ID, custom generators with thread safety
  • Memory Management: Object pooling and memory-efficient operations

πŸ“¦ Installation

NuGet Package

# Package Manager
Install-Package IMEX.CORE -Version 2.0.5

# .NET CLI
dotnet add package IMEX.CORE --version 2.0.5

# Paket
paket add IMEX.CORE --version 2.0.5

System requirements

  • .NET 8.0, .NET 9.0, or .NET 10.0
  • Redis 6.0+ (optional, for distributed cache)
  • Elasticsearch 7.0+ (optional, for logging)

πŸ†• Latest (Fluent API & Package):

  • Unified Fluent API: AddImexCore, CacheConfigurationBuilder, SqlSugarOptionsBuilder; UseRateLimiting(configure), UseMonitoring(configure), UseImexCore(app, configure), ConfigureMonitoring
  • ISmartCache registered in CacheConfigurationBuilder for Memory/Hybrid
  • L1 Cache Hit: 199ns (5x faster than target), GetAsync: 320ns
  • Throughput: 40M ops/sec
  • Lock contention: 0.01% (near zero)
  • Memory/Operation: 192B
  • Tested with 100+ threads, real production config
  • Details: docs/PRODUCTION_REALISTIC_RESULTS.md
  • Release notes: RELEASE_NOTES.md
  • Samples: CacheTestApp Β· HealthCheckExample
  • Benchmarks: How to run benchmarks

βš™οΈ Configuration

1. Database Configuration

{
  "MainDB": "DB_Main",
  "MultiDBEnabled": true,
  "DBConnections": [
    {
      "ConnId": "DB_Main",
      "DBType": 1,
      "Enable": true,
      "HitRate": 50,
      "ConnectionStr": "Data Source=localhost;Initial Catalog=MedcomHR;Integrated Security=True;",
      "ProviderName": "System.Data.SqlClient",
      "Slaves": [
        {
          "HitRate": 0,
          "Connection": "Data Source=slave-server;Initial Catalog=MedcomHR;Integrated Security=True;"
        }
      ]
    }
  ]
}

2. Complete Cache Configuration

{
  "Cache": {
    "Hybrid": {
      "L2TtlMultiplier": 3.0,
      "L1PromotionTtlMinutes": 30,
      "EnableFactoryTimeouts": true,
      "SoftTimeoutMs": 500,
      "HardTimeoutMs": 20000,
      "FailSafeTtlMultiplier": 5.0,
      "EnableRedisPubSubSync": true,
      "EnableSignalRSync": true,
      "HealthCheckIntervalSeconds": 30,
      "DefaultLockTimeoutSeconds": 30
    },
    "EnhancedMemoryCache": {
      "BackgroundRefreshRatio": 0.7,
      "BackgroundRefreshIntervalSeconds": 45,
      "CleanupSampleSize": 1500,
      "CleanupSamplingRate": 0.08,
      "AdaptiveTtl": {
        "HighHitRatioThreshold": 0.85,
        "LowHitRatioThreshold": 0.6,
        "HighPerformanceMultiplier": 2.0,
        "MaxTtlMultiplier": 4.0,
        "MinTtlMinutes": 5
      }
    },
    "Medical": {
      "PatientDataCacheMinutes": 90,
      "MedicalRecordsCacheMinutes": 180,
      "VitalSignsCacheMinutes": 15,
      "AllergiesCacheMinutes": 360,
      "MedicationsCacheMinutes": 120
    },
    "Warming": {
      "EnableWarmingOnStartup": true,
      "MaxConcurrentWarmingTasks": 4,
      "WarmingTaskTimeoutSeconds": 30,
      "TotalWarmingTimeoutMinutes": 5,
      "BatchSize": 50,
      "RetryCount": 3
    },
    "Tracing": {
      "EnableTracing": true,
      "SamplingRate": 0.1,
      "ServiceName": "EMR-Cache",
      "EnableMetrics": true,
      "EnableDetailedAttributes": false
    },
    "Eviction": {
      "PolicyType": "Adaptive",
      "MemoryPressureThreshold": 80,
      "MaxEntriesPerEviction": 1000
    },
    "LargeScale": {
      "ExpectedConcurrentUsers": 10000,
      "EnableCacheWarming": true,
      "EnableTracing": true,
      "TracingSamplingRate": 0.1
    },
    "BatchOperations": {
      "MaxBatchSize": 500,
      "MaxConcurrentBatches": 10,
      "BatchTimeoutSeconds": 30,
      "EnablePipelining": true
    },
    "RedisCluster": {
      "EnableCluster": false,
      "EnableSentinel": false,
      "InstanceName": "EMR-Cache",
      "VirtualNodesPerPhysical": 150,
      "ClusterEndpoints": [],
      "SentinelEndpoints": [],
      "SentinelServiceName": "mymaster"
    },
    "MultiTenant": {
      "Enabled": false,
      "AllowEmptyTenant": true,
      "TrackKeysPerTenant": true,
      "TenantHeaderName": "X-Tenant-Id"
    },
    "Decorators": {
      "EnableTracingDecorator": true,
      "EnableTenantDecorator": false
    }
  },
  "Redis": {
    "Enable": true,
    "ConnectionString": "localhost:6379",
    "Database": 0,
    "KeyPrefix": "emr:"
  }
}

3. Logging Configuration

{
  "Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"
        }
      },
      {
        "Name": "Elasticsearch",
        "Args": {
          "nodeUris": "http://localhost:9200",
          "indexFormat": "medcom-hr-logs-{0:yyyy.MM.dd}"
        }
      }
    ]
  }
}

πŸ“š Usage Guide

Namespaces: AddImexCore, UseImexCore, CacheMode, ImexCoreOptions nαΊ±m trong IMEX.CORE.Extensions; CachePreset lΓ  CacheConfigurationBuilder.CachePreset trong IMEX.CORE.Caching.Configuration.

using IMEX.CORE.Extensions;
using IMEX.CORE.Caching.Configuration;

// Program.cs (.NET 6+)
var builder = WebApplication.CreateBuilder(args);

// βœ… Option 1: Minimal setup (Memory Cache + SqlSugar)
builder.Services.AddImexCore();

// βœ… Option 2: Full configuration
builder.Services.AddImexCore(options =>
{
    options.UseCache(CacheMode.Hybrid, "localhost:6379");
    options.UseCachePreset(CacheConfigurationBuilder.CachePreset.HighPerformance);
    options.UseSqlSugar();
    options.UseMonitoring();
    options.UseCacheWarming();
});

// βœ… Option 3: From appsettings.json
builder.Services.AddImexCore(builder.Configuration);

// βœ… Option 4: Detailed Rate Limiting & Monitoring (Fluent)
builder.Services.AddImexCore(options =>
{
    options.UseCache(CacheMode.Hybrid, "localhost:6379");
    options.UseSqlSugar();
    options.UseRateLimiting(config =>
    {
        config.TokensPerSecond = 200;
        config.MaxConcurrentOperations = 80;
        config.QueueLimit = 150;
    });
    options.UseMonitoring(config =>
    {
        config.RefreshIntervalSeconds = 15;
        config.MinHitRatio = 0.80;
        config.LatencyWarningMs = 1.5;
    });
});

var app = builder.Build();
app.UseImexCore(); // Default: App.IsRun = true

// Or configure middleware (e.g. disable setting application state)
app.UseImexCore(opts => opts.SetApplicationRunningState = false);

app.Run();

appsettings.json Configuration

{
  "ImexCore": {
    "CacheMode": "Hybrid",
    "RedisConnection": "localhost:6379",
    "EnableMonitoring": true,
    "EnableCacheWarming": true,
    "UseSqlSugar": true
  }
}

Extended usage (Fluent)

When using AddImexCore, you can configure in detail via these overloads:

Method Description Example
UseRateLimiting(configure) Enable rate limiting and configure (tokens/s, concurrent, queue) options.UseRateLimiting(c => { c.TokensPerSecond = 200; })
UseMonitoring(configure) Enable monitoring and configure dashboard (refresh, thresholds) options.UseMonitoring(c => { c.RefreshIntervalSeconds = 15; })
UseSqlSugar(configure) Enable SqlSugar and Fluent config (NOLOCK, slow query, …) See SqlSugarOptionsBuilder
ConfigureCache(configure) Detailed CacheConfigurationBuilder configuration options.ConfigureCache(b => b.ConfigureL1(...))

Middleware: app.UseImexCore(configure) – configure middleware (e.g. SetApplicationRunningState = false to avoid setting App.IsRun = true).

Cache (direct AddCache()): use .ConfigureMonitoring(configure) to configure the health dashboard (RefreshIntervalSeconds, MinHitRatio, LatencyWarningMs, …).


πŸ—ƒοΈ Cache configuration (Fluent API)

Cache Strategies

Strategy Description Use Case
UseMemoryCache() L1 in-memory only Single server, development
UseHybridCache(redis) L1 Memory + L2 Redis Multi-server, production
UseRedisCache(redis) Redis only Stateless containers
UseAutoStrategy() Auto-detect Auto-scaling environments

Cache Presets

Preset MaxEntries MaxMemory BackgroundRefresh Use Case
HighPerformance 5000 1GB 90% High-traffic APIs
MemoryOptimized 1000 256MB 70% Limited RAM
HighReliability 3000 512MB 80% Critical apps
HybridProduction 2000 512MB 85% Multi-server

Fluent API - Full Example

using IMEX.CORE.Caching.Configuration;

// 🎯 Option 1: Fluent Builder (Recommended)
services.AddCache()
    // Choose strategy
    .UseHybridCache("localhost:6379", instanceName: "MyApp")
    
    // Choose preset
    .WithPreset(CachePreset.HighPerformance)
    
    // Optional features
    .WithMonitoring()           // Health dashboard
    .WithMetrics()              // Prometheus metrics
    .WithRateLimiting(config => // Rate limiting
    {
        config.TokensPerSecond = 100;
        config.MaxConcurrentOperations = 50;
    })
    .WithMemoryPressureManagement() // Auto-eviction when RAM is low
    
    // Configure Monitoring/Dashboard (refresh interval, thresholds)
    .ConfigureMonitoring(config =>
    {
        config.RefreshIntervalSeconds = 20;
        config.MaxHistoryCount = 240;
        config.MinHitRatio = 0.75;
        config.LatencyWarningMs = 2.0;
    })
    
    // Custom L1 configuration
    .ConfigureL1(config =>
    {
        config.FailSafe.MaxEntries = 10000;
        config.FailSafe.MaxTotalBytes = 2L * 1024 * 1024 * 1024; // 2GB
        config.BackgroundRefreshRatio = 0.85;
        config.AdaptiveTtl.HighHitRatioThreshold = 0.9;
        config.AdaptiveTtl.HighPerformanceMultiplier = 2.5;
    })
    
    // Custom Hybrid configuration
    .ConfigureHybrid(config =>
    {
        config.EnableL2Cache = true;
        config.L2TtlMultiplier = 3.0;
        config.EnableBackgroundRefresh = true;
        config.SoftTimeout = TimeSpan.FromSeconds(3);
        config.HardTimeout = TimeSpan.FromSeconds(20);
    })
    .Build();

Quick Setup Methods

// πŸš€ High Performance (Memory or Hybrid)
services.AddHighPerformanceCache();                        // Memory only
services.AddHighPerformanceCache("localhost:6379");        // Hybrid vα»›i Redis

// πŸ’Ύ Memory Optimized (tiαΊΏt kiệm RAM)
services.AddMemoryOptimizedCache();

// 🏭 Production Hybrid (full features)
services.AddProductionHybridCache(
    redisConnectionString: "localhost:6379",
    enableRateLimiting: true,
    enableMemoryPressure: true
);

Cache Configuration in appsettings.json

{
  "Cache": {
    "EnhancedMemoryCache": {
      "BackgroundRefreshRatio": 0.85,
      "BackgroundRefreshIntervalSeconds": 30,
      "CleanupSampleSize": 2000,
      "CleanupSamplingRate": 0.15,
      "FailSafe": {
        "MaxEntries": 5000,
        "MaxTotalBytes": 536870912,
        "DefaultTtlMinutes": 45,
        "AlwaysCompressSnapshot": true
      },
      "AdaptiveTtl": {
        "HighHitRatioThreshold": 0.85,
        "LowHitRatioThreshold": 0.5,
        "HighPerformanceMultiplier": 1.8,
        "MaxTtlMultiplier": 2.5,
        "MinTtlMinutes": 2
      },
      "SingleFlight": {
        "HighConcurrencyThreshold": 256,
        "WaitForExistingHighMs": 1500,
        "FactoryHardTimeoutMs": 8000
      }
    },
    "Hybrid": {
      "EnableL2Cache": true,
      "L2TtlMultiplier": 2.5,
      "L1PromotionTtlMinutes": 25,
      "EnableFactoryTimeouts": true,
      "SoftTimeoutMs": 3000,
      "HardTimeoutMs": 20000,
      "FailSafeTtlMultiplier": 1.2
    }
  },
  "Redis": {
    "Enable": true,
    "ConnectionString": "localhost:6379",
    "InstanceName": "MyApp",
    "DefaultExpiration": "00:45:00",
    "EnableCircuitBreaker": true,
    "CbErrorRateThreshold": 0.08,
    "CbBreakSeconds": 20,
    "RetryCount": 3
  }
}

πŸ—„οΈ SqlSugar configuration

Setup cΖ‘ bαΊ£n

// Program.cs
builder.Services.AddSqlSugarSetup();

// Or via AddImexCore
builder.Services.AddImexCore(options =>
{
    options.UseSqlSugar();
});

Using SqlSugarOptionsBuilder in Program.cs

When you need to customize SqlSugar (AutoCloseConnection, WithNoLock, SlowQuery, callbacks…), use SqlSugarOptionsBuilder in one of two ways:

Option 1: Call AddSqlSugarSetup directly with a lambda

// Program.cs
using IMEX.CORE.DBAccess;
using IMEX.CORE.DBAccess.Options;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSqlSugarSetup(options => options
    .UseAutoCloseConnection(true)
    .UseWithNoLock(true)                    // SQL Server: WITH (NOLOCK)
    .UseSlowQueryDetection(thresholdMs: 3000, samplingRate: 1)
    .UseDiffLog(true)
    .ConfigureMoreSettings(settings =>
    {
        settings.IsAutoRemoveDataCache = true;
        settings.SqlServerCodeFirstNvarchar = true;
    })
    .OnSlowQueryDetected((sql, parameters, duration) =>
    {
        Console.WriteLine($"Slow query: {duration}ms - {sql}");
    })
    .OnError(ex =>
    {
        // Handle SqlSugar errors
    }));

var app = builder.Build();
app.Run();

Option 2: Via AddImexCore with overload UseSqlSugar(Action<SqlSugarOptionsBuilder>)

// Program.cs
builder.Services.AddImexCore(options =>
{
    options.UseCache(CacheMode.Memory);
    options.UseSqlSugar(sqlSugar => sqlSugar
        .UseAutoCloseConnection(true)
        .UseWithNoLock(true)
        .UseSlowQueryDetection(3000)
        .OnSlowQueryDetected((sql, parameters, duration) =>
        {
            // Custom handling
        }));
});

Common builder methods:

Method Description
UseAutoCloseConnection(bool) Close connection after each query
UseWithNoLock(bool) SQL Server: use WITH (NOLOCK)
UseSlowQueryDetection(thresholdMs, samplingRate) Enable slow query detection
UseDiffLog(bool) Enable Diff Log for audit
ConfigureMoreSettings(Action<ConnMoreSettings>) Configure MoreSettings in detail
OnSlowQueryDetected(Action<sql, parameters, duration>) Callback when slow query is detected
OnError(Action<SqlSugarException>) Callback on SQL error

Database Configuration (appsettings.json)

{
  "MainDB": "DB_Main",
  "DBConnections": [
    {
      "ConnId": "DB_Main",
      "DBType": 1,
      "Enable": true,
      "HitRate": 50,
      "ConnectionStr": "Server=localhost;Database=MyDB;User Id=sa;Password=xxx;",
      "Slaves": [
        {
          "HitRate": 30,
          "ConnectionStr": "Server=slave1;Database=MyDB;User Id=sa;Password=xxx;"
        },
        {
          "HitRate": 20,
          "ConnectionStr": "Server=slave2;Database=MyDB;User Id=sa;Password=xxx;"
        }
      ]
    },
    {
      "ConnId": "DB_Log",
      "DBType": 1,
      "Enable": true,
      "HitRate": 0,
      "ConnectionStr": "Server=localhost;Database=MyDB_Log;User Id=sa;Password=xxx;"
    }
  ]
}

Database Types (DBType)

DBType Database Description
0 MySql MySQL/MariaDB
1 SqlServer Microsoft SQL Server
2 Sqlite SQLite
3 Oracle Oracle Database
4 PostgreSQL PostgreSQL
5 Dm DM Database
6 Kdbndp Kingbase

(SqlSugar SQL support)

SqlSugar AOP Configuration

{
  "AppSettings": {
    "SqlAOP": {
      "EnableSlowQueryDetection": true,
      "SlowQueryThresholdMs": 3000,
      "SlowQuerySamplingRate": 100,
      "EnableDiffLog": true
    },
    "LogToDb": true
  }
}

Using SqlSugar in a Service

public class UserService
{
    private readonly ISqlSugarClient _db;
    private readonly ISmartCache _cache;

    public UserService(ISqlSugarClient db, ISmartCache cache)
    {
        _db = db;
        _cache = cache;
    }

    // Query vα»›i cache
    public async Task<List<User>> GetActiveUsersAsync()
    {
        var key = _cache.CreateKey("users", "active");
        return await _cache.GetAsync(key, async () =>
        {
            return await _db.Queryable<User>()
                .Where(u => u.IsActive)
                .ToListAsync();
        }, ttlMinutes: 30);
    }

    // Transaction
    public async Task<bool> CreateUserAsync(User user)
    {
        try
        {
            _db.Ado.BeginTran();
            
            await _db.Insertable(user).ExecuteCommandAsync();
            await _db.Insertable(new UserProfile { UserId = user.Id })
                     .ExecuteCommandAsync();
            
            _db.Ado.CommitTran();
            
            // Invalidate cache
            await _cache.RemoveByPrefixAsync("users");
            return true;
        }
        catch
        {
            _db.Ado.RollbackTran();
            throw;
        }
    }

    // Multi-database
    public async Task<List<AuditLog>> GetLogsAsync()
    {
        // Switch to Log database
        return await _db.GetConnection("DB_Log")
            .Queryable<AuditLog>()
            .OrderByDescending(l => l.CreatedAt)
            .Take(100)
            .ToListAsync();
    }
}

Read/Write Splitting (Master-Slave)

{
  "DBConnections": [
    {
      "ConnId": "DB_Main",
      "DBType": 1,
      "Enable": true,
      "HitRate": 50,
      "ConnectionStr": "Server=master;Database=MyDB;...",
      "Slaves": [
        {
          "HitRate": 30,
          "ConnectionStr": "Server=slave1;Database=MyDB;..."
        },
        {
          "HitRate": 20,
          "ConnectionStr": "Server=slave2;Database=MyDB;..."
        }
      ]
    }
  ]
}

Note: Higher HitRate means that slave is used more often. HitRate = 0 means not used.

Encrypted Connection String

{
  "DBConnections": [
    {
      "ConnId": "DB_Main",
      "DBType": 1,
      "Enable": true,
      "ConnectionStr": "BASE64_ENCRYPTED_STRING.SIGNATURE"
    }
  ]
}

The library automatically detects and decrypts connection strings in the base64.signature format.


Large-Scale Cache Setup (5K-20K Users)

// Option 1: All features from appsettings.json
services.AddLargeScaleCacheFeatures();

// Option 2: Custom configuration
services.AddLargeScaleCacheFeatures(new LargeScaleCacheOptions
{
    ExpectedConcurrentUsers = 20000,
    EnableCacheWarming = true,
    EnableTracing = true,
    TracingSamplingRate = 0.05,
    ServiceName = "EMR-Production"
});

// ThΓͺm tΓ­nh nΔƒng riΓͺng lαΊ»
services.AddCacheWarming();
services.AddCacheTracing();
services.AddAdaptiveEviction();
services.AddBatchOperationsConfig();

Redis Cluster Setup

// From appsettings.json
services.AddRedisClusterSupport();

// Or custom configuration
services.AddRedisClusterSupport(options =>
{
    options.EnableCluster = true;
    options.ClusterEndpoints = new List<string>
    {
        "redis-node1:6379",
        "redis-node2:6379",
        "redis-node3:6379"
    };
    options.VirtualNodesPerPhysical = 150;
});

Multi-Tenant Cache Setup

// Add tenant context provider
services.AddDefaultTenantContextProvider();

// ThΓͺm cache decorators vα»›i tenant isolation
services.AddCacheDecorators(
    enableTracing: true,
    enableTenantIsolation: true
);

Basic Cache Operations

using IMEX.Core.Caching.Interfaces;

public class MyService
{
    private readonly ISmartCache _cache;
    
    public MyService(ISmartCache cache)
    {
        _cache = cache;
    }
    
    // Get vα»›i factory pattern
    public async Task<Employee> GetEmployeeAsync(int id)
    {
        var key = _cache.CreateKey("employees", id.ToString());
        return await _cache.GetAsync<Employee>(key, async () =>
        {
            return await _dbContext.Employees.FindAsync(id);
        }, ttlMinutes: 30);
    }
    
    // Batch operations
    public async Task<Dictionary<string, Employee>> GetEmployeesAsync(IEnumerable<int> ids)
    {
        var keys = ids.Select(id => _cache.CreateKey("employees", id.ToString()));
        return await _cache.GetManyAsync<Employee>(keys);
    }
    
    // Remove by prefix
    public async Task InvalidateEmployeeCacheAsync()
    {
        await _cache.RemoveByPrefixAsync("employees");
    }
}

Cache Warming Registration

public class EmployeeCacheWarmer
{
    private readonly CacheWarmingService _warmingService;
    
    public EmployeeCacheWarmer(CacheWarmingService warmingService)
    {
        _warmingService = warmingService;
    }
    
    public void RegisterWarmingTasks()
    {
        // Register critical keys
        _warmingService.RegisterCriticalKey("employees:all", async () =>
        {
            return await _dbContext.Employees.ToListAsync();
        }, priority: 1, ttlMinutes: 60);
        
        _warmingService.RegisterCriticalKey("departments:all", async () =>
        {
            return await _dbContext.Departments.ToListAsync();
        }, priority: 2, ttlMinutes: 120);
    }
}

Performance Monitoring

using IMEX.CORE.Caching.Monitoring;

// Get metrics
var metrics = cache.GetCacheMetrics();

// Export Prometheus format
var prometheusData = CacheOpenTelemetryExporter.ExportPrometheusSnapshot(metrics);

// Metrics bao gα»“m:
// - cache_hit_ratio
// - cache_latency_ms
// - cache_memory_usage_bytes
// - cache_entries_count
// - cache_eviction_count

Database Operations vα»›i Caching

using IMEX.CORE.DBAccess;

public class EmployeeRepository
{
    private readonly DbContextBase _dbContext;
    private readonly ISmartCache _cache;
    
    public async Task<List<Employee>> GetActiveEmployeesAsync(int departmentId)
    {
        var key = _cache.CreateKey("employees", $"dept:{departmentId}:active");
        
        return await _cache.GetAsync(key, async () =>
        {
            return await _dbContext.Queryable<Employee>()
                .Where(e => e.DepartmentId == departmentId && e.IsActive)
                .ToListAsync();
        }, ttlMinutes: 30);
    }
}

πŸ—οΈ Architecture

System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    IMEX.Core                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚   Caching   β”‚  β”‚  Database   β”‚  β”‚  Security   β”‚        β”‚
β”‚  β”‚   System    β”‚  β”‚   Access    β”‚  β”‚   Layer     β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚   Logging   β”‚  β”‚  Utilities  β”‚  β”‚   Models    β”‚        β”‚
β”‚  β”‚   System    β”‚  β”‚   & Helpers β”‚  β”‚   & DTOs    β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                 .NET 8.0/9.0/10.0 Runtime                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Enhanced Cache Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Enterprise Cache System (Large-Scale)                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                              DECORATORS LAYER                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
β”‚  β”‚ TracingDecorator  β”‚  β”‚ TenantDecorator   β”‚  β”‚ EventDrivenDeco.  β”‚        β”‚
β”‚  β”‚ β€’ OpenTelemetry   β”‚  β”‚ β€’ Multi-tenant    β”‚  β”‚ β€’ Cache Events    β”‚        β”‚
β”‚  β”‚ β€’ Prometheus      β”‚  β”‚ β€’ Key isolation   β”‚  β”‚ β€’ Notifications   β”‚        β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                              HYBRID CACHE LAYER                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚                  EnhancedHybridMemoryCache                          β”‚    β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚    β”‚
β”‚  β”‚  β”‚ L1 Memory   β”‚  β”‚ L2 Redis    β”‚  β”‚ Lock        β”‚  β”‚ Circuit     β”‚ β”‚    β”‚
β”‚  β”‚  β”‚ Cache       β”‚β†’ β”‚ Cache       β”‚  β”‚ Manager     β”‚  β”‚ Breaker     β”‚ β”‚    β”‚
β”‚  β”‚  β”‚ (Enhanced)  β”‚  β”‚ (MsgPack)   β”‚  β”‚ (Single-    β”‚  β”‚ (Enhanced)  β”‚ β”‚    β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  Flight)    β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚    β”‚
β”‚  β”‚                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                              L1 CACHE FEATURES                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚  β”‚ Fail-Safe   β”‚  β”‚ Background  β”‚  β”‚ Adaptive    β”‚  β”‚ Prefix/Tag  β”‚         β”‚
β”‚  β”‚ Cache       β”‚  β”‚ Refresh     β”‚  β”‚ TTL         β”‚  β”‚ Manager     β”‚         β”‚
β”‚  β”‚ β€’ Stale     β”‚  β”‚ β€’ SWR       β”‚  β”‚ β€’ Hit-based β”‚  β”‚ β€’ Trie DS   β”‚         β”‚
β”‚  β”‚   fallback  β”‚  β”‚ β€’ Scheduled β”‚  β”‚ β€’ Frequency β”‚  β”‚ β€’ Pattern   β”‚         β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                              L2 CACHE FEATURES                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚ MessagePack     β”‚  β”‚ Redis Cluster   β”‚  β”‚ Consistent Hash β”‚              β”‚
β”‚  β”‚ Provider        β”‚  β”‚ Provider        β”‚  β”‚ Ring            β”‚              β”‚
β”‚  β”‚ β€’ LZ4 compress  β”‚  β”‚ β€’ Cluster mode  β”‚  β”‚ β€’ Virtual nodes β”‚              β”‚
β”‚  β”‚ β€’ Pipeline      β”‚  β”‚ β€’ Sentinel      β”‚  β”‚ β€’ Auto balance  β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                              MANAGEMENT SERVICES                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚  β”‚ Warming     β”‚  β”‚ Eviction    β”‚  β”‚ Memory      β”‚  β”‚ Health      β”‚         β”‚
β”‚  β”‚ Service     β”‚  β”‚ Policy      β”‚  β”‚ Pressure    β”‚  β”‚ Monitor     β”‚         β”‚
β”‚  β”‚ β€’ Startup   β”‚  β”‚ β€’ LRU/LFU   β”‚  β”‚ β€’ Auto GC   β”‚  β”‚ β€’ Real-time β”‚         β”‚
β”‚  β”‚ β€’ Priority  β”‚  β”‚ β€’ Adaptive  β”‚  β”‚ β€’ Auto GC   β”‚  β”‚ β€’ Metrics   β”‚         β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Cache Configuration Classes

CacheConfiguration (Root)
β”œβ”€β”€ HybridCacheConfiguration
β”‚   └── L2 TTL, Factory Timeouts, Sync settings
β”œβ”€β”€ EnhancedMemoryCacheConfiguration
β”‚   └── AdaptiveTtlConfiguration
β”œβ”€β”€ MedicalCacheConfiguration
β”‚   └── Domain-specific TTLs
β”œβ”€β”€ WarmingConfiguration
β”œβ”€β”€ TracingConfiguration
β”œβ”€β”€ EvictionConfiguration
β”œβ”€β”€ LargeScaleConfiguration
β”œβ”€β”€ BatchOperationsConfiguration
β”œβ”€β”€ RedisClusterConfiguration
β”œβ”€β”€ MultiTenantConfiguration
└── DecoratorsConfiguration

πŸ”§ API Reference

Core Interfaces

ISmartCache
public interface ISmartCache : IDisposable, IAsyncDisposable
{
    // Key management
    string CreateKey(string tableName, string key);
    
    // Sync operations
    T? Get<T>(string key);
    T? GetFast<T>(string key);  // Ultra-fast, skip complex features
    void Set<T>(string key, T value, int ttlMinutes);
    void SetFast<T>(string key, T value, int ttlMinutes);
    void SetSmart<T>(string key, T value, int ttlMinutes);
    
    // Async operations
    Task<T?> GetAsync<T>(string key, Func<Task<T>> factory, int ttlMinutes);
    Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> factory, int ttlMinutes);
    Task SetAsync<T>(string key, T value, int ttlMinutes);
    
    // Batch operations
    Task<Dictionary<string, T?>> GetManyAsync<T>(IEnumerable<string> keys);
    Task SetManyAsync<T>(Dictionary<string, T> entries, int ttlMinutes);
    Task RemoveManyAsync(IEnumerable<string> keys);
    Task<Dictionary<string, T>> GetOrCreateManyAsync<T>(
        IEnumerable<string> keys, 
        Func<IEnumerable<string>, Task<Dictionary<string, T>>> factory, 
        int ttlMinutes);
    
    // Removal operations
    Task RemoveAsync(string key);
    Task RemoveByPrefixAsync(string prefix);
    
    // Metrics
    CacheDashboardMetrics GetCacheMetrics();
}
ICacheWarmer
public interface ICacheWarmer
{
    Task WarmCacheAsync(CancellationToken cancellationToken);
    Task WarmCriticalKeysAsync(IEnumerable<CacheWarmingEntry> entries);
    void RegisterCriticalKey<T>(string key, Func<Task<T>> factory, int priority, int ttlMinutes);
}
IEvictionPolicy
public interface IEvictionPolicy
{
    string PolicyName { get; }
    IEnumerable<string> SelectKeysForEviction(
        IEnumerable<CacheEntryInfo> entries, 
        int maxEntriesToEvict);
}

// Available policies:
// - LruEvictionPolicy
// - LfuEvictionPolicy
// - SizeBasedEvictionPolicy
// - TtlEvictionPolicy
// - RandomEvictionPolicy
// - AdaptiveEvictionPolicy
// - CompositeEvictionPolicy
IConsistentHashRing
public interface IConsistentHashRing
{
    void AddNode(string nodeId);
    void RemoveNode(string nodeId);
    string GetNode(string key);
    int NodeCount { get; }
    int VirtualNodeCount { get; }
}

Extension Methods

// CacheSetup extensions
public static class CacheSetup
{
    // Auto-detection
    void AddSmartCacheSetup(this IServiceCollection services, IConfiguration? config);
    
    // Strategy-based
    void AddCacheSetupWithStrategy(this IServiceCollection services, CacheStrategy strategy, IConfiguration? config);
    
    // Large-scale features
    void AddLargeScaleCacheFeatures(this IServiceCollection services, LargeScaleCacheOptions? options);
    void AddCacheWarming(this IServiceCollection services, Action<CacheWarmingConfig>? configure);
    void AddCacheTracing(this IServiceCollection services, Action<CacheTracingConfig>? configure);
    void AddRedisClusterSupport(this IServiceCollection services, Action<RedisClusterOptions>? configure);
    void AddAdaptiveEviction(this IServiceCollection services, Action<EvictionPolicyConfig>? configure);
    void AddBatchOperationsConfig(this IServiceCollection services, Action<BatchOperationsConfig>? configure);
    
    // Multi-tenant
    void AddDefaultTenantContextProvider(this IServiceCollection services);
    void AddCacheDecorators(this IServiceCollection services, bool? enableTracing, bool? enableTenantIsolation);
}

πŸ§ͺ Testing

Run tests

dotnet test .\tests\IMEX.CORE.Tests\IMEX.CORE.Tests.csproj -c Release

Note:

  • Some Performance/Regression tests may depend on hardware/OS scheduler (especially on Windows) and can be less stable than logic unit tests.
  • Some Integration tests (Redis/L2) may be Skipped if the environment does not meet requirements.

To exclude Performance tests from the release pipeline, run with a filter by namespace (depending on how tests are organized):

dotnet test .\tests\IMEX.CORE.Tests\IMEX.CORE.Tests.csproj -c Release --filter FullyQualifiedName!~IMEX.CORE.Tests.Performance

Unit Tests

[Test]
public async Task Cache_Should_Store_And_Retrieve_Data()
{
    // Arrange
    var cache = new EnhancedMemoryCache(memoryCache, config);
    var testData = new { Id = 1, Name = "Test" };
    
    // Act
    await cache.SetAsync("test:key", testData, 10);
    var result = await cache.GetAsync<object>("test:key", () => Task.FromResult(testData));
    
    // Assert
    Assert.That(result, Is.Not.Null);
    Assert.That(result.Id, Is.EqualTo(1));
}

Batch Operations Tests

[Test]
public async Task BatchOperations_Should_Handle_Multiple_Keys()
{
    // Arrange
    var entries = new Dictionary<string, Employee>
    {
        ["emp:1"] = new Employee { Id = 1, Name = "John" },
        ["emp:2"] = new Employee { Id = 2, Name = "Jane" }
    };
    
    // Act
    await cache.SetManyAsync(entries, ttlMinutes: 30);
    var results = await cache.GetManyAsync<Employee>(entries.Keys);
    
    // Assert
    Assert.That(results.Count, Is.EqualTo(2));
}

Performance Tests

[Test]
public async Task Cache_Performance_Should_Be_Optimal()
{
    // Arrange
    var analyzer = new CachePerformanceAnalyzer();
    
    // Act
    var results = await analyzer.RunBenchmarkAsync(100_000);
    
    // Assert
    Assert.That(results.SetOperations.Improvement, Is.GreaterThan(2.0)); // 2x faster
    Assert.That(results.MemoryUsage.MemorySavings, Is.GreaterThan(40.0)); // 40% less memory
}

πŸ“ˆ Performance

Benchmark Results

Operation Standard .NET IMEX.Core Improvement
Set Operations 100ns 25ns 4x faster
Get Operations 50ns 15ns 3.3x faster
Memory Usage 100% 30-50% 50-70% less
GC Pressure 100% 10-20% 80-90% less
Throughput 800K ops/sec 3.2M ops/sec 4x higher
Batch Operations 10K ops/sec 500K ops/sec 50x faster

Large-Scale Performance

  • Concurrent Users: 5K-20K vα»›i Redis Cluster
  • Hit Ratio: 90-98% vα»›i background refresh
  • Response Time: < 500ΞΌs cho cache hits
  • Memory Efficiency: 50-70% tiαΊΏt kiệm memory
  • Distributed Performance: Sub-millisecond vα»›i Redis pipelining

Advanced Optimizations

  • Object Pooling: 90% reduction trong memory allocations
  • Span-based Operations: Zero-allocation string operations
  • Adaptive Circuit Breaker: Auto-adjust error threshold
  • Memory Advisor: Auto-optimize cache size
  • Consistent Hashing: Even distribution across Redis nodes

πŸ“‚ Samples

Dα»± Γ‘n cung cαΊ₯p sample code để bαΊ―t Δ‘αΊ§u nhanh:

Sample MΓ΄ tαΊ£
CacheTestApp Ứng dα»₯ng mαΊ«u dΓΉng IMEX Core vα»›i cache: AddImexCore, ISmartCache, GetAsync/SetAsync.
HealthCheckExample.cs VΓ­ dα»₯ health check endpoint (IdGenerator, cache, DB) để tΓ­ch hợp vα»›i ASP.NET Core Health Checks hoαΊ·c monitoring.

ChαΊ‘y sample:

cd samples/CacheTestApp
dotnet run

πŸ“Š Benchmarks

Benchmark cache (L1, Hybrid, Redis, so sΓ‘nh FusionCache, v.v.) nαΊ±m trong thΖ° mα»₯c benchmarks/CacheBenchmarks:

  • HOW-TO-RUN-BENCHMARKS.md – HΖ°α»›ng dαΊ«n chαΊ‘y benchmark (BenchmarkDotNet).
  • README.md – Tα»•ng quan cΓ‘c bα»™ benchmark vΓ  kα»‹ch bαΊ£n.

ChαΊ‘y nhanh (vΓ­ dα»₯):

cd benchmarks/CacheBenchmarks
dotnet run -c Release

❓ Troubleshooting & FAQ

Redis / Cache

Triệu chα»©ng NguyΓͺn nhΓ’n thường gαΊ·p CΓ‘ch xα»­ lΓ½
Redis connection failed / timeout Redis chΖ°a chαΊ‘y, sai host/port, firewall chαΊ·n. Kiểm tra Redis Δ‘ang chαΊ‘y (redis-cli ping), Δ‘ΓΊng connection string (vΓ­ dα»₯ localhost:6379), mở port 6379.
Hybrid cache chỉ dΓΉng L1, khΓ΄ng ghi L2 L2 (Redis) lα»—i hoαΊ·c circuit breaker mở. Xem log/exception khi kαΊΏt nα»‘i Redis; kiểm tra cαΊ₯u hΓ¬nh circuit breaker (CbErrorRateThreshold, CbBreakSeconds) trong Redis/Cache.Hybrid.
ISmartCache not registered DΓΉng strategy Redis-only; ISmartCache chỉ Δ‘Δƒng kΓ½ cho Memory vΓ  Hybrid. DΓΉng UseMemoryCache() hoαΊ·c UseHybridCache(redis); hoαΊ·c tα»± Δ‘Δƒng kΓ½ implementation ISmartCache cho Redis nαΊΏu cαΊ§n.

SqlSugar / Database

Triệu chα»©ng NguyΓͺn nhΓ’n thường gαΊ·p CΓ‘ch xα»­ lΓ½
Connection string not found / DB khΓ΄ng kαΊΏt nα»‘i MainDB hoαΊ·c ConnId khΓ΄ng khα»›p vα»›i DBConnections. Trong appsettings kiểm tra MainDB trΓΉng ConnId cα»§a mα»™t connection trong DBConnections; connection Δ‘Γ³ cΓ³ Enable: true vΓ  ConnectionStr Δ‘ΓΊng.
Slow query khΓ΄ng log ChΖ°a bαΊ­t slow query detection. DΓΉng UseSlowQueryDetection(thresholdMs, samplingRate) trΓͺn SqlSugarOptionsBuilder hoαΊ·c cαΊ₯u hΓ¬nh AppSettings:SqlAOP:EnableSlowQueryDetection vΓ  SlowQueryThresholdMs.
Read/Write splitting khΓ΄ng dΓΉng slave HitRate = 0 hoαΊ·c chỉ cΓ³ master. ThΓͺm Slaves vα»›i HitRate > 0 (tα»•ng HitRate thường 100); Δ‘αΊ£m bαΊ£o connection slave hợp lệ.

CαΊ₯u hΓ¬nh chung

Triệu chα»©ng NguyΓͺn nhΓ’n thường gαΊ·p CΓ‘ch xα»­ lΓ½
AddImexCore tα»« appsettings khΓ΄ng Δ‘ΓΊng Section ImexCore thiαΊΏu hoαΊ·c sai key. Đảm bαΊ£o cΓ³ section ImexCore vα»›i CacheMode, RedisConnection (nαΊΏu Hybrid/Redis), UseSqlSugar, EnableMonitoring, EnableCacheWarming theo appsettings mαΊ«u.
UseImexCore() sau khi Build() vαΊ«n lα»—i Middleware chΖ°a được gọi hoαΊ·c thα»© tα»± middleware. Gọi app.UseImexCore() (hoαΊ·c UseImexCore(configure)) sau builder.Build(), trΖ°α»›c app.Run().
CachePreset / CacheMode khΓ΄ng nhαΊ­n ThiαΊΏu namespace. ThΓͺm using IMEX.CORE.Extensions; vΓ  using IMEX.CORE.Caching.Configuration;; dΓΉng CacheConfigurationBuilder.CachePreset.HighPerformance cho preset.

TΓ i liệu thΓͺm

🀝 Contributing

We welcome contributions from the community! To contribute:

  1. Fork the repository
  2. Create a branch for your feature (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow .NET coding standards
  • Write unit tests for new code
  • Update documentation
  • Ensure backward compatibility

πŸ“„ License

This project is distributed under the MIT license. See LICENSE for details.

πŸ“ž Contact

  • Author: truongnv2412
  • Company: IMEXsoft.,JSC
  • Email: support@imexsoft.net
  • Website: https://imexsoft.net

πŸ™ Acknowledgments

  • SqlSugar ORM - Powerful ORM framework
  • Serilog - Structured logging framework
  • StackExchange.Redis - High-performance Redis client
  • MessagePack - Fast serialization library
  • OpenTelemetry - Observability framework
  • Microsoft.Extensions - .NET extension libraries

⭐ If this library is useful to you, please give us a star! ⭐

Made with ❀️ by truongnv2412

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.  net10.0 is compatible.  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. 
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
2.0.6 110 1/30/2026
2.0.3 119 1/13/2026
2.0.2.1 120 1/8/2026
2.0.1 194 12/25/2025
2.0.0 306 12/16/2025
1.0.3.10 155 12/12/2025
1.0.3.9 440 12/11/2025
1.0.3.8 176 11/28/2025
1.0.3.7 215 11/26/2025
1.0.3.6 209 10/3/2025
1.0.2.35 553 9/17/2025
Loading failed

v2.0.6: FLUENT API & PACKAGE UPDATE

Fluent API (evaluation complete):
β€’ ImexCoreOptions: UseCache, UseCachePreset, ConfigureCache, UseSqlSugar() / UseSqlSugar(configure), UseRateLimiting(configure), UseMonitoring(configure), UseImexCore(app, configure)
β€’ CacheConfigurationBuilder: UseMemoryCache/Hybrid/Redis, WithPreset, ConfigureL1, ConfigureHybrid, ConfigureMonitoring, WithRateLimiting(configure), WithMemoryPressureManagement. ISmartCache registered for Memory/Hybrid
β€’ SqlSugarOptionsBuilder: Use* / Configure* / On* covering SqlSugarOptions (NOLOCK, slow query, callbacks, MoreSettings)
β€’ ImexCoreMiddlewareOptions: SetApplicationRunningState for UseImexCore

Performance (unchanged):
β€’ L1 Cache Hit: 199ns, 40M ops/sec, zero lock contention
β€’ Circuit breaker, fail-safe, background refresh, single-flight

Package: Title/Description/Tags updated per Fluent API evaluation.