鱼皮AI导航
鱼皮AI导航
  • 主页
  • 社区
  • AI工具
  • AI知识库
  • AI提示词
  • MCP
  • Skills
  • AI学习资源
  • AI资讯
  • GitHub开源
登录/注册

implementing-database-caching

构建多级缓存体系以优化数据库性能,涵盖 Redis 分布式缓存部署、应用层内存缓存集成及静态资源 CDN 加速,支持缓存策略选型、键设计、失效机制与容错监控全流程。

数据库
访问 GitHub
文档Skills.MD
name
implementing-database-caching
description
Process use when you need to implement multi-tier caching to improve database performance. This skill sets up Redis, in-memory caching, and CDN layers to reduce database load. Trigger with phrases like "implement database caching", "add Redis cache layer", "improve query performance with caching", or "reduce database load".
allowed-tools
Read, Write, Edit, Grep, Glob, Bash(redis-cli:*), Bash(docker:redis:*)
version
1.0.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
license
MIT

Database Cache Layer

This skill provides automated assistance for database cache layer tasks.

Prerequisites

Before using this skill, ensure:

  • Redis server available or ability to deploy Redis container
  • Understanding of application data access patterns and hotspots
  • Knowledge of which queries/data benefit most from caching
  • Monitoring tools to measure cache hit rates and performance
  • Development environment for testing caching implementation
  • Understanding of cache invalidation requirements for data consistency

Instructions

Step 1: Analyze Caching Requirements

  1. Profile database queries to identify slow or frequently executed queries
  2. Determine which data is read-heavy vs write-heavy
  3. Identify data that can tolerate eventual consistency
  4. Calculate expected cache size and Redis memory requirements
  5. Document current database load and target performance metrics

Step 2: Choose Caching Strategy

  1. Cache-Aside (Lazy Loading): Application checks cache first, loads from DB on miss
    • Best for: Read-heavy workloads, unpredictable access patterns
    • Pros: Only caches requested data, simple to implement
    • Cons: Cache misses incur database hit, stale data possible
  2. Write-Through: Application writes to cache and database simultaneously
    • Best for: Write-heavy workloads needing consistency
    • Pros: Cache always consistent, no stale data
    • Cons: Write latency, unnecessary caching of rarely-read data
  3. Write-Behind (Write-Back): Application writes to cache, async writes to database
    • Best for: High write throughput requirements
    • Pros: Low write latency, batched database writes
    • Cons: Risk of data loss, complexity in implementation

Step 3: Design Cache Architecture

  1. Set up Redis as distributed cache layer (L2 cache)
  2. Implement in-memory LRU cache in application (L1 cache)
  3. Configure CDN for static assets (images, CSS, JS)
  4. Design cache key naming convention (e.g., user:123:profile)
  5. Define TTL (Time To Live) for different data types

Step 4: Implement Caching Code

  1. Add Redis client library to application dependencies
  2. Create cache wrapper functions (get, set, delete, invalidate)
  3. Modify database query code to check cache before DB query
  4. Implement cache population on cache miss
  5. Add error handling for cache failures (fail gracefully to database)

Step 5: Configure Cache Invalidation

  1. Implement TTL-based expiration for time-sensitive data
  2. Add explicit cache invalidation on data updates/deletes
  3. Use cache tags or patterns for bulk invalidation
  4. Implement cache warming for critical data after deployments
  5. Set up cache stampede prevention (lock/queue on miss)

Step 6: Monitor and Optimize

  1. Track cache hit rate, miss rate, and eviction rate
  2. Monitor Redis memory usage and eviction policy
  3. Analyze query performance improvements
  4. Adjust TTLs based on data update frequency
  5. Identify and cache additional hot data

Output

This skill produces:

Redis Configuration: Docker Compose or config files for Redis deployment with appropriate memory and eviction settings

Caching Code: Application code implementing cache-aside, write-through, or write-behind patterns

Cache Key Schema: Documentation of cache key naming conventions and TTL settings

