Build a High-Performance Caching Layer with Cache Invalidation Strategy

Implement a production-grade caching layer with smart invalidation, stampede prevention, and monitoring in your language.

๐Ÿ“ The Prompt

Design and implement a robust caching layer in [PROGRAMMING_LANGUAGE] for a [APPLICATION_TYPE] application that reduces database load and improves response times. The caching solution should use [CACHE_BACKEND] (e.g., Redis, Memcached, in-memory) and meet the following requirements: 1. **Cache Service Abstraction**: Create a cache service class/module with a clean interface that abstracts the underlying cache backend. Include these core methods: - `get(key)` โ€” retrieve cached data with automatic deserialization - `set(key, value, ttl)` โ€” store data with a configurable TTL (default: [DEFAULT_TTL] seconds) - `delete(key)` โ€” remove a specific cache entry - `invalidatePattern(pattern)` โ€” bulk invalidate keys matching a pattern - `getOrSet(key, fetchFunction, ttl)` โ€” cache-aside pattern that fetches from the source on cache miss 2. **Key Naming Convention**: Implement a structured key generation strategy using the format `[APP_PREFIX]:{entity}:{identifier}:{variant}` to avoid collisions and enable pattern-based invalidation. 3. **Cache Invalidation Strategy**: Implement [INVALIDATION_STRATEGY] (choose from: write-through, write-behind, event-driven, or TTL-based). Include logic that automatically invalidates related cache entries when underlying data is mutated (e.g., when a [PRIMARY_ENTITY] is updated, invalidate its detail cache, list caches, and related entity caches). 4. **Cache Stampede Prevention**: Add mutex/locking mechanisms to prevent thundering herd problems when a popular cache key expires and multiple requests try to regenerate it simultaneously. 5. **Monitoring & Logging**: Track cache hit/miss ratios, latency, and error rates. Include a method to retrieve cache health statistics. 6. **Graceful Degradation**: Ensure the application continues to function (by falling back to the database) if the cache backend becomes unavailable. Provide fully working code with error handling, TypeDoc/JSDoc/docstring comments, and a usage example showing how to integrate this caching layer into a [FRAMEWORK] controller or service for the [PRIMARY_ENTITY] entity.

๐Ÿ’ก Tips for Better Results

Specify your real entity names (e.g., 'Product', 'User') so the invalidation logic maps to your actual data relationships. If you're unsure which invalidation strategy to pick, describe your read/write ratio โ€” the AI can recommend the best approach. Request unit tests as a follow-up to ensure your caching layer handles edge cases like serialization errors and connection failures.

๐ŸŽฏ Use Cases

Backend developers building data-heavy applications who need to reduce database queries and latency with a well-structured, maintainable caching solution.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.