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.