Monitoring Dashboards: Metrics for cache hit rates, memory usage, and performance improvements

Cache Invalidation Logic: Code for explicit and implicit cache invalidation on data changes

Error Handling

Cache Connection Failures:

  • Implement circuit breaker pattern to prevent cascading failures
  • Fall back to database when cache is unavailable
  • Log cache connection errors for monitoring
  • Retry cache connections with exponential backoff
  • Consider read-replica or cache cluster for high availability

Cache Stampede:

  • Implement probabilistic early expiration (PER) for TTLs
  • Use distributed locks (Redis SETNX) to prevent concurrent cache population
  • Queue cache refresh requests instead of parallel execution
  • Add jitter to TTLs to spread expiration times
  • Use stale-while-revalidate pattern for acceptable delays

Stale Data Issues:

  • Implement versioning in cache keys (e.g., user:123:v2)
  • Use cache tags for related data invalidation
  • Set aggressive TTLs for frequently changing data
  • Implement active cache invalidation on data updates
  • Monitor data consistency between cache and database

Memory Pressure:

  • Configure Redis eviction policy (allkeys-lru recommended)
  • Monitor Redis memory usage and set max memory limits
  • Implement tiered caching (hot data in Redis, warm data in DB)
  • Reduce TTLs for less critical data
  • Scale Redis horizontally with cluster mode

Resources

Redis Configuration Templates:

  • Docker Compose: {baseDir}/docker/redis-compose.yml
  • Redis config: {baseDir}/config/redis.conf
  • Cluster config: {baseDir}/config/redis-cluster.conf

Caching Code Examples: {baseDir}/examples/caching/

  • Cache-aside pattern (Node.js, Python, Java)
  • Write-through pattern
  • Cache invalidation strategies
  • Distributed locking

Cache Key Design Guide: {baseDir}/docs/cache-key-design.md Performance Tuning: {baseDir}/docs/cache-performance-tuning.md Monitoring Setup: {baseDir}/monitoring/redis-dashboard.json

Overview

This skill provides automated assistance for the described functionality.

Examples

Example usage patterns will be demonstrated in context.

数据库

快捷安装

复制到终端去安装 Skill

npxskillsaddjeremylongshore/claude-code-plugins-plus-skills--skill"implementing-database-caching"
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
🔥热门工具
朱雀AI检测朱雀AI检测
朱雀AI检测助手朱雀AI检测助手
LovartLovart
老鱼简历老鱼简历
WritePass官网WritePass官网
面多多面多多
CursorCursor
星月写作星月写作
朱雀AI检测助手朱雀AI检测助手
零感AI零感AI
Skills 推荐
relational-database-mcp-cloudbase
面向 CloudBase 关系型数据库的自动化管理,支持安全执行只读查询、写入与结构变更操作,并可检视及调整表级安全规则,确保在无应用上下文的代理环境中精准控制数据访问与修改权限。
database-index-advisor
通过分析数据库查询负载和现有索引配置,自动识别缺失的高效索引以加速慢查询,同时发现长期未被使用的冗余索引以便清理,从而在提升读取性能的同时节省存储空间并优化写入效率。
detecting-sql-injection-vulnerabilities
自动识别和分析应用程序代码及数据库查询中的 SQL 注入风险,覆盖输入点检测、查询构造审查、参数化验证与安全模式推荐,生成结构化安全报告并提供修复建议。
database
规范 NestJS 应用的数据访问层设计,涵盖 ORM 选型、连接池与分片等可扩展架构,强制使用仓库模式隔离数据库逻辑,通过异步配置加载敏感信息,禁用生产环境自动同步,依赖迁移脚本实现零停机演进,并统一要求分页、索引声明与事务保护。
prisma-schema-helper
提供 Prisma 数据模型定义的自动化辅助能力,涵盖模式校验、关系配置、字段类型推导及迁移脚本生成,支持 Node.js 等后端技术栈的数据库层快速搭建与维护。