Enable Query Caching

Improve performance by caching frequently executed queries.

Prerequisites

  • CQL installed and configured

  • Understanding of your query patterns

Basic Query Caching

Enable caching for specific queries:

# Cache user by ID for 5 minutes
user = User.cache(5.minutes).find(user_id)

# Cache query results
posts = Post.cache(1.minute)
            .where(published: true)
            .order(created_at: :desc)
            .limit(10)
            .all

Configure Default Cache

Set up a cache store in your schema:

Cache Stores

Memory Cache

Simple in-memory cache (per-process):

Redis Cache

Shared cache across processes:

See Configure Redis Cache for details.

Cache Keys

CQL generates cache keys from the query:

Custom Cache Keys

Override the default key:

Cache Invalidation

Manual Invalidation

Automatic Invalidation

Invalidate on model changes:

Conditional Caching

Cache based on conditions:

Cache Statistics

Monitor cache performance:

Verify Caching

Best Practices

  1. Cache read-heavy queries - Queries run many times with same parameters

  2. Short TTLs for volatile data - Data that changes frequently

  3. Long TTLs for static data - Reference tables, configurations

  4. Invalidate on writes - Clear cache when data changes

See Also

Last updated

Was this helpful?