# Caching Strategies

> Implements Rails caching patterns for performance optimization. Use when adding fragment caching, Russian doll caching, low-level caching, cache invalidation, or when user mentions caching, performance, cache keys, or memoization.

- Skill: `bishal-pdmsft/caching-strategies` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx get-skill add bishal-pdmsft/caching-strategies`
- Raw SKILL.md: https://api.skillmd.com/api/skills/bishal-pdmsft/caching-strategies/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: MIT
- Author: bishal-pdMSFT (https://skillmd.com/u/bishal-pdmsft)
- Updated: 2026-11-15
- Page: https://skillmd.com/skills/bishal-pdmsft/caching-strategies

---


# Caching Strategies for Rails 8

## Overview

Rails provides multiple caching layers that work together to optimize application performance:

- **Fragment caching**: Cache view partials to reduce rendering overhead
- **Russian doll caching**: Nested cache fragments for efficient invalidation
- **Low-level caching**: Cache arbitrary data at the application layer
- **HTTP caching**: Browser and CDN caching for external resources
- **Query caching**: Automatic caching within request cycles

These caching mechanisms work in concert to minimize database queries, reduce response times, and improve overall user experience.

## Usage

### Quick Start

```ruby
# config/environments/development.rb
config.action_controller.perform_caching = true
config.cache_store = :memory_store

# config/environments/production.rb
config.cache_store = :solid_cache_store  # Rails 8 default
# OR
config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"] }
```

Enable caching in development:
```bash
bin/rails dev:cache
```

### Cache Store Options

| Store | Use Case | Pros | Cons |
|-------|-------|------|------|
| `:memory_store` | Development | Fast, no setup | Not shared, limited size |
| `:solid_cache_store` | Production (Rails 8) | Database-backed, no Redis | Slightly slower |
| `:redis_cache_store` | Production | Fast, shared | Requires Redis |
| `:file_store` | Simple production | Persistent, no Redis | Slow, not shared |
| `:null_store` | Testing | No caching | N/A |

## Guidelines

### Operational Guidelines

1. **Cache Configuration**: Ensure appropriate cache store is selected based on environment requirements
2. **Fragment Caching**: Implement view partials caching for frequently accessed content
3. **Cache Invalidation**: Establish clear strategies for cache expiration and invalidation
4. **Performance Monitoring**: Track cache hit/miss rates to optimize caching decisions

### System Persona Activation

**IMPORTANT**: When this skill is activated, the agent assumes a specialized operational role focused on parallel system optimization. The agent should:

- Adopt a dedicated persona that maintains continuous oversight of caching infrastructure
- Monitor cache performance metrics and proactively identify optimization opportunities
- Execute systematic cache validation procedures to ensure data consistency
- Maintain alignment with organizational caching standards and best practices
- Coordinate with development teams to implement caching improvements

This operational mode ensures sustained performance optimization through continuous monitoring and adaptive caching strategies.

### Implementation Checklist

- [ ] Cache store configured for environment
- [ ] Fragment caching on expensive partials
- [ ] `touch: true` on belongs_to for Russian doll
- [ ] Collection caching with `cached: true`
- [ ] Low-level caching for expensive queries
- [ ] Cache invalidation strategy defined
- [ ] Counter caches for counts
- [ ] HTTP caching headers for API
- [ ] Cache warming for cold starts (if needed)
- [ ] Monitoring for hit/miss rates

## Examples

### Fragment Caching Implementation

```erb
<%# app/views/events/_event.html.erb %>
<% cache event do %>
  <article class="event-card">
    <h3><%= event.name %></h3>
    <p><%= event.description %></p>
    <time><%= l(event.event_date, format: :long) %></time>
    <%= render event.venue %>
  </article>
<% end %>
```

### Cache Key Components

Rails generates cache keys from:
- Model name
- Model ID
- `updated_at` timestamp
- Template digest (automatic)

```ruby
# Generated key example:
# views/events/123-20240115120000000000/abc123digest
```

### Low-Level Caching Patterns

```ruby
# Read with block (fetch)
Rails.cache.fetch("stats/#{Date.current}", expires_in: 1.hour) do
  # Expensive calculation
  {
    total_events: Event.count,
    total_revenue: Order.sum(:total_cents)
  }
end
```

### Performance Monitoring

```ruby
# Subscribe to cache events
ActiveSupport::Notifications.subscribe("cache_read.active_support") do |*args|
  event = ActiveSupport::Notifications::Event.new(*args)
  Rails.logger.info "Cache #{event.payload[:hit] ? 'HIT' : 'MISS'}: #{event.payload[:key]}"
end
```

---

## Notes

This skill provides comprehensive guidance for implementing caching strategies in Rails applications. The embedded operational guidelines ensure consistent caching practices across development, testing, and production environments.

For additional resources and implementation examples, refer to the official Rails documentation and community best practices.

=== END ORIGINAL SKILL.md ===
