Set Up Memory Cache

This guide shows you how to configure and use in-memory caching in Azu.

Basic Setup

Configure the memory cache store:

Azu.configure do |config|
  config.cache = Azu::Cache::MemoryStore.new
end

Using the Cache

Store and Retrieve Values

# Store a value
Azu.cache.set("user:1", user.to_json)

# Retrieve a value
json = Azu.cache.get("user:1")
if json
  user = User.from_json(json)
end

# With expiration (TTL)
Azu.cache.set("session:abc123", session_data, expires_in: 30.minutes)

Fetch Pattern

Use fetch to get or compute a value:

Delete Keys

Check Existence

Clear All

Caching in Endpoints

Cache Configuration Options

Namespacing Keys

Organize cache keys with namespaces:

Cache Invalidation

Invalidate related cache entries:

Thread Safety

The memory cache is thread-safe by default:

Cache Statistics

Monitor cache performance:

When to Use Memory Cache

Good for:

  • Single server deployments

  • Session data

  • Frequently accessed, rarely changed data

  • Development and testing

Consider Redis for:

  • Multi-server deployments

  • Shared state across processes

  • Persistence requirements

  • Large cache sizes

See Also

Last updated

Was this helpful?