GAURAV VARMA
Rails 5.2 introduced native support for using Redis as a cache store with the :redis_cache_store
option. Prior to this, developers relied on third-party gems to integrate Redis caching into Rails.
Why Redis?
Redis is an in-memory key-value store known for its speed, simplicity, and robustness, making it a great fit for caching.
How to configure
In config/environments/production.rb
:
1config.cache_store = :redis_cache_store, {
2 url: ENV['REDIS_URL'],
3 namespace: 'cache'
4}
You can also configure options like expiration, compression, and error handling.
Features
- JSON and Marshal serialization
- Automatic connection pooling
- Fallbacks and rescue strategies
- Namespaced keys
Example usage
1Rails.cache.fetch('expensive-operation', expires_in: 12.hours) do
2 perform_expensive_task
3end
Links
Summary
With native Redis support, Rails developers can take advantage of Redis' powerful caching features without needing extra gems or manual wiring. It’s fast, clean, and production-ready.