in-memory-caching

Implement thread-safe in-memory caching with database-triggered invalidation for .NET applications.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/Xentinus/ai-toolbox --skill in-memory-caching
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: in-memory-caching
Source: https://github.com/Xentinus/ai-toolbox/tree/main/Skills/in-memory-caching
Command: npx skills add https://github.com/Xentinus/ai-toolbox --skill in-memory-caching

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This skill addresses the repetitive, expensive data access patterns by providing a production-ready in-memory caching layer for .NET applications, reducing database load and latency while enabling responsive user experiences.

Core Features & Use Cases

  • Thread-safe storage: Uses ConcurrentDictionary to store and retrieve cached entities with minimal contention.
  • Database-driven invalidation: Leverages a TableRecordChanges mechanism and background refresh to ensure cache coherence across changes.
  • Background management: Includes a CacheManagementService that initializes caches on startup and processes invalidations periodically.
  • Database support and hybrid options: Supports PostgreSQL and MSSQL with optional Redis-based two-level caching for even faster access or pure in-memory caching when Redis is unavailable or undesired.
  • Usage scenarios: Ideal for read-heavy workloads, paging, and hot data like reference lookups, where stale data is acceptable within configured TTLs.

Quick Start

  • Wire up CacheManagementService in your ASP.NET Core app, register your cache function classes, and configure your database adapters.
  • Ensure you have the necessary NuGet packages (Microsoft.Extensions.Caching.StackExchangeRedis for Redis, Microsoft.EntityFrameworkCore for querying) and a DbContext for your entities.
  • Run the application and observe initialization logs; your primary caches will be populated and refreshed automatically.

Frequently Asked Questions about in-memory-caching

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement in-memory caching in a .NET application to reduce database load?

You can implement in-memory caching in .NET using ConcurrentDictionary for thread-safe storage alongside a background CacheManagementService. This skill handles cache initialization on startup and processes invalidations periodically, supporting both PostgreSQL and MSSQL to reduce database load and latency.

How does database-driven cache invalidation work with PostgreSQL and MSSQL?

Database-driven cache invalidation leverages a TableRecordChanges mechanism to detect data modifications in PostgreSQL and MSSQL. A background CacheManagementService processes these invalidations periodically, automatically refreshing affected cache entries to maintain coherence without manual intervention.

Can I use Redis for hybrid caching alongside the in-memory cache in .NET?

Yes, Redis is supported as an optional two-level hybrid caching layer alongside the in-memory cache. The skill provides pure in-memory caching using ConcurrentDictionary when Redis is unavailable, and integrates Redis for faster access speeds using Microsoft.Extensions.Caching.StackExchangeRedis.

Do I need Entity Framework Core to set up in-memory caching with this approach?

Yes, Entity Framework Core and a DbContext for your entities are required to query the database. You also need the Microsoft.Extensions.Caching.StackExchangeRedis NuGet package if you choose to enable the optional Redis-based hybrid caching configuration.

When should I use pure in-memory caching versus a Redis hybrid caching approach?

Pure in-memory caching is ideal for read-heavy workloads and hot data like reference lookups where stale data is acceptable within configured TTLs. A Redis hybrid approach is better when you need faster distributed access across multiple instances or want a two-level caching architecture